• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JCheckBox itemListener

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for (ListIterator i = userList.listIterator(); i.hasNext()
{
jcb = new JCheckBox((String) i.next());
jcb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ie) {

* assuming that userList is populated
* I am displaying a list of JCheckBoxs with an associated label
* what do I do here to get the label
* associated with the selected JCheckBox??

};
});
namePanel.add(jcb);
}

Thanks,
Gary.
PS. how does one include these Instant Graemlins
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


getUser null < I expected this 2nd
words1 null <<<<< I expected this last
listener Dad <<<<< this is the correct label for the selected JCheckBox


I would not expect the user item to be populated until the itemStateChanged listener was activated. Should it be activated by your code or by the GUI user? If by the code, what is its position in the table? You would need to generate something specific to select the first or last item. If by the user, then "user" will not be populated by clicking on the list?

Does the list display what you expect?
 
Gary Down
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the input,

I reposted the code, hopefuly it is easier to read.
The display from the itemListener displays correctly when I select the JCheckBox, so the field "user" in class WordUser is populated OK.
The field "user" is not populated wgen displayed from class Words1.

Thanks again,
Gary.
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I hope I can explain this properly, but the inner class instance of ItemListener that you have attached to jcb does not execute its itemStateChanged method until you change the value of a check box, and that does not happen until you click on it.

The variable "user" is only populated when you click on a checkbox! When you are populating the checkboxes, "user" will still be null.

So variable "user" can't be used till you've ticked a box!

Maybe we can help you if you explain what your program should do that it does not do now.

Ed
 
Gary Down
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,
I understand that, and that part is working.
What I want to do is then access the WordUser.user back in Words1.
I have restructured the code somewhat

These are the System.out messages in the order they appear

WordUser: getUser null
Words1: after get null
At this point the screen "WordUser" is now visible
with eveything correct and in place

After clicking the check box for "Roy"
WordUser: setUser Roy
WordUser: listener Roy

I think (and sometimes that hurts) that the problem is to do with threads.
Could be wrong.

Thanks for you patience,
Gary.
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you worried about the first two nulls? They are there because no checkbox has been clicked yet.

Your main code starts the whole thing going and then terminates. The Swing thread is handling your checkbox click and other things should happen from that.

You would need another widget if you wanted anything else to happen.
 
Gary Down
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eddie,
firstly thanks for the help.

I understand aout the threads but what is a widget.

What confuses me is that both the first 2 "null" diaplays originate from the "main" of Words1 after WordUser is instantiated but get produced before WordUser Frame is displayed.
I realise that there is 2 threads running and there us no way to tell which one will run first.
How do I get the value back to the "main"?

Gary.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gary Down:
Eddie,
firstly thanks for the help.

I understand aout the threads but what is a widget.

What confuses me is that both the first 2 "null" diaplays originate from the "main" of Words1 after WordUser is instantiated but get produced before WordUser Frame is displayed.
I realise that there is 2 threads running and there us no way to tell which one will run first.
How do I get the value back to the "main"?

Gary.



A widget it a GUI control, like a JButton, JFrame, etc.

The logic of your application confuses me. I don't understand why you are using 2 classes the way you are and passing your Connection object from one to the other. There is no point/need to do it this way. I think that is possibly making your app more complicated than it needs to be.

Regarding the whole "2 threads running issue", that point is moot. You are trying to get information from a variable before that variable has a value. Thus, the NULL being returned. The only way to fix this is to give your String user and value as soon as the variable is created. Or at least before you call getUser().

It doesn't matter how many threads there are or how long you wait before calling getUser(). If you don't give getUser() a value, you will get null.

The logic in your application is this:



Since you never set the value of user prior to calling getUser()....I don't know how to make this more clear.

As far as your last question....

How do I get the value back to the "main"?

The value of what, user? Again, user needs a value for you to get back. Until you click on a check box, which gives user a value, user will have no value. That's your logic.
 
Gary Down
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clear as mud so far,


The logic of your application confuses me.......


This is only the first of many screens to be user and controlled from the one application. If this is the wrong path to go down feel free to point me in the right direction


Regarding the whole "2 threads running issue", that point is moot. You are trying to get information from a variable before that variable has a value. Thus, the NULL being returned. The only way to fix this is to give your String user and value as soon as the variable is created. Or at least before you call getUser().


Given these two lines of code I was expecting the WordUser to be populated in the first. I understand the JFrame is contructed and displayed in the constructor of WordUser however, I had not expected the next line to run until the WordUser frame had accepted any response/clicks and thus polpulated "user" before the next line attepts to retrieve the value of user.

WordUser wu = new WordUser(Words1.con);
String user = wu.getUser();


It doesn't matter how many threads there are or how long you wait before calling getUser(). If you don't give getUser() a value, you will get null.


As above, I had expected to the GetUser.user to be populated before returning to Words1 and therefore being populated.


Get connectionInstantiate WorkUser class
------Get Data from database
------Create JPanel
------Display data as Check Boxes
------show JFrame
Try and Get Value from user variable



------show JFrame

this is where I had expected the WordUser.user to be populated and ready for retrival by the code :- String user = wu.getUser();

Try and Get Value from user variable
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given these two lines of code I was expecting the WordUser to be populated in the first. I understand the JFrame is contructed and displayed in the constructor of WordUser however, I had not expected the next line to run until the WordUser frame had accepted any response/clicks and thus polpulated "user" before the next line attepts to retrieve the value of user.

You make an interesting point. I am going to do some investigation on this tonight as soon as my kids go to bed and I'll let you know what I find out. I think you are correct in your assumption, however, I have an idea as to why the code is being executed....more to come...
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, this is kind of interesting and I'll have to see if I can find out why. Here is the code I tested with...





