Search code examples
javaswingjcheckbox

My JCheckBox program only displays one box. Why is that?


I am attempting to add another checkbox to this program but for some reason it will not display when I run the program. Only the check box for the blue pill displays. I have attempted to add a couple things or change the way the program is structured, but nothing I have done so far has helped.

Code Below:

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    public class CMIS242WK4DonnersonAReply {
    
    static JCheckBox red;
    static JCheckBox blue;
    static JButton button;
    
 
    public CMIS242WK4DonnersonAReply() {
        
        
        button = new JButton("submit"); // Creates submit button 
    widget
        ButtonHandler listener = new ButtonHandler(); // Creates the handler for the button.
        button.addActionListener((ActionListener) listener); // adds the handler to the button widget
        
        
        JPanel content = new JPanel(); // "container"
        content.setLayout(new BorderLayout());
        content.add(button, BorderLayout.PAGE_END);// places submit button at the bottom of panel.
        
        
        JLabel label = new JLabel("At last. Welcome, Neo. As you no doubt have guessed, I am Morpheus.  This is your last chance. After this there is no turning back."); // Label in frame.
        content.add(label, BorderLayout.NORTH);// places label at the top of the screen.

        //Creating Check Boxes
        JCheckBox red = new JCheckBox("You take the red pill, you stay in Wonderland and I show you how deep the rabbit hole goes.");
        red.setBounds(100,100, 50,50);  
        content.add(red);
        
        JCheckBox blue = new JCheckBox("You take the blue pill, the story ends, you wake up in your bed and believe whatever you want to believe. ");
        blue.setBounds(100,100, 50,50);  
        content.add(blue);
        
        //Adding Frame
        JFrame window = new JFrame("Matrix Monologue"); // JFrame = Window
        window.setContentPane(content);
        window.setSize(750,200); // Length, Height
        window.setLocation(200,200); // X/Y "OF THE ENTIRE FRAME" Not the contents
        window.setVisible(true); // makes window visible
       

    }
    
    // Method handles what happens when button is pressed.
    private static class ButtonHandler implements ActionListener{
        public void actionPerformed1(ActionEvent e) {
            
            
            // Checks if which pill was selected and responds to user depending on their action.
            if (red.isSelected() == true) {
                System.out.println("Follow me");
                System.out.println();
            }
            if (blue.isSelected() == true) {
                System.out.println("Very Well, You may go back to your world");
                System.out.println();
            }
            else
                System.out.println("You must make a choice for what pill you will take");
            System.exit(0); //closes program
            
            
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            
        }
    }
    
    
    // Main/driver method that runs everything.
    public static void main(String[] args) {

        CMIS242WK4DonnersonAReply matrixMonologue= new CMIS242WK4DonnersonAReply();
        
    }
}

Any pointers?


Solution

  • I feel that you need some guidance with your Swing programming. I have rewritten your CMIS242WK4DonnersonAReply class. Code is below. But first some comments about the code in your question.

    JCheckBox red = new JCheckBox("You take the red pill, you stay in Wonderland and I show you how deep the rabbit hole goes.");
    

    You have created a local variable which is hiding the class member. Hence static JCheckBox red; remains null and consequently the following if statement will throw NullPointerException.

    if (red.isSelected() == true) {
    

    By the way, the == true is not necessary. The following is sufficient.

    if (red.isSelected()) {
    

    Now another point.

    red.setBounds(100,100, 50,50);
    

    Since you are using a layout manager, namely BorderLayout, method setBounds will be ignored. The layout manager determines where to place the component on the screen.

    window.setContentPane(content);
    

    By default, the content pane of JFrame is a JPanel with BorderLayout so no need to replace the default content pane.

    private static class ButtonHandler implements ActionListener
    

    No need to create a nested class. Simply make class CMIS242WK4DonnersonAReply implement the ActionListener interface.

    System.out.println("Follow me");
    

    I don't think it's a good idea to involve the console in a GUI application. I would use JOptionPane to display a message to the user.

    static JCheckBox blue;
    

    I think that JRadioButton is more appropriate than JCheckBox in your situation.

    Here is my code.

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.BoxLayout;
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    
    public class CMIS242WK4DonnersonAReply implements Runnable, ActionListener {
        private JButton  button;
        private JRadioButton  blue;
        private JRadioButton  red;
        private JFrame  window;
    
        @Override
        public void actionPerformed(ActionEvent event) {
            if (red.isSelected()) {
                JOptionPane.showMessageDialog(window, "Follow me.");
            }
            else if (blue.isSelected()) {
                JOptionPane.showMessageDialog(window, "Very Well, You may go back to your world");
            }
            else {
                JOptionPane.showMessageDialog(window, "You must make a choice for what pill you will take");
            }
        }
    
        @Override
        public void run() {
            createAndShowGui();
        }
    
        private void createAndShowGui() {
            window = new JFrame("Matrix Monologue");
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JLabel label = new JLabel("At last. Welcome, Neo. As you no doubt have guessed, I am Morpheus. This is your last chance. After this there is no turning back."); // Label in frame.
            window.add(label, BorderLayout.PAGE_START);
            window.add(createCheckBoxes(), BorderLayout.CENTER);
            JPanel buttonPanel = new JPanel();
            button = new JButton("submit");
            button.addActionListener(this);
            buttonPanel.add(button);
            window.add(buttonPanel, BorderLayout.PAGE_END);
            window.setSize(750,200); // Length, Height
            window.setLocation(200,200); // X/Y "OF THE ENTIRE FRAME" Not the contents
            window.setVisible(true); // makes window visible
        }
    
        private JPanel createCheckBoxes() {
            JPanel panel = new JPanel();
            BoxLayout layout = new BoxLayout(panel, BoxLayout.PAGE_AXIS);
            panel.setLayout(layout);
            red = new JRadioButton("You take the red pill, you stay in Wonderland and I show you how deep the rabbit hole goes.");
            blue = new JRadioButton("You take the blue pill, the story ends, you wake up in your bed and believe whatever you want to believe.");
            ButtonGroup grp = new ButtonGroup();
            grp.add(red);
            grp.add(blue);
            panel.add(red);
            panel.add(blue);
            return panel;
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new CMIS242WK4DonnersonAReply());
        }
    }
    

    Here is how the app looks when I run it.

    screen capture of running app