Search code examples
javajscrollpanejtextareajoptionpanejcheckbox

JCheckBox State Remain Consistent Among Classes


I have a help pane which appears at the start of a program, but can be turned off. If the user wants it to return, there is an option in the menu bar to reactivate it. However, when they choose to show it from the help menu, it automatically rechecks the "do not show again" box. How do I keep the box in the same state the user originally had it, but still open the help pane?

Gui:

public class Gui {
    private Game game;
    private JFrame frame;
    private MenuBar menuBar;

    private HelpDialog helpMenu;
    private boolean showHelp;

    public Gui(Game game) {
        this.game = game;
        this.showHelp = true;
        this.createAndShowGUI();
    }

    public boolean shouldShowHelpDialog() {
        return this.showHelp;
    }

    public void displayHelp() {
        this.helpMenu.showHelpDialog();
    }

MenuBar:

public class MenuBar {
    private JMenuBar menuBar;
    private JMenu menu;
    private JMenuItem menuItem;
    private JFrame frame;
    private Gui gui;
    private Game game;

    public MenuBar(JFrame frame, Gui gui, Game game) {
        this.menuBar = new JMenuBar();
        this.frame = frame;
        this.gui = gui;
        this.game = game;
    }

    public void buildMenuBar() {
        this.buildFileMenu();
        this.buildSettingsMenu();
        this.buildHelpMenu();

        this.frame.setJMenuBar(this.menuBar);
    }

    private void buildHelpMenu() {
        this.menu = new JMenu("Information");
        this.menu.setMnemonic(KeyEvent.VK_I);
        this.menu.getAccessibleContext().setAccessibleDescription("Help menu");

        JMenuItem menuHelp = new JMenuItem("Help", KeyEvent.VK_H);
        menuHelp.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                MenuBar.this.gui.displayHelp();
            }
        });
        this.menu.add(menuHelp);

        this.menuBar.add(this.menu);
    }

HelpDialog:

public class HelpDialog {

    private boolean shouldShowHelpDialog;
    private JFrame theFrame;

    public HelpDialog(boolean helpDialog, JFrame frame) {
        this.shouldShowHelpDialog = helpDialog;
        this.theFrame = frame;
    }

    public boolean showHelpDialog() {
        if (!this.shouldShowHelpDialog) {
            return false;
        }

        JCheckBox shouldShowCheckBox = new JCheckBox("Do not show this message again", this.shouldShowHelpDialog);

        Object[] msgContent = { this.buildHelpPane(), shouldShowCheckBox };

        JOptionPane.showMessageDialog(this.theFrame, msgContent, "Help", JOptionPane.INFORMATION_MESSAGE);

        return shouldShowCheckBox.isSelected();
    }

    private Object buildHelpPane() {
        String helpMessage = "Game rules: This is how you play.";

        JTextArea helpTextArea = new JTextArea(helpMessage);
        helpTextArea.setRows(6);
        helpTextArea.setColumns(40);
        helpTextArea.setLineWrap(true);
        helpTextArea.setWrapStyleWord(true);
        helpTextArea.setEditable(false);
        helpTextArea.setOpaque(false);

        JScrollPane helpPane = new JScrollPane(helpTextArea);
        return helpPane;
    }
}

EDIT:

Updated HelpDialog class:

    public class HelpDialog {
        private boolean shouldShowHelpDialog;
        private JFrame theFrame;
        private JCheckBox shouldShowCheckBox;

        public HelpDialog(boolean helpDialog, JFrame frame) {
            this.shouldShowHelpDialog = helpDialog;
            this.theFrame = frame;


            this.shouldShowCheckBox = new JCheckBox("Do not show this message again", this.shouldShowHelpDialog);
        }

        public boolean showHelpDialog() {
            if (!this.shouldShowHelpDialog) {
                return false;
            }

            Object[] msgContent = { this.buildHelpPane(), shouldShowCheckBox };

            JOptionPane.showMessageDialog(this.theFrame, msgContent, "Help", JOptionPane.INFORMATION_MESSAGE);

            return shouldShowCheckBox.isSelected();
        }

The checkbox remains unmarked now when displaying the help menu through the menu bar. However, now when a new game is created, it will show the help dialog even if the box is unchecked.

Full answer includes this change to the method in the GUI:

public void displayHelp() {
    this.showHelp = this.helpMenu.showHelpDialog();
}

Solution

  • Your showHelpDialog() method creates a new checkbox each time it is called. You should create the dialog once in the constructor, and showHelpDialog() should just display it.