I've made a GUI using IntelliJ IDEA's form designer, and I have added a JList inside a JScrollPane. The thing is that no matter when or how I add elements to the JList, it doesn't show them. I used the debug tool and I can see that the elements are inside the JList, they just aren't rendered.
I'm currently using a DefaultListModel, but I've tried using Vector and arrays without success. I have also tried using the function updateUI() in the JList, the JScrollPane and the JFrame itself, and the function ensureIndexIsVisible() with the last index of the list in the JList, but nothing.
This form is called from another one and I don't think the code for the main one is needed, so I'll only paste here the code for the faulty form:
import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Enviar extends JFrame {
private JTextField codigoTxt;
private JButton anadirBtn;
private JPanel enviar;
private JLabel errorCodigoLbl;
private JList<String> companerosLBox;
private DefaultListModel<String> listaCompas = new DefaultListModel<>();
private JButton eliminarSelecBtn;
private JButton eliminarTodoBtn;
private JTextField xPosTxt;
private JTextField yPosTxt;
private JLabel errorClickLbl;
private JButton clickBtn;
private JButton atrasBtn;
private JScrollPane scrollPane;
public Enviar() {
setContentPane(enviar);
setTitle("Remote Clicker - Enviar click");
setResizable(false);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pack();
errorCodigoLbl.setVisible(false);
errorClickLbl.setVisible(false);
setVisible(true);
listaCompas.addElement("sdd");
listaCompas.addElement("sd2d");
listaCompas.addElement("sdd3");
companerosLBox = new JList<>(listaCompas);
scrollPane = new JScrollPane(companerosLBox);
anadirBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
anadirCompa(codigoTxt.getText());
}
});
codigoTxt.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
anadirCompa(codigoTxt.getText());
}
}
});
}
private void anadirCompa(String codigo) {
if (valido(codigo)) {
codigoTxt.setText("");
errorCodigoLbl.setVisible(false);
listaCompas.addElement(codigo);
companerosLBox.setModel(listaCompas);
} else {
errorCodigoLbl.setVisible(true);
}
}
private boolean valido(String codigo) {
boolean res = true;
int i = 0;
while (res && i < codigo.length())
{
res = codigo.charAt(i) >= '0' && codigo.charAt(i) <= '9' || codigo.charAt(i) == '-';
i++;
}
return res && codigo.indexOf('-') > 0 && codigo.indexOf('-') < codigo.length()-1;
}
}
What else can I do or what am I doing wrong?
EDIT: I'll also add that if I populate the JList via the form builder itself, the data I add there is shown, but once it's loaded it doesn't change.
Ok, the problem was that IntelliJ IDEA's form designer doesn't work the same way that plain Java does.
The thing is that in this conditions it's not needed to create a new JList, so when I did companerosLBox = new JList<>(listaCompas);
I was unbinding it from the form (I suppose).
So, for the provided code to work, it's just needed to replace
companerosLBox = new JList<>(listaCompas);
with
companerosLBox.setModel(listaCompas);
in the constructor (and for correction, deleting that same line from anadirCompa()
).