Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JComboBox  RSS feed

 
Zohr Ridhan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having some problem using JCombobox . It doesn't show up until I slightly resize my window screen.And mouse click event some-times need two or three clicks to response.Does anybody know why this happens?
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anybody know why this happens?


Your code is wrong and you didn't post your code so I can't begin to guess what you are doing..

Read the section from the Swing tutorial on How to Use Combo Boxes. for working examples that show the proper way to create a frame with combo boxes.
 
Zohr Ridhan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using the acm.program.GraphicsProgram class, which I'm not familiar with. However, after looking at the javadoc, it seems to me that your run() method should not be called run but instead should be called init. Does changing it help?
 
Zohr Ridhan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Init doesn't bring any difference
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zohr Ridhan wrote:Init doesn't bring any difference


Well that's too bad. Perhaps someone with familiarity with the ACM library can help you.

I will say, though, that it is incorrect to call add(component, ...) in the run() method, or in methods called from run(), and doing so will result in behavior such as you describe. That's what the init() method is for.
 
Zohr Ridhan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohk ! Thanks for sharing your view.
What is your view about using run method in complete graphics program.In that case is it fine to use run method ? I think init method is used in event driven programs like I showed you above(by mistake I used run method) According to you, where should I specifically use init method?
 
Knute Snortum
Sheriff
Posts: 3942
92
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags (← that's a link) when posting code. I'll do it for you this time.
 
Knute Snortum
Sheriff
Posts: 3942
92
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zohr Ridhan wrote:What is your view about using run method in complete graphics program.In that case is it fine to use run method ? I think init method is used in event driven programs like I showed you above(by mistake I used run method) According to you, where should I specifically use init method?


When I used public void init() instead of public void run(), the GUI displayed without resizing. I think your kind of program should only use init().
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zohr Ridhan wrote:
What is your view about using run method...
...where should I specifically use init method?


In a program like this one, run() shouldn't appear at all. All component creation, all adding to containers, and all event registration should happen in init().

I'm presuming that acm.program.GraphicsProgram calls init() on the EDT and calls run() on some other thread. If it doesn't, then you probably need to call invokeLater() in init(). (The javadoc I linked to doesn't say one way or the other. But it does say, "In general, subclasses will override init in GUI-based programs where the program simply sets up an initial state and then waits for events from the user. The run method is required for applications in which there needs to be some control thread while the program runs, as in a typical animation." You're not doing animation, so you don't need a run method.)

Swing threading can be tricky. (See here.) Presumably this ACM library is trying to protect you from some of that, but I don't know for sure.
 
Zohr Ridhan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry ,using init solved the issue. Thanks again. I don't know why it doesn't work in first run. Thnks knute.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!