Disabling a jToggleButton throughout entire execution, possible? [closed]

百般思念 提交于 2019-12-02 12:56:42

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.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!