DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • NullPointerException in Java: Causes and Ways to Avoid It
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • Legacy Code Refactoring: Tips, Steps, and Best Practices
  • Two Cool Java Frameworks You Probably Don’t Need

Trending

  • Docker Base Images Demystified: A Practical Guide
  • FIPS 140-3: The Security Standard That Protects Our Federal Data
  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  1. DZone
  2. Coding
  3. Java
  4. Unselect all Toggle Buttons of a Group

Unselect all Toggle Buttons of a Group

By 
Nicolas Fränkel user avatar
Nicolas Fränkel
DZone Core CORE ·
Aug. 10, 10 · Interview
Likes (0)
Comment
Save
Tweet
Share
29.6K Views

Join the DZone community and get the full member experience.

Join For Free

It’s summer and since I’m hearing more and more the sound of waves splashing instead of the sound of keyboards typing, I’m providing you with quick tricks instead of well-thought deep articles.

This time, it’s about Swing. I’m doing Swing for a pet project… a lot. And I’m more of a web guy. So, I’m always butting my head against the wall trying to overcome problem people more experienced in Swing wouldn’t find a problem at all. This is good, since it teaches me another way too think (I as a web developer am not so much event-based, but it is coming to a change with frameworks such as Vaadin or standards like CDI).

This said, my latest problem was the following. Most of the time, the toggle buttons are radio buttons: you programmatically select one radio before screen display and since all radios are in the same ButtonGroup, when the user selects one, it deselects the one previously selected. This is good but what if my default state would be that no radio be selected?

In fact, to always select a radio means the default state should be displayed as a radio like the others. In my project, I want the default state to be no radio selected, but keep the classical behaviour where selecting a radio means deselecting the one before. Using Swing default classes, it doesn’t work, even if you replace the radio buttons by toggle buttons: when one is selected, it stays selected. Looking at the ButtonGroup code, one understands why:

public void setSelected(ButtonModel m, boolean b) {
if (b && m != null && m != selection) {
ButtonModel oldSelection = selection;
selection = m;
if (oldSelection != null) {
oldSelection.setSelected(false);
}
m.setSelected(true);
}
}

The classical ButtonGroup only manages when the state goes from unselected to selected! This is cool when using radio buttons, not so with toggle buttons.

In order to potentially have no selected buttons in your group, use the following class instead of ButtonGroup:

public class NoneSelectedButtonGroup extends ButtonGroup {

@Override
public void setSelected(ButtonModel model, boolean selected) {

if (selected) {

super.setSelected(model, selected);

} else {

clearSelection();
}
}
}

Note: clearSelection() is available only since Java 6

From http://blog.frankel.ch/unselect-all-toggle-buttons-of-a-group

Web developer IT Framework Java (programming language) Typing dev Vaadin

Opinions expressed by DZone contributors are their own.

Related

  • NullPointerException in Java: Causes and Ways to Avoid It
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • Legacy Code Refactoring: Tips, Steps, and Best Practices
  • Two Cool Java Frameworks You Probably Don’t Need

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!