-2

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

user3144549
  • 13
  • 1
  • 1
  • 6

1 Answers1

2

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.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • sorry I don't really understand what is being said cause I just started java programming, could you demo/how could I do that?? @hovercraft full of eels – user3144549 Jan 08 '14 at 18:56
  • @user3144549: Shouldn't **you** be the one to first show code? We can be much better positioned to help you with specifics if you do. – Hovercraft Full Of Eels Jan 08 '14 at 18:57
  • can I send you email or something? lol even copying part of code is too long at comment section @hover – user3144549 Jan 08 '14 at 19:03
  • @user3144549: no, you should try to post relevant code here as an addition/edit to your original question. You don't want to post too much code as it will be too much for volunteers to read and may be ignored. You don't want to post too little code as it may not contain enough relevant code for us to understand your problem. Your task then is to try to figure out how to post the right amount and type of code. This will take effort but is worth it. Look into creating and posting an [sscce](http://sscce.org). – Hovercraft Full Of Eels Jan 08 '14 at 19:08
  • /* this.setVisible(false); MainSelection s = new MainSelection(); s.setVisible(true); if(jToggleButton1.isSelected()) { jToggleButton1.setEnabled(false); } */ The above code is under a button where it redirects to main menu. The if loop shows a seat which if the seat is chosen ,I want it to be disable throughout entire execution. And for the solution you gave, what do you mean? The form I'm using for this code is JFrame form, what do I do?? @hover Also why isn't my code highlighting working – user3144549 Jan 08 '14 at 19:16
  • 1
    Please post code always as an edit to your original question, not in a comment where it can't be read. Thanks. Also, there's no such thing as an "if *loop*". It's not a *loop*, and that's an important distinction. – Hovercraft Full Of Eels Jan 08 '14 at 19:21
  • @JonathanDrapeau & hover so how should I fix the "new mainselection();??Sorry I'm really new in this java thing, my teacher just copy pasted all codes on projector and ask us to copy without explaining properly, I'm learning a lot today from here. – user3144549 Jan 08 '14 at 19:29
  • @user3144549 Hover as provided you with a few possible solutions already in his answer. If you want a more specific solution, edit your question and add the code, it will give us a better understanding of what is happening and how to help you with the code you have. – Jonathan Drapeau Jan 08 '14 at 19:34
  • @JonathanDrapeau edited and added the code at question, hope you guys can be more specific, cause I really have not much clue of what is being said at the solution . Also you haven't told me how should I fix the" new mainselection();" – user3144549 Jan 08 '14 at 19:41
  • @user3144549: see edit to answer. Please try some of the suggestions given – Hovercraft Full Of Eels Jan 09 '14 at 00:52
  • @HovercraftFullOfEels hey there, 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? – user3144549 Jan 09 '14 at 08:43
  • @user3144549: please see edits to answer and recommendations for you to be a better netizen of this site. – Hovercraft Full Of Eels Jan 09 '14 at 15:19