Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GUI Access Problems

 
David Dickinson
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've got a problem which im sure will be quiet common when building GUI's.

I have a class called GUI in which the constructor sets up the entire interface, however parts of the interface need to be updatable to reflect changes in data and user selection.

The method im using atm is JTextFields etc are created in the constructor, however they only exist for the duration of the constructor because they aren't class level.

So how do I get around the problem of needing to update these controls? I would need to create an entire new GUI with my current approach which isn't efficent.

Another example..

I have two combo boxes, the value selected in the first decides the value in the second. However because neither are class level I can't access them in the ActionListener that picks up the change in value from the first combo. So how do I get around this?

I'm assuming I don't have to make all of my controls class level fields?

I know this is quite a long post but it's stopping me from working atm.

Thank-you
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to use "class-level fields" (more commonly called "members" or "member variables") for all of your components -- just the ones that you'll need to refer to later. You seem to think this is difficult, somehow -- but all you need to do is move the declarations of the variables out to class scope. You can still initialize them in the constructor. In other words,

 
David Dickinson
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't mind doing this and have started making the changes, but it didn't feel natural. I thought there would be a better way which I was missing.

Can you give me some advice on this for instance...

I have a class-level member called cboComboBox


It is added to a panel during my constructor, then when an action is handled the following occurs..


This doesn't work however, or at least if it does the changes aren't showing in the interface. I've tried adding the following below the new JComboBox line...


But this doesn't work either.

Any idea what im doing wrong?

Thank you for confirming my original post.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have created a new comboBox, but you haven't added it to your panel. A better approach would be to modify the existing comboBox.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you create a brand new JComboBox, then the old one is still part of the GUI, and you don't have a handle to it anymore. Remember that a Java variable is just a reference or "pointer" to an object -- it's not the object itself. (See here for an entertaining explanation of this, if needed.)

What you want to do is to tell the existing JComboBox that it should display a new set of items. Perhaps the shortest -- if not the easiest to understand -- way to do this is to construct a new ComboBoxModel containing the new data, and giving it to the JComboBox:



I made a few other small but significant changes to this bit of code. One important one is to use the equals() method rather than the "==" operator to compare Strings. The operator checks for physical equality -- i.e., both Strings are the same physical block of memory. The equals() method checks for equivalence -- i.e., the same characters in the same order, but perhaps two different copies of them.

The other change is that I renamed your "StringArray" variable to "stringArray". Java's naming conventions for classes (CapitalizedLikeThis) and variables (lowercaseLikeThis) are very firmly entrenched; flouting them makes your code harder for other people to understand at a glance.
 
David Dickinson
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Java's naming conventions for classes (CapitalizedLikeThis) and variables (lowercaseLikeThis) are very firmly entrenched.


I have implemented your suggestions and I appreciate your help, when I found your post I was researching the ComboBoxModel interface but hadn't come across the DefaultComboBoxModel class, I appreciate it!

Additionally are there any conventions for method naming?

Thank-you for everybodys help!
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Code Conventions are here.

The section on naming conventions are here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic