This question already has an answer here:
-
Disabling a jToggleButton throughout entire execution, possible? [closed]
1 answer
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
Well, the JToggleButton
's "enabled" property most probably doesn't reset itself to true
when you set it to false
. I think that you actually are looking at an other JToggleButton
's instance. Maybe you re-create it every time you access the "select seat" GUI?
In that case, what you want is to dissociate the application-data (where you have the information about which seat are already reserved etc) from the user interface.
A good way to do so, I believe, is to work with the Model-View-Controller pattern.
Let me introduce you to this design pattern:
- the Model holds your application data (e.g., could be a
Set<Seat>
)
- the View is use to display your data (i.e. your
JToggleButton
)
- the Controller is the entry point from which your Model is updated (in your case, those are also the
JToggleButton
)
You can read more about it there: MVC Pattern - tutorialspoint.
Basically, instead of creating a new "empty" View every time, you want to load it with the values contained by your data container (the Model). Then, when you click on some JToggleButton
that represents a seat (a Controller), you don't directly change the state of the button, but you just simply update your Model to toggle the state (reserved or not) of your seat. Then, each time the Model is updated, your View refreshes using the new data available. That's it!
+--------------------------------------+
| +-----------+ |
| | Model <-------+ |
| +-----v-----+ | |
| | | |
| | | |
| +-----v---+---------^----------+ |
| | View | Controller | |
| +---------+--------------------+ |
| |
+---------- Your application ----------+
Feel free to ask more about it, it might seem really like a lot to change but it comes with many interesting design features :)
Especially if you need to have another View (for example, a File
in which you would like to save your data every time a change is made)!