//Insira um Panel chamado: jpanelDinamico
//Mude o layout para GridBagLayout
//Mude o layout para GridBagLayout
private Map<String, JTextField> jtxtMap = new HashMap<>();
private GridBagConstraints gbc = new GridBagConstraints();
private void limparPainel(){
this.jpanelDinamico.removeAll();
this.jtxtMap.clear();
}//end method
private void atualizarPainel(){
SwingUtilities.invokeLater(() -> { updatePanel(this.jpanelDinamico); });
}//end method
private void updatePanel(JPanel panel) {
panel.revalidate();
panel.repaint();
this.pack();
}//end method
private void addLabelTextFieldPair(String labelText, String fieldName, JPanel panel) {
// Largura (preferred) do JTextField
JTextField textField = new JTextField(20);
// Armazena referência ao JTextField dinâmico
this.jtxtMap.put(fieldName, textField);
JLabel label = new JLabel(labelText);
//Largura da label 50. Altura igual do jtextField
label.setPreferredSize(new Dimension(50, textField.getPreferredSize().height));
gbc.gridx = 1;
panel.add(label, gbc);
gbc.gridx = 2;
panel.add(textField, gbc);
}//end method
private GridBagConstraints gbc = new GridBagConstraints();
private void limparPainel(){
this.jpanelDinamico.removeAll();
this.jtxtMap.clear();
}//end method
private void atualizarPainel(){
SwingUtilities.invokeLater(() -> { updatePanel(this.jpanelDinamico); });
}//end method
private void updatePanel(JPanel panel) {
panel.revalidate();
panel.repaint();
this.pack();
}//end method
private void addLabelTextFieldPair(String labelText, String fieldName, JPanel panel) {
// Largura (preferred) do JTextField
JTextField textField = new JTextField(20);
// Armazena referência ao JTextField dinâmico
this.jtxtMap.put(fieldName, textField);
JLabel label = new JLabel(labelText);
//Largura da label 50. Altura igual do jtextField
label.setPreferredSize(new Dimension(50, textField.getPreferredSize().height));
gbc.gridx = 1;
panel.add(label, gbc);
gbc.gridx = 2;
panel.add(textField, gbc);
}//end method
...
//Como usar
this.limparPainel();
jtxtMap.get("nameField").setText("New Name");
jtxtMap.get("ageField").setText("30");
jtxtMap.get("emailField").setText("new@example.com");
this.atualizarPainel();
jtxtMap.get("nameField").setText("New Name");
jtxtMap.get("ageField").setText("30");
jtxtMap.get("emailField").setText("new@example.com");
this.atualizarPainel();
Nenhum comentário:
Postar um comentário