Now, if I run main.java the word Main is printed on the console. And if I run Test.java test is printed on the console. Both of this print statements are issued after the instantiation of the JFrame.

I was under the impression, as you were, that lines after creating a new JFrame were not executed. I'm not sure why I thought this except for the fact that I thought when a JFrame was opened the Swing thread started and that's what doesn't allow public static void main() to exit until you kill the Swing thread of the VM with System.exit(0). However, this is not the case.

Now, as far as your application is concerned, I'm not sure that this matters. There is no reason you need to get the value of user until a checkbox is checked. So while we found out something interesting today, I don't think it really matters for you.
 
Gary Down
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After re-reading about threads I believe the program is working and the problem is not a problem but is the way threads work. As the "main" thread has very little (actually nothing) before the call to getName it access it first, before the screen has had time to populate name.

The easy way around it was to add synchronised to the getName function (this holds the "main" access until a switch activated by a click on the screen releases populates "user" and releases "main".

Thanks,
Gary.
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What actually happens is that main is terminated and the app is held together by the swing thread. So any further processing needs to come from a timer or from a widget - your main is finished. Sync locks will not make any difference to this.

Methods are not easily held up. Sleep will only delay for a little while, there is a version of invokeLater that will hold up a thread waiting for another, you can have infinite loops, and key entry can be interesting, but otherwise methods (main is just a method) only take at the most milliseconds to run.

So to proceed further with the action of your program you need to provide hooks on widgets from human input to make more things happen!

Have fun!
 
Gary Down
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eddie,
could you please relate your comment to code I have posted. I don't folow what you are getting at. The displays at the end of the code show that the "main" in Words1 waited until I selected a checkbox and clicked continue. Probably unecessary, but just to see if the "main" waited I left the screen untouched for a few minutes before selction.

Sorry about the persistance. I'm just getting into java.

Gary.



WordUser: setUser William William
Words1: ending 6 William
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gary,

Firstly, one day you'll make a great programmer with great job security. Nobody will be able to understand your code so you will have maximum job security. You should take note of Greg's comments about simplifying your code!

Seondly, threads can be a difficult area and most programmers minimize interaction between threads so as to avoid deadlocks.

Your code runs exactly the same if you rewrite getUser without referring to threads:


The only reason main does not finish quickly is because you've locked it into an infinite loop in getUser until nameAvailable is set. That introduces huge coupling between the swing and main threads!

Thirdly, if you are ever to be employed you need to be able to listen to user requirements. You can practice here by writing your own user requirement.

Generally, users only see your screens and they will specify how they will behave when various widgets are activated.

For us to be able to help you further, you need to specify exactly what you expect your GUI to do.

Programming should be fun and you should find it easier as you streamline your programs!

Ed
 
Gary Down
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I understand the point about the "main" thread just being in a loop until thw "swing" thread sets the variable to release it.


Firstly, one day you'll make a great programmer with great job security. Nobody will be able to understand your code so you will have maximum job security.



.



Seondly, threads can be a difficult area and most programmers minimize interaction between threads so as to avoid deadlocks.


A fairly big assumption to make. However it is retrospectively correct as I have more than 20 years successful programming experience in a number of languages. It could be that background that inhibits my understanding of threads. However, the understanding will be achieved and if minimizing interaction between threads is the way to go then I will undertand why

All sarcasm aside,
a simple question, how do you make one thread (in this case the "main") wait on another's (the "swing" thread) completion before the first continues.

As for the comments about simplifying the code, I am open for constructive suggestions (as stated in earlier posts). However, until I get a satisfactory understanding of threads (that is what tis about) I am not too concerned.

 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gary Down:
Ok, I understand the point about the "main" thread just being in a loop until thw "swing" thread sets the variable to release it.


Firstly, one day you'll make a great programmer with great job security. Nobody will be able to understand your code so you will have maximum job security.



.



Seondly, threads can be a difficult area and most programmers minimize interaction between threads so as to avoid deadlocks.


A fairly big assumption to make. However it is retrospectively correct as I have more than 20 years successful programming experience in a number of languages. It could be that background that inhibits my understanding of threads. However, the understanding will be achieved and if minimizing interaction between threads is the way to go then I will undertand why

All sarcasm aside,
a simple question, how do you make one thread (in this case the "main") wait on another's (the "swing" thread) completion before the first continues.

As for the comments about simplifying the code, I am open for constructive suggestions (as stated in earlier posts). However, until I get a satisfactory understanding of threads (that is what tis about) I am not too concerned.


Gary,

First of all, I am not trying to be sarcastic. I have seen many programmers like you and once they settle in they do quite well. You are inquisitive and trying to find your own way in swing programming.

If you are trying to plumb the depths of thread management you should pose your question in the threads and synch forum. I have little experience of using threads in swing programming so I feel I cannot really help you with your program.

I do have a little experience using swing, and am writing a program with about 80 major classes. I have to keep things simple to cope with the complexity. This program has a major GUI component and most actions occur as a result of user action through widgets and some timer events.

The only time I would use threads is to offload some of the action to speed up the GUI.

Hope that clears the air.

Ed

 
Gary Down
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,
to you all, I believe I now have a workable understanding of threads and how they interact with each other. I will probably discard the code I was using as it was just a learning exercise.

Eddie,
thanks and good luck with your new program. Sounds like it could be a bit of a challenge.

Gary.
[ May 23, 2004: Message edited by: Gary Down ]
 
You've gotta fight it! Don't give in! Read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic