2

I'm new to Java and following an online tutorial to learn swing. I have just gotten to a part where we are adding in JCheckBox and I can't figure out why they are not showing up when I run the program. I'm running it on Eclipse on a Mac if that makes a difference.

I've tried re-writing everything but that didn't work. I know that they are added but they are just not showing up. The JRadioButtons are showing up just fine.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Userimputcomplex 
{
    public static void main(String[] args) 
    {
         RadioDemo r = new RadioDemo();
    }
}

class RadioDemo extends JFrame
{
    JTextField t1; //Input Text
    JButton b; //Button
    JRadioButton r1,r2; //Check buttons
    JLabel l; //Text
    JCheckBox c1,c2; //Multiple check buttons

    public RadioDemo()
    {
        t1 = new JTextField(15);
        b = new JButton("Ok");
        r1 = new JRadioButton("Male");
        r2 = new JRadioButton("Female");
        l = new JLabel("Greetings");
        c1 = new JCheckBox("Dancing");
        c2 = new JCheckBox("Singing");

        ButtonGroup bg = new ButtonGroup(); 
        bg.add(r1);
        bg.add(r2);

        add(t1);

        add(r1);
        add(r2);
        add(c1);
        add(c2);
        add(b);
        add(l);

        b.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
                String name = t1.getText();

                if(r1.isSelected()) 
                {
                    name = "Mr." + name;
                }
                else
                {
                    name = "Ms." + name;
                }
                if(c1.isSelected()) 
                {
                    name = name + " Dancer";
                }
                if(c2.isSelected()) 
                {
                    name = name + " Singer";
                }
                l.setText(name);
            }
        });

        setLayout(new FlowLayout());
        setVisible(true);
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • 2
    1) Can't reproduce your problem: [image](https://i.stack.imgur.com/UR6WB.png), 2) Give your variables meaningful names, `t1`, `b`, `r1` just don't tell us what they are. And makes it hard to follow. 3) Don't extend `JFrame` it's better to [create an instance of it](https://stackoverflow.com/questions/22003802/extends-jframe-vs-creating-it-inside-the-program). I suggest setting the layout to the container **before** adding anything to it. – Frakcool Jun 04 '19 at 15:21
  • move your flowlayout setter to the begining – Mohammed Housseyn Taleb Jun 04 '19 at 15:54
  • 1
    See [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html), and don't forget to `pack()` your `Window`. – trashgod Jun 04 '19 at 16:18
  • 1
    Like @Frakcool, I can also [show a screen shot](https://i.stack.imgur.com/UNcn0.png) of the GUI showing the check boxes. But this image also alludes to why the programmer should not guess the size the GUI needs to be, and why `pack()` should be used instead. – Andrew Thompson Jun 04 '19 at 16:43
  • Thank you for all your help!!! – Troy Medinis Jun 04 '19 at 21:36

0 Answers0