Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Java Swing Checkbox Issues

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all I'm new to the forums, and have a small issue I'm hoping to get help with. I'm basically just trying to add checkboxes in java swing and have only one of them able to be checked at a time. this is what I've got so far, but it doesn't seem to be working. any help is appreciated. Sample code below.

 
Saloon Keeper
Posts: 24557
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Elijah!

That's really not a good use for checkboxes. Checkboxes are for multiple-choice selections.

A better option would be RadionButtons. Or, if you prefer, a dropdown menu.
 
Elijah Tully
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok well, how do I make it work this way, but with radio buttons then? because I already tried those to
 
Tim Holloway
Saloon Keeper
Posts: 24557
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Radio buttons will automatically switch off when another button is switched on.

The trick is that you have to organize them in a group. That way you can have multiple groups of radio buttons and they don't interfere with each other.

I forget what that grouping class is in Swing, so you'll have to look it up, but it should be there.

Another thing to keep in mind is that unlike checkboxes, which have a boolean value for each checkbox, a radio button group has only one value for the entire group, and that value will identify the button that's "ON".
 
Elijah Tully
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, that makes sense to me. I'll have to look into it more. Thanks for pointing me in the right direction
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you want is a ButtonGroup. Adding multiple radio buttons to a ButtonGroup instance will thus allow only one of those buttons to be selectable at a time.
Note that it is possible to have none of the buttons selected initially.
 
Marshal
Posts: 74374
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More details in the Java™ Tutorials.

I think this discussion would fit better in our GUIs forum: moving.
 
Author
Posts: 974
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW you can add JCheckBoxes to a ButtonGroup if you want, to answer the original question. But I agree that from a UI perspective JRadioButtons make more sense.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic