Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6833003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:55:38+00:00 2026-05-26T22:55:38+00:00

This will be a really theoretical question, just bear with me a bit. I

  • 0

This will be a really theoretical question, just bear with me a bit. I need to do something with my JButtons, and I have no idea where to start.

So what I need is to be able to click on a JButton, and have a visual proof that it is selected, for example a red border or white background or something. And I want it to stay like that until another JButton is selected in the same way. Right now, when I click on a Jbutton there is a short visual display that it was clicked, but I can’t make it lasts longer.

I tried to play a bit with ChangeListeners, but no results.

So my question is basically: what kind of approach would you advise me to try?

1 – go back to ChangeListener, it’s the only option
2 – JButton has another option that does exactly that

Sorry if it’s too vague, but everything else I found was super specific, and didn’t answer my questions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:55:38+00:00Added an answer on May 26, 2026 at 10:55 pm

    I wonder if you want to use a JToggleButton, perhaps one added to a ButtonGroup so that only one button is selected at a time.

    edit
    for example:

    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class ToggleArray extends JPanel {
       private static final int SIDE = 5;
    
       public ToggleArray() {
          ActionListener listener = new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                System.out.println("Button selected: " + e.getActionCommand());
             }
          };
    
          setLayout(new GridLayout(SIDE, SIDE));
          ButtonGroup btnGroup = new ButtonGroup();
          for (int i = 0; i < SIDE * SIDE; i++) {
             String text = String.format("[%d, %d]", i % SIDE, i / SIDE);
             JToggleButton btn = new JToggleButton(text);
             btn.addActionListener(listener);
             btnGroup.add(btn);
             add(btn);
          }
       }
    
       private static void createAndShowGui() {
          ToggleArray mainPanel = new ToggleArray();
    
          JFrame frame = new JFrame("ToggleArray");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    

    which would look like:
    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this one is really easy. I'm trying to create a Regular Expression that will
This will hopefully be an easy one. I have an F# project (latest F#
This will be probable quite odd question. But i thought I will give it
This will be a bit subjective, I'm afraid, but I'd value the advice of
This will probably be a bot of a waffly question but ill try my
This is probably a noob question that I will get slated for but here
A Purely theoretical question on Tcl. Following this question I was thinking on what
I know this will really turn out to be simple, but my brain is
I have this query for a DB2 DB where I need to pull data
I was asked this interview question today!! (it was a really awkward telephonic interview..):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.