Disabling a jToggleButton throughout entire execution, possible?

297 views Asked by At

Hey guys so I'm making this simple movie ticketing system My program flow is as follow and all pages are in different JFrames: Main Menu>Choose Day>Select movie>select seat>back to MainMenu

I'm using JToggle in the seat chooser. Is it possible to disable the toggle button throughout entire execution once selected? I'm using JToggleButton.setEnabled(false); but everytime I go back to menu and come back to seat chooser,the button is still not disabled .what I want to do is to have it disable even after I'm back to MainMenu, so when I go back to seat chooser, I can't select this seat anymore.

Below are some codes in it:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    this.setVisible(false);
    MainSelection s =  new MainSelection();
    s.setVisible(true);

     if(jToggleButton1.isSelected())

    {
        jToggleButton1.setEnabled(false);
    }

    if(jToggleButton2.isSelected())

    {
        jToggleButton2.setEnabled(false);
    }

    if(jToggleButton3.isSelected())

    {
        jToggleButton3.setEnabled(false);
    }

}                                        

Please check it out

1

There are 1 answers

12
Hovercraft Full Of Eels On

You seem to be re-creating the GUI that displays your toggle buttons each time it is displayed, and you should not be doing this.

Instead

  • create a variable for this window
  • consider creating it in a lazy way -- create it if and only if it is null
  • otherwise if not null and it needs to show, simply make it visible via setVisible(true).
  • and conversely make it invisible when needed via setVisible(false).
  • don't show multiple JFrames in your application. Instead the application should have one main JFrame, and then you can have it launch dialog windows, such as JDialogs, if appropriate, or swap "views" via a CardLayout if appropriate.

Specifically:

  • Make MainSelection variable, s, an instance field of the class -- declare it and initialize it once in the class.
  • Only set it visible in this method. Don't create a new one.
  • In the future, don't spit a bunch of JFrames at the user as it is a terrible and annoying user interface. Instead read the CardLayout tutorial (Google will help you find it), and use it. Gear your code towards creating JPanels, not JFrames.

Edit
You ask:

I really need help Make MainSelection variable, s, an instance field of the class -- declare it and initialize it once in the class. Only set it visible in this method. Don't create a new one. How do I make it instance field?? Also do I declare it at mainselection form or the seatselection form?

You are doing something like:

public class Foo {

  private void someMethod() {
    // the code below creates a new SomeClass instance each time the method is called
    SomeClass localVariable = new SomeClass();
    localVariable.setVisible(true);
  }
}

And I'm recommending that instead you do:

public class Foo {
  // the code below creates a SomeClass instance only *once*.
  private SomeClass instanceField = new SomeClass();

  private void someMethod() {
    instanceField.setVisible(true);
  }
}

Also, you should do something about that duplicate post of yours:

  • First close it -- you should not have more than one of the same question -- it's not fair to us and others.
  • And accept and up-vote the answer in the other post to show appreciation for the effort and helpfulness of the poster's post.