• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does my ItemStateChanged only change just once

 
Jason Smit
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a strange problems. Perhaps some of you already have had this problem before.

I have method which is suppose to check if the itemstate has changed.
It ONLY works ONCE?

Thereafter, it does not matter how many times I changed the selection of the ComboBox.

---

Basically I have two Comboboxes.
1. If Combobox 1 is selected (e.g. Dogs, Cats)
2. Then get information from what ever the item from ComboBox 1 is selected, and display on ComboBox 2. (Dog Name, Cat Name etc)
e.g.
So If I selected Dogs on Combobox 1
Then display the Dog Names on Combobox 2.
If I then change and select Cats on Combobox 1,
then display Cat Names on ComboBox 2.

 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Impossible to guess without seeing the code. Please post a SSCCE.
 
Jason Smit
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Impossible to guess without seeing the code. Please post a SSCCE.


 
Jason Smit
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I resolved the issue, I had to set the model of the 2nd Combobox to null:

 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is NOT a SSCCE. How can we compile/execute that code? How can we access your database.

The point of a SSCCE is simplify the problem. Usually, as you found out here, you will find the problem while you create the SSCCE.
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that, by convention, variable and method names in Java start with a lowercase letter, with subsequesnt words being capitalized, aka camelCase. Underscores are normaly used only as word separators in STATIC_FINAL_CONSTANTS

Failure to follow the convention can make it difficult for others to read and understand your code, and can directly affect the amount of help you get on any public forum.

Additionally, it's not good practice to mix JDBC and Swing code in the same class.
 
Jason Smit
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Please note that, by convention, variable and method names in Java start with a lowercase letter, with subsequesnt words being capitalized, aka camelCase. Underscores are normaly used only as word separators in STATIC_FINAL_CONSTANTS

Failure to follow the convention can make it difficult for others to read and understand your code, and can directly affect the amount of help you get on any public forum.

Additionally, it's not good practice to mix JDBC and Swing code in the same class.


Thanks for the tip,
Please show me examples of your code to illustrate your explanations.
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show me examples of your code to illustrate your explanations.


Look at code you find in any text book, or in the Swing tutorial or in answers given in the forum.
 
Jason Smit
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I found this on the net:



I am right or wrong in saying that swing component/s should be placed on the main method of a script (i.e. public static void main(String[] args))?

And on methods?
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You place Swing components on Containers or other Components. You mean should you put the code in a certain place? And what is this about scripts? That is a program not a script.

That is a very basic example you have found; it is the GUI equivalent of Hello World. At least it specifically starts the event despatch thread (EDT).
No, don't put GUI assembling code in the main method. The main method is for starting an application and should do nothing else. There are several place you can write code; I like to put it in the constructor for a GUI class. A method which sets up the GUI is another place you can put it.
I do not like extending GUI classes. You often have to override JLabel#paintComponent, but apart from that you should be able to create a GUI without using any inheritance beyond what Swing already uses. That example does exactly that. It creates instances of the classes and adds them to one another. Spot on. And in a good location.
That createGUI method is old‑fashioned. In Java5 (2004) the add method was altered so there is no longer any need to write getContentPane(). Also the method for starting the EDT was greately simplified in Java8 (2014) so there is no need for the anonymous class any more. In fact Horstmann in Core Java for the Impatient says you should no longer teach anonymous classes.

If you use Java8, you can change two of those statements to:-
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!