0

So I have a class which creates the following GUI the first time it the weapon's button is clicked on: enter image description here

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:

enter image description here

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:

enter image description here

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;
}
BlacKnight BK
  • 119
  • 4
  • 12

2 Answers2

2

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

JOptionPane's dialogs are modal, so they stop execution of the calling code until the dialog is closed. So you'll want to populate and show the list first, with "No weapons listed" as its contents, before showing the message dialog.

BinaryDigit09
  • 110
  • 10
  • once the dialogue is closed I still do not get the panel's components drawn however the println both get printed on the console – BlacKnight BK Sep 27 '17 at 16:11
  • search = new Object[] {"No Weapons listed"}; It isn't populating the list model with the new 'search' array contents. – BinaryDigit09 Sep 27 '17 at 16:53
  • @BinaryDigit09: As suggested, [here](https://stackoverflow.com/a/18728637/230513) modal dialog only blocks user interaction; it doesn't block updates. – trashgod Sep 27 '17 at 20:43
  • @trashgod: Modal dialogs do block execution of the code that shows them. The OP is showing the modal dialog, then updating the 'search' array, which doesn't execute until after the dialog is closed. – BinaryDigit09 Sep 28 '17 at 15:11
  • @BinaryDigit09: Updates posted to the `EventQueue` are still processed; try the experiment suggested [here](https://stackoverflow.com/a/18728637/230513) to see the effect. – trashgod Sep 28 '17 at 21:57
  • @trashgod: You are correct that *Swing events* are still processed, but as I said, *execution* is blocked. Put a print a statement immediately after showing a modal dialog, it won't be printed until after the dialog is closed. – BinaryDigit09 Sep 29 '17 at 23:14
  • @BinaryDigit09: I'm not suggesting that your answer is _wrong_; if I'm not mistaken, getting _the panel's components drawn_ is orthogonal to modality in this case. – trashgod Sep 29 '17 at 23:26