I tried to add a label
to each mandatory component of my screen that is empty (field 01 and field 02), so I created a list of labels
with the corresponding amount of fields / components. However, the attempts I made to "set" the corresponding messages of each label
(with the names of each field) do not work properly.
In the example I used only 3 fields of type JTextField
, but the idea is that the number of fields can vary.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class AdicionaCmp extends JFrame {
private MeuJTextField jTextField1 = new MeuJTextField(true, "Campo 01");
private MeuJTextField jTextField2 = new MeuJTextField(true, "Campo 02");
private MeuJTextField jTextField3 = new MeuJTextField(false, "Campo 03");
private JButton jButton = new JButton("Validar");
//private JLabel labelInferior = new JLabel();
private Font fonte = new Font("Arial", Font.PLAIN, 10);
private List<JLabel> listLabel = new ArrayList<>();
private List<JComponent> listComponentes = new ArrayList<>();
public static void main(String[] args) {
EventQueue.invokeLater(AdicionaCmp::new);
}
public AdicionaCmp() {
add(montaTela());
//setSize(600, 350);
pack();
setVisible(true);
actionButton();
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private JComponent montaTela() {
JPanel painelPrincipal = new JPanel();
JPanel painel = new JPanel();
painel.setLayout(new GridBagLayout());
adicionaComponente(1, 1, 1, 1, painel, painelPrincipal);
adicionaComponente(1, 1, 1, 2, jTextField1, painel);
adicionaComponente(2, 1, 1, 2, jTextField2, painel);
adicionaComponente(3, 1, 1, 2, jTextField3, painel);
adicionaComponente(4, 1, 1, 1, jButton, painel);
return painelPrincipal;
}
public void adicionaComponente(int linha, int coluna, int linhas, int colunas, JComponent componente, JPanel painel) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = linha;
gbc.gridx = coluna;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(5, 5, 5, 5);
JPanel painelInterno = new JPanel();
painelInterno.setLayout(new GridBagLayout());
JLabel rotulo = new JLabel();
if (componente instanceof MeuJTextField) {
rotulo = new JLabel(((MeuJTextField) componente).getNome());
}
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.gridx = 1;
gbc1.gridy = 1;
gbc1.anchor = GridBagConstraints.WEST;
gbc1.insets = new Insets(5, 5, 5, 5);
painelInterno.add(rotulo, gbc1);
GridBagConstraints gbc2 = (GridBagConstraints) gbc1.clone();
gbc2.gridx = 2;
gbc2.fill = GridBagConstraints.BOTH;
gbc2.gridwidth = GridBagConstraints.REMAINDER; //Especifica que esse componente é o último componente em sua coluna ou linha.
painelInterno.add(componente, gbc2);
GridBagConstraints gbc3 = (GridBagConstraints) gbc1.clone();
gbc3.gridx = 2;
gbc3.gridy = 2;
listComponentes.add(componente);
if ((componente instanceof MeuJTextField) && (((MeuJTextField) componente).eObrigatorio())) {
JLabel labelInferior = new JLabel();
for (int i = 0; i < listComponentes.size(); i++) {
labelInferior = new JLabel();
painelInterno.add(labelInferior, gbc3);
setMessage(listLabel, ((MeuJTextField) componente).getNome());
//listLabel.add(labelInferior);
}
listLabel.add(labelInferior);
}
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx++;
gbc.gridheight = linhas;
gbc.gridwidth = colunas;
painel.add(painelInterno, gbc);
}
private void actionButton() {
jButton.addActionListener(e -> {
validaComponentes();
});
}
private void setMessage(List<JLabel> listLabel, String message) {
for (JLabel labels : listLabel) {
labels.setFont(fonte);
labels.setForeground(new Color(255, 21, 8));
labels.setText(message);
}
pack();
}
private boolean validaComponentes() {
java.lang.reflect.Field[] allFields = this.getClass().getDeclaredFields();
for (java.lang.reflect.Field field : allFields) {
if (Modifier.isPrivate(field.getModifiers())) {
try {
field.setAccessible(true);
Object fieldValue = field.get(this);
if (fieldValue instanceof MeuJTextField) {
if (((MeuJTextField) fieldValue).isEmpty()) {
setMessage(listLabel, "O " + ((MeuJTextField) fieldValue).getNome() + " não pode ser vazio !");
return false;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
return true;
}
class MeuJTextField extends JTextField implements MeuComponente {
private boolean eObrigatorio;
private String nome;
public MeuJTextField(boolean eObrigatorio, String nome) {
this.eObrigatorio = eObrigatorio;
this.nome = nome;
setPreferredSize(new Dimension(200, 25));
}
@Override
public String getNome() {
return nome;
}
public boolean isEmpty() {
return getText().trim().isEmpty();
}
@Override
public boolean eObrigatorio() {
return eObrigatorio;
}
}
public interface MeuComponente {
public String getNome();
public boolean eObrigatorio();
}
}