1

There are two buttons in my ui which re-directs to two JFrames. I am trying to make that if user press button one, button two becomes disabled, and if the user presses button two, button one becomes disabled, so that the user cannot open both the JFrames at the same time.

import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;


 public class Main extends JFrame {

 public Main() {

JPanel panel = new JPanel();
getContentPane().add (panel,BorderLayout.NORTH);

JButton button1 = new JButton("One"); 
panel.add(button1);
JButton button2 = new JButton("Two");
panel.add(button2);

button1.addActionListener (new ActionListener()
  {
    public void actionPerformed (ActionEvent e)
      {
        button2.setEnabled(false);
        One f = new One();
        f.setSize(350,100);
        f.setVisible(true);
      }
   });  

button2.addActionListener (new ActionListener()
   {
    public void actionPerformed (ActionEvent e)
      {
            button1.setEnabled(false);
            Two fr = new Two();
            fr.setSize(350,100);
            fr.setVisible(true);
      }
    });

 public void enableButtons()
 {
    button1.setEnabled(true);
    button2.setEnabled(true);
 }

}

public static void main(String[] args) {

Main frame = new Main();
frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    enableButtons();
        System.exit(0);
  }
});
frame.setSize(300,200);
frame.setLocationByPlatform(true);
frame.setVisible(true);
  }
}
Rudy dev
  • 47
  • 7
  • 1
    You really shouldn't be using multiple frames (check out [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice)). Instead, use one frame and switch between panels using `CardLayout`. Not only is it easier for the user to use, but you won't need to worry about them pressing both buttons. To make a button non-clickable, call `button.setEnable(false)` – Vince Oct 14 '14 at 17:11
  • Since you are trying to implement a selection UI with only one option out of two being selected, you might want to use [`JToggleButton`](http://docs.oracle.com/javase/8/docs/api/javax/swing/JToggleButton.html) or [`JRadioButton`](http://docs.oracle.com/javase/8/docs/api/javax/swing/JRadioButton.html). – user1803551 Oct 14 '14 at 17:41

2 Answers2

1

In your ActionListener for Button One, add

ButtonTwo.setEnabled(false);

and in the ActionListenerfor Button Two add

ButtonOne.setEnabled(false);

Don't forget to add the corresponding enables (button.setEnable(true)) otherwise you will be left with two disabled buttons. Maybe in an event for closing the JFrames.

EDIT:

You can write a method like this

public void enableButtons()
{
   button1.setEnabled(true);
   button2.setEnabled(true);
}

Call this method in an event of JFrame closing. This tutorial explains the JFrame closing event.

nbz
  • 3,806
  • 3
  • 28
  • 56
  • I have tried if i press on button1,button2 remains disabled unless we close the application and vice versa. – Rudy dev Oct 15 '14 at 07:44
  • That's why it remains disabled till the end. Think about it logically, when would you like the buttons enabled again? If I understand your program correctly, you would want the buttons enabled once the user closes the JFrame. – nbz Oct 15 '14 at 09:10
  • I have tried as you said @nbz but it does not works. – Rudy dev Oct 20 '14 at 09:41
  • Yes, that will be helpful. – nbz Oct 21 '14 at 10:13
  • Your windowClose event seems to be hooked to your Main object. You want the event to be hooked to your two JFrame closing events. – nbz Oct 24 '14 at 09:14
  • I have tried @nbz but it shows error.can u please show me how to do it – Rudy dev Nov 02 '14 at 12:54
  • Please can you show what you tried and what error you are getting – nbz Nov 03 '14 at 12:52
  • I have tried @nbz closing event for the two JFrames but it shows error illegal start of expression public void enableButtons(). – Rudy dev Nov 11 '14 at 03:53
0

Check out the ButtonGroup for a more elegant solution.

http://docs.oracle.com/javase/tutorial/uiswing/components/buttongroup.html

David Gidony
  • 1,243
  • 3
  • 16
  • 31