• Post Reply Bookmark Topic Watch Topic
  • New Topic

Button Group  RSS feed

 
Jesse Walker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the Javadocs, I see that you can add JRadioButtons to a ButtonGroup using the method add(). When you want to display the buttons on a JFrame's ContentPane do you use the add method to add each Button in the Button Group or each buttongroup by themselves
ie
ButtonGroup buttons;
JRadiobutton radio;
buttons.add(radio);
Container content = frame.getContentPane();
content.add(buttons);
or
content.add(radio);

I have tried both ways and I can't get the buttons to show up at all.
Any help would be appreciated
Thank you,
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see the word "new" anywhere. Did you create any instances? Or are "radio" and "buttons" just null references?
 
Jesse Walker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in my application I am trying to create I have:
ButtonGroup fulldesk = new ButtonGroup();
JRadioButton[] radio = {new JRadioButton("Partial"), new JRadioButton("Full")};
fulldesk.add(radio[0]);
fulldesk.add(radio[1]);
Container content = frame.getContentPane();
content.add(fulldesk);

should it be
content.add(radio[0]);
content.add(radio[1]);
???
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you packing the JFrame and setting it visible?

frame.pack();
frame.setVisible(true);

Yes, you add the buttons (not the group) to the container. The group takes care of unselecting prior radio button choices when a new selection is made. Note that when adding more than one button, you will need to consider the layout manager. (If I'm not mistaken, the default is BorderLayout; and unless you specificy differently, elements will keep being added to the CENTER, overlaying each other so that all you will see is the last one.)

See the Java Swing Tutorial:
  • How to use buttons...
  • Laying out components

  • Also, since this is a Swing topic, I'm moving this to the Swing forum. Please continue this discussion there. (See links at top of page.)
     
    Brian Cole
    Author
    Ranch Hand
    Posts: 949
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Jesse Walker:
    should it be
    content.add(radio[0]);
    content.add(radio[1]);


    Yes, that's how it should be.

    A ButtonGroup isn't even a Component, so you can't add one to a Container. It is used just to keep the selectedness of the buttons in sync.
    [ January 04, 2007: Message edited by: Brian Cole ]
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!