简体   繁体   中英

My Panel's components are not drawn if JOptionPane is drawn

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, eg JList , to the layout and update its model , eg ListModel , as required. In this example , a JList listens to a nearby table's model.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM