How to keep a JButton pressed after its JPanel loses focus?

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(); } }); } }.

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(); } }); } }.

I've never seen anything like that before. Could you elaborate (for me)? – fireshadow52 Sep 27 at 0:55 2 It's a private inner class, that's all.

It can only be used inside of the outer class, like an anonymous inner class, but unlike the anonymous variety, it can be used multiple times. – Hovercraft Full Of Eels Sep 27 at 1:00 Thank you for clearing that up! :D – fireshadow52 Sep 27 at 1:22 @fireshadow52: to clarify, an object derived from an anonymous inner class may be used multiple times, but you can only create one object with it while with a private inner class, you can call new MyPrivateInnerClass(...) as many times as needed.

– Hovercraft Full Of Eels Sep 27 at 1:26.

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).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions