Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep a JButton pressed after its JPanel loses focus

I have found how to keep a JButton in its pressed state using this code:

JButton[] buttons;
.
.
.
public void actionPerformed(ActionEvent e)
{

    for(int i = 0; i < buttons.length; i++)
    {
        if(e.getSource() == buttons[i])
        {
            buttons[i].getModel().setPressed(true);
        }
        else
        {
            buttons[i].getModel().setPressed(false);
        }

    }
}

This code captures the clicked button, keeps it pressed, and makes all other buttons on the panel unpressed. And this code works great... until the window loses focus (or more specifically, its parent JPanel loses focus). After that, all the buttons return to a non-pressed state.

Right now the tutorial on how to write WindowFocusListeners is down. Is there a way to make a JButton's pressed state persist through a loss of focus?

like image 498
user966029 Avatar asked Sep 27 '11 00:09

user966029


1 Answers

Why not simply use a series of JToggleButtons and add them to the same ButtonGroup object? All the hard work is done for you since the toggle button is built to stay in pressed state if pressed. Think of it as a JRadioButton that looks like a JButton (since in actuality, JRadioButton descends from JToggleButton).

For example:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class BunchOfButtons extends JPanel {
   private static final String[] TEXTS = {"One", "Two", "Three", "Four", "Five"};
   private ButtonGroup btnGroup = new ButtonGroup();
   private JTextField textField = new JTextField(20);

   public BunchOfButtons() {
      JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 0));
      BtnListener btnListener = new BtnListener();
      for (String text : TEXTS) {
         JToggleButton toggleBtn = new JToggleButton(text);
         toggleBtn.addActionListener(btnListener);
         toggleBtn.setActionCommand(text);
         btnPanel.add(toggleBtn);
         btnGroup.add(toggleBtn);
      }

      JPanel otherPanel = new JPanel();
      otherPanel.add(textField ); // just to take focus elsewhere

      setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      setLayout(new GridLayout(0, 1, 0, 15));
      add(btnPanel);
      add(otherPanel);
   }

   private class BtnListener implements ActionListener {
      public void actionPerformed(ActionEvent aEvt) {
         textField.setText(aEvt.getActionCommand());
      }
   }

   private static void createAndShowGui() {
      BunchOfButtons mainPanel = new BunchOfButtons();

      JFrame frame = new JFrame("BunchOfButtons");
      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();
         }
      });
   }
}
like image 126
Hovercraft Full Of Eels Avatar answered Sep 27 '22 21:09

Hovercraft Full Of Eels