tags:

views:

65

answers:

1

Hi all,

I am new to java swing .I have pasted my code below for your reference,

I am trying to create 2 JRradioButtons in JFrame and if i click that JRadioButton it should display 5 JCheckboxes for each JRadioButton in the same JFrame.

JRadiobutton is displaying now but if i click that JRadioButton "JCheckboxes" is not displaying. please see my code below, if any changes need in my code, please do accordingly.i am struggling for this.

MultipleFramesExample.java calling createMainView() in Mainview.java class

public class MultipleFramesExample extends JFrame {

 public  void fun()
 {

  MainView MV = new MainView();
  MV.createMainView();
 }

 public static void main(String[] args) {
   MultipleFramesExample ob=new MultipleFramesExample();
   ob.fun();
 }
}

Mainview.java creates Jframe and Buttons etc..

    public class MainView extends JFrame implements ActionListener  {

     JFrame frame1;
     MainView mV=null;
     JCheckBox chinButton;
     JRadioButton birdButton;
    MultipleFramesExample ob=new MultipleFramesExample();
  JPanel panel = new JPanel(new BorderLayout());

  public void createMainView() {
  mV = new MainView();

  frame1 = new JFrame();
  frame1.setTitle("Main View");
  frame1.setSize(50,50);
  frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame1.setVisible(true);
   birdButton = new JRadioButton("click");
  frame1.getContentPane().add(birdButton);
  birdButton.addActionListener(this);

 }

  public void actionPerformed(ActionEvent event) {
    Container contentPane = frame1.getContentPane();
   contentPane.setLayout(new FlowLayout());

    JCheckBox jb=new JCheckBox();
    if (event.getActionCommand().equals(birdButton)) {

   frame1.add(new JCheckBox("JIL1"));
   frame1.add(new JCheckBox("JIL2"));
   frame1.add(new JCheckBox("JIL3"));
   frame1.add(new JCheckBox("JIL4"));
   frame1.add(new JCheckBox("JIL5"));
   frame1.setVisible(true);

   //panel.add(jb, BorderLayout.PAGE_START);

  // panel.getComponentCount();
     }
   }
 public void fun1(){

 }
}

Is it possible to create like this?

+3  A: 

Update: Here's a working example:

Button 1 Button 2

public class MainView extends JFrame implements ActionListener{
    JRadioButton radioButton1 = new JRadioButton("Button 1");
    JRadioButton radioButton2 = new JRadioButton("Button 2");
    JCheckBox checkBox = new JCheckBox("CheckBox");
    ButtonGroup buttonGroup = new ButtonGroup();

    public MainView() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(300, 100);
        setLayout(new GridLayout());

        buttonGroup.add(radioButton1);
        buttonGroup.add(radioButton2);
        radioButton1.addActionListener(this);
        radioButton2.addActionListener(this);
        radioButton2.setSelected(true);  // remove to have no button selected

        // ActionListener for CheckBox
        checkBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO: Your action here
            }
        });

        getContentPane().add(radioButton1);
        getContentPane().add(radioButton2);
        getContentPane().add(checkBox);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        checkBox.setVisible(radioButton2.isSelected());
    }

    public static void main(String[] args) {
        new MainView();
    }
}

Original post

You have some problems with your code:

  1. You create new JCheckBoxes whenever the button is clicked. If the user clicks it twice, more checkboxes would be created.
  2. You try to add two checkboxes:
    frame1.add(new JCheckBox("JIL"));
    frame1.getContentPane().add(jb);

Try the following steps:

  1. Create and add all objects that you need for testing (JRadioButton, JCheckBox) and make sure that they are both displayed (check Using Layout Managers and A Visual Guide to Layout Managers if you add both but do not see both).

  2. In your ActionListener, use something like checkBox.setVisible(radioButton.isSelected()) to switch visibility of your checkBox according to the state of your radioButton.

Peter Lang
@ Peter.. could you please edit the above code.. i am totally confused on this..
Manu
@ Peter.. i tried as you told but i think i am slipping from the right direction
Manu
@Manu: Please see my updated answer, hope that helps.
Peter Lang
@ Peter .. by default the button2 is set selected.but i want both button should be unchecked by default.user has to select once window is popup
Manu
i try to set "radioButton2.setSelected(false)" but its not working.. any idea?
Manu
@Peter..and one more question, if want to perform some action on clicking the above checkbox what to do?
Manu
@Manu: (1) Remove the line where I call `radioButton2.setSelected(true);` (see new comment). (2) Add another `ActionListener` (see new example).
Peter Lang
@Peter.. i removed that line already but now check box is coming by default.. my expectation is, both the radio button should be uncheck by default ,and if check anyone of it should dispaly 5 checkboxes or if i check both radiobutton it should diplay 10 checkboxs, and incase if i check anyone of checkboxs or all some action have to perform for the corresponding checkbox. Thanks for all your effort. and please assist for above one also
Manu
@Manu: RadioButtons are used if the user has some options and decides for **one** of them. Not none, not more than one. What you want are CheckBoxes, where the user can decide for each one to check or not. Don't forget to remove my code about `buttonGroup`, as it makes sure that only one option can be selected at a time.
Peter Lang
@Peter.. after few changes in code.. its working fine.. Thanks for all your effort so far.
Manu