Can anybody tell me why I'm not getting checkboxes? Sometimes I can get one, but never the three I need.

public class SwingLab 
    implements ActionListener
    
{
    // instance variables - this instance will get reader's name
    private Scanner reader;//name from user
    private Random random;
    private ArrayList<String> name;//construct a responder, declare a field type ArrayList
    private String userName;//stores userName for later use.
    private JFrame frame;
    private String firstCheck;
    private String secondCheck;
    private String thirdCheck;

    /**
     * Constructor for objects of class Calculator
     * Create a responder. 
     * Create the Random and ArrayList object.
     * Create the Scanner input object.
     */
    public SwingLab()
    {
       reader = new Scanner(System.in); 
       name = new ArrayList<String>();
       makeFrame();
       makeCheckBox();
    }
    
    /**
     * Saves userName for later use.
     */
    private String userName()
    {
       
        userName = reader.nextLine();
        return userName;
    }
    
    public void actionPerformed(ActionEvent event)
    {
        System.out.println("Item: " + event.getActionCommand());
    }
    private void start()
    {

    }
   
    /**
     * Create the Swing frame and it's content.
     */
    private void makeFrame()
    {
        
        frame = new JFrame("SwingLab");
        makeMenuBar(frame);
        
        Container contentPane = frame.getContentPane();
        
        System.out.println("What is your name?");
        userName=reader.nextLine();
        System.out.println("Please enter your first check box label");        
        firstCheck=reader.nextLine();        
        System.out.println("You entered " + firstCheck + ". Please enter your second one.");
        secondCheck=reader.nextLine();
        System.out.println("You entered " + secondCheck + ". Please enter your third one.");
        thirdCheck=reader.nextLine();
        System.out.println("You entered " + thirdCheck + " for your final entry. Your check boxes will now be created.");
        
        JLabel label = new JLabel("Welcome to my first GUI, " + userName + ". It's pretty, but doesn't really work yet!");
        contentPane.add(label);
        
        frame.pack();
        frame.setVisible(true);
        
        
        
    }
    private void makeCheckBox()
    {
        //this is to create the first checkbox the user created
        JCheckBox firstCheck = new JCheckBox(this.firstCheck);
        frame.add(firstCheck);
        frame.setSize(400, 400);
        frame.setVisible(true);
    
        JCheckBox secondCheck = new JCheckBox(this.secondCheck);
        frame.add(secondCheck);
        frame.setSize(600, 600);
        frame.setVisible(true);
    
        
        JCheckBox thirdCheck = new JCheckBox(this.thirdCheck);
        frame.add(thirdCheck);
        frame.setSize(700, 700);
        frame.setVisible(true);
        
        JPanel checkPanel = new JPanel (new GridLayout(0, 1));
        checkPanel.add(firstCheck);
        checkPanel.add(secondCheck);
        checkPanel.add(thirdCheck);
    }
    
    /**
     * Create main frame's menu bar.
     */
    private void makeMenuBar(JFrame frame)
    {
        JMenuBar menubar = new JMenuBar();
        frame.setJMenuBar(menubar);
        //create File menu
        JMenu fileMenu = new JMenu("File");
        menubar.add(fileMenu);
        //create Open menu item.
        JMenuItem openItem = new JMenuItem("Open");
        openItem.addActionListener(this);
        fileMenu.add(openItem);
        //create Quit menu item.
        JMenuItem quitItem = new JMenuItem("Quit");
        quitItem.addActionListener(this);
        fileMenu.add(quitItem);
    }
    
    }

Recommended Answers

All 10 Replies

You should construct all the boxes, add them to the "checkPanel", add "checkPanel" to the frame and then set it visible.

I don't know how to do that.

You are already doing most of those things. You just aren't doing them in the correct order as I outlined above.

Okay, I tried rearranging and adding, but now it's doing all kinds of crazy things, like only two (the first and the third) checkboxes appear but if the mouse rolls over them, they disappear. Here's the new code:

public class SwingLab 
    implements ActionListener
    
{
    // instance variables - this instance will get reader's name
    private Scanner reader;//name from user
    private Random random;
    private ArrayList<String> name;//construct a responder, declare a field type ArrayList
    private String userName;//stores userName for later use.
    private JFrame frame;
    private String firstCheck;
    private String secondCheck;
    private String thirdCheck;

    /**
     * Constructor for objects of class Calculator
     * Create a responder. 
     * Create the Random and ArrayList object.
     * Create the Scanner input object.
     */
    public SwingLab()
    {
       reader = new Scanner(System.in); 
       name = new ArrayList<String>();
       makeFrame();
       makeCheckBox();

    }
    
    /**
     * Saves userName for later use.
     */
    private String userName()
    {
       
        userName = reader.nextLine();
        return userName;
    }
    
    public void actionPerformed(ActionEvent event)
    {
        System.out.println("Item: " + event.getActionCommand());
    }
    private void start()
    {

    }
    private void makeCheckBox()
    {
        //this is to create the first checkbox the user created
        JCheckBox firstCheck = new JCheckBox(this.firstCheck);
        frame.add(firstCheck);
        frame.setSize(400, 400);
        frame.setVisible(true);
    
        JCheckBox secondCheck = new JCheckBox(this.secondCheck);
        frame.add(secondCheck);
        frame.setSize(400, 400);
        frame.setVisible(true);
    
        
        JCheckBox thirdCheck = new JCheckBox(this.thirdCheck);
        frame.add(thirdCheck);
        frame.setSize(400, 400);
        frame.setVisible(true);
        
        
    }
   
    /**
     * Create the Swing frame and it's content.
     */
    private void makeFrame()
    {
        
        frame = new JFrame("SwingLab");
        makeMenuBar(frame);
        
        Container contentPane = frame.getContentPane();
        
        System.out.println("What is your name?");
        userName=reader.nextLine();
        System.out.println("Please enter your first check box label");        
        firstCheck=reader.nextLine();        
        System.out.println("You entered " + firstCheck + ". Please enter your second one.");
        secondCheck=reader.nextLine();
        System.out.println("You entered " + secondCheck + ". Please enter your third one.");
        thirdCheck=reader.nextLine();
        System.out.println("You entered " + thirdCheck + " for your final entry. Your check boxes will now be created.");
        
        JLabel label = new JLabel("Welcome to my first GUI, " + userName + ". It's pretty, but doesn't really work yet!");
        contentPane.add(label);
        
        frame.pack();
        frame.setVisible(true);
    }

    /**
     * Create main frame's menu bar.
     */
    private void makeMenuBar(JFrame frame)
    {
        JMenuBar menubar = new JMenuBar();
        frame.setJMenuBar(menubar);
        //create File menu
        JMenu fileMenu = new JMenu("File");
        menubar.add(fileMenu);
        //create Open menu item.
        JMenuItem openItem = new JMenuItem("Open");
        openItem.addActionListener(this);
        fileMenu.add(openItem);
        //create Quit menu item.
        JMenuItem quitItem = new JMenuItem("Quit");
        quitItem.addActionListener(this);
        fileMenu.add(quitItem);
    }
    
    }

You didn't really rearrange - just just dropped the JPanel portion. Re-read what I posted and try to actually perform those steps.
Make checkbox >> make panel >> put panel in frame.

I'm sorry, Ezzaral. I am trying. I'm new at this and I've been working on this program since 7am this morning. I just simply do not understand.

Your first version of makeCheckBox was pretty close. You want to add the checkboxes to 'checkPanel' and then add 'checkPanel' to the frame. So for a single check box

JCheckBox firstCheck = new JCheckBox(this.firstCheck);

        JPanel checkPanel = new JPanel (new GridLayout(0, 1));
        checkPanel.add(firstCheck);

        frame.add(checkPanel);
        frame.pack();

Thank you, Ezzaral. I got it working. I even added my ActionListeners without an issue. My Welcome line is still disappearing no matter where I put it in my code, with the exception of the bottom, so I think I'll cheat a little, and have it say thank you instead so I can keep it there! It's been 12 hours...

Again, I can't thank you enough. You've brought a long day to an end.

Heather

If you feel like changing the layout of your frame, you can clear up the disappearing message issue. You could use that GridLayout(0,1) for the frame content pane and add the checkboxes directly to the frame instead of the panel. It all lines up nicely that way.

I'm trying, but I think it's a hopeless case at this point. Thank you for the thought though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.