• Post Reply Bookmark Topic Watch Topic
  • New Topic

JComboBox show just one line.  RSS feed

 
Joao Luiz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody.

I'm new here and in Java programming.

Well, I have 2 JComboBox in a JFrame.
In the FocusLost of the first JComboBox I execute a query and add the result set to the second JComboBox, it is ok.
But when I click in the second JComboBox the first time, it open only one line(but load all the items) but when I click one more time it open all the lines (3).

Image:


The code is here:


P.S.: Sorry for the bad english, I'm trying to improve it.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joao,

the problem might be caused by updating the second combobox when the first one loses the focus. This might be the problem, change of focus is a complex process and unless you're very familiar with the order of events generated when focus changes, it's easy to get it wrong. Instead of reloading the combobox when the focus is lost, try to reload it when the value in the first combobox changes. This could solve your issue.

Apart from this, it is not a good idea to mix database code and user interface code. You'll benefit a lot if you separate these two functionalities based eg. on a DAO pattern. Google for "java dao pattern" to get some insight on it.

The next, and much more urgent thing is that you definitely should learn to use bind variables. Your code performs less well and is subject to the SQL injection attack unless you do. Search for "bind variables" on this forum, or read a decent JDBC tutorial, to get the idea.
 
Joao Luiz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Martin.

It solve my problem.

But one question, I'm using the cbProdutosItemStateChanged, when I select an item it execute the code twice!
I know this because of the "System.out.println(codProd);", when I select an item it print the code twice in the console.
It is normal?
'Cuz the query is executed twice two.. And when I get more data in my database it can be a problem.

About the DAO Pattern and the bind variable, I'll search about this later. Added to my "java-learning to-do-list".
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joao Luiz wrote:... But one question, I'm using the cbProdutosItemStateChanged, when I select an item it execute the code twice! ...


I don't know Swing enough to understand whether this indicates a problem. However, you can easily avoid the overhead by remembering the last value for which you've loaded data for the second combobox and reloading it only when it actually changes. Something like this:

 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But one question, I'm using the cbProdutosItemStateChanged, when I select an item it execute the code twice!


This is the normal behaviour for a combo box.

Read the section from the Swing tutorial on How to Write an Item Listener and you should understand why.
 
Rob Spoor
Sheriff
Posts: 21050
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer using an ActionListener for JComboBoxes.
 
Joao Luiz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, you all rock!
Thanks for the replies.
Answered very simply.
Even me, a novice could understand.
I resolved my problem and some tips on materials for further study.
Thanks again.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!