Soroush Hakami
Soroush Hakami

Reputation: 5396

Keep track of Swing GUI "current state"

Sorry the title is fuzzy, but I really coudln't come up with a fitting title.

I'm developing my first application with Swing, and I'm having a hard time figuring out how to keep track of the current view of the application. With I mean with current view is for example if a button has already been pushed. For example, you shouldn't be able to press "Execute" before a file has even been loaded. I've come up with an architechtural solution to this that is really crappy, and I'd like tips on how to improve it.

I have a label called infoText, and it's updated pretty much every time I press a button. Through this, I'm keeping track of the applications state in this fugly way:

if (infoText == LOADING_NARROW){
                    printSelected(narrow_list);
                }else{
                    printSelected(list);
                }

Upvotes: 1

Views: 1131

Answers (3)

user unknown
user unknown

Reputation: 36229

First: Are they different methods, or a copy-paste-error?

      printSelecteds (narrow_list);
      printSelected (list);

Second: To disable a button you usually use:

ok.setEnabled (false);

If the file is loaded, you call

ok.setEnabled (true);

to enable the JButton "ok".

I don't see how that is related to your info-text, and to your printSelected(s) method. If you pass the state via the GUI, you might loose the one or the other due to race conditions. Changing a label could be the sink of an state change.

You could have mutual exclusive bit patterns to allow interference:

FILE_OPEN = 1;
SEARCHED = 2;
FRIDAY = 4; 

to add them bitwise:

state |= FRIDAY

to ask them up in a binary pattern:

if (state | FILE_OPEN) ....

It doesn't look very elegant to me. I guess I'm not sure what your problem is. :) To fire an action if some button is pressed, you have to implement an actionListener, which could modify your label as well. But the swing eventloop will already check the state of your components. You seem to duplicate the work partly.

Upvotes: 0

brian_d
brian_d

Reputation: 11385

Swing Components keep track of their own states.

My advice:

  • Initiate the application to a default state.
  • Adjust the settings in an event driven manner. For instance when JButton A is clicked, enable JButtons B and C and set a JTextField.
  • Check the states of objects with their builtin methods. Example if((jButtonA.isEnabled() && jTextField.getText().equals("foobar"))

You can also use the mediator pattern to group related components and their actions.

Upvotes: 2

jzd
jzd

Reputation: 23629

Rather than keeping track of your state with GUI components, use normal Java objects and variables.

Just keep a boolean loadingNarrow in this case that you reference and update when needed.

Also if you are running a large load as the result of a button press and don't want the user to press it again you can disable the button once the load starts and re-enable it later. (Note I am assuming you are running the load on a separate thread so the GUI does not freeze).

Upvotes: 4

Related Questions