Peter F
Peter F

Reputation: 435

Swing gui not hiding/showing properly

I am writing this gui in java and it works great, except I recently discovered a bug. Currently I access each of my screens through file menus and use the below method to switch back and forth between the panels I am looking at. I have also included the method which one of the file menu actionlisteners executes. For the sake of brevity I have not included the others, just know that they use the same types of commands in a very similar order.

The problem is that sometimes when clicking between the screens, elements of the previous panel will be still visible on the new panel. This new panel will usually be missing most or all of its elements as well. Another (and probably related) issue I am experiencing is that when I run the code half or more of the time I see my initial screen but after that when I click to a new screen nothing shows up at all. It is very confusing because I don't change anything with the code or even recompile between runs and it behaves differently. This second problem has only been occurring since I moved the add methods for the panels to the setCurrentPanel for simplicity's sake.

private void setCurrentPanel(JPanel current) {

    System.out.println(oldCurrent.getName() + " " + current.getName());
    if (oldCurrent.getName().equals(current.getName())) {

    } else {
        buildingPanel.setVisible(false);
        securityPanel.setVisible(false);
        adminUsersPanel.setVisible(false);
        adminBuildingPanel.setVisible(false);
        adminServerPanel.setVisible(false);
        changePasswordPanel.setVisible(false);
        serverSettingsPanel.setVisible(false);
        addBuildingPanel.setVisible(false);
        addUser.setVisible(false);
        for (BuildingItem item : buildingMenuItem) {
            item.panel.setVisible(false);
        }
        add(current);
        current.setVisible(true);
        revalidate();
        repaint();
        oldCurrent = current;
        refreshCount = refreshCount + 1;
        System.out.println("Refresh " + refreshCount);
    }
}

private void setupAdminServerPanel() {

    getServerSettingsFromSQL();
    serverSettingsPanel = new JPanel(new GridBagLayout());
    serverSettingsPanel.setName("Server Settings Panel");
    GridBagConstraints gbr = new GridBagConstraints();
    SpinnerModel minPasswordModel = new SpinnerNumberModel(Integer.parseInt(settingMinPassword), 5, 20, 1);
    SpinnerModel minUsernameModel = new SpinnerNumberModel(Integer.parseInt(settingMinUsername), 5, 20, 1);
    final JSpinner minPasswordSpinner = new JSpinner(minPasswordModel);
    final JSpinner minUsernameSpinner = new JSpinner(minUsernameModel);
    JButton lockdownButton = new JButton("Lockdown");
    lockdownButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            lockdownMode();
        }
    });
    JButton ApplyButton = new JButton("Apply");
    ApplyButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            if (minPasswordSpinner.getValue() != Integer.parseInt(settingMinPassword) ||
                (minUsernameSpinner.getValue() != Integer.parseInt(settingMinUsername))) {
                writeSettingsToSQL((Integer) minPasswordSpinner.getValue(),
                                   (Integer) minUsernameSpinner.getValue());
                getServerSettingsFromSQL();
            }
        }
    });
    lockdownButton.setPreferredSize(new Dimension(50, 50));
    minUsernameSpinner.setPreferredSize(new Dimension(150, 30));
    minPasswordSpinner.setPreferredSize(new Dimension(150, 30));
    gbr.gridy = 0;
    gbr.gridx = 1;
    gbr.gridwidth = 1;
    gbr.ipady = 0;
    gbr.insets = new Insets(10, 10, 10, 10);
    serverSettingsPanel.add(lockdownButton, gbr);
    gbr.gridy = 1;
    gbr.gridx = 0;
    serverSettingsPanel.add(new JLabel("Minimum Username Length"), gbr);
    gbr.gridy = 2;
    gbr.gridx = 0;
    serverSettingsPanel.add(new JLabel("Minimum Password Length"), gbr);
    gbr.gridy = 1;
    gbr.gridx = 1;
    gbr.gridwidth = 2;
    serverSettingsPanel.add(minUsernameSpinner, gbr);
    gbr.gridy = 2;
    serverSettingsPanel.add(minPasswordSpinner, gbr);
    gbr.gridy = 3;
    gbr.gridx = 1;
    gbr.gridwidth = 1;
    serverSettingsPanel.add(ApplyButton, gbr);

    setCurrentPanel(serverSettingsPanel);
}

If you need to see more of the code let me know. I really would appreciate any help I can get! Thanks

Upvotes: 0

Views: 47

Answers (1)

ThoFin
ThoFin

Reputation: 1510

I am not allowed to comment, so maybe it is not a full answer:

You revalidate and repaint the panel, do you do it to the frame or panel, in which it is displayed?

As already suggested, Card Layout is a better way.

Upvotes: 1

Related Questions