So I have a class which creates the following GUI the first time it the weapon's button is clicked on:
Every button on the side I click on should switch to a different panel accordingly. However, currently, I am just working on the weapons panel. To save on memory I decided to save whatever was typed in the search box and if the user decides to open the panel again the application searches up the list again. Currently, I have only one item called test as you see, my problem is if I type something in the search box, that is not available and I switch panels, this is my result:
The expected result is supposed to be the error message but with the search box, buttons and the JList still available with the JList showing "No Weapons Listed".
Now when I remove the JOptionPane I get the following which is half the result:
I would like to know why when the error message appears the panel is not drawn?
PS: I had entered some console messages, one after the Joptionpane and the other before the return statement and they both get printed correctly
Here is my code for the panel:
private static JPanel searchMenu() {
JPanel pnl = new JPanel();
pnl.setOpaque(false);
pnl.setLayout(new BorderLayout());
Object[] search = search();
if(search.length == 0) {
JOptionPane.showMessageDialog(MyFrame.getFrame(),
"No weapons with your search criteria has been found.\n"
+ "What you type in the search box is searched for in the weapon names, classnames and their description.\n",
"No Weapon found", JOptionPane.ERROR_MESSAGE);
search = new Object[] {"No Weapons listed"};
}
System.out.println(search.length);
DefaultListModel list = new DefaultListModel();
for(Object o: search) list.addElement(o);
JList searched = new JList();
searched.setModel(list);
searched.setPreferredSize(new Dimension(250, 700));
searched.setFont(new Font("Tahoma", Font.PLAIN, 15));
pnl.add(searched, BorderLayout.WEST);
pnl.add(weaponDisplay(lastSelec),BorderLayout.CENTER);
JPanel space = new JPanel();
space.setOpaque(false);
pnl.add(space,BorderLayout.SOUTH);
return pnl;
}
It looks like you're adding components to a container after it's been made visible. At a minimum, you'll need to validate()
the enclosing container, as shown here, and possibly invoke repaint()
. A better approach is to add the view component, e.g. JList
, to the layout and update its model, e.g. ListModel
, as required. In this example, a JList
listens to a nearby table's model.