File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting the previously selected item from a JComboBox

 
Esben Christensen
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I have dropped the idea with a loop defining things but then I would need a string containing the previously selected item from a JComboBox. The combobox is set up by NetBeans and the code for creating it isn´t showed.

Any one have any idea how to make such thing how I imagine it should look (though this doesn´t exactly work):



Thanks in advance, any help is highly apreciated
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add an java.awt.event.ItemListener to the JComboBox. The listener will be given an java.awt.event.ItemEvent whenever the item's state changes. So whenever a new item is selected, one ItemEvent is sent for the Item that gets DeSelected, and one ItemEvent is sent for the Item that gets Selected.
 
Esben Christensen
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for the help again Steve. However im not entirely sure how it works, how I made it so far inside the combobox:


I made it from looking at what NetBeans made about ActionListener:


and also by looking at http://java.sun.com/docs/books/tutorial/uiswing/events/itemlistener.html

Any idea what caused the error i got? Here´s the full error message:

Exception in thread "main" java.lang.NoClassDefFoundError: ItemEvent
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
at java.lang.Class.getDeclaredMethod(Class.java:1935)
at java.awt.Component.isCoalesceEventsOverriden(Component.java:5901)
at java.awt.Component.access$400(Component.java:169)
at java.awt.Component$4.run(Component.java:5855)
at java.awt.Component$4.run(Component.java:5853)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Component.checkCoalescing(Component.java:5852)
at java.awt.Component.<init>(Component.java:5821)
at java.awt.Container.<init>(Container.java:251)
at javax.swing.JComponent.<init>(JComponent.java:581)
at javax.swing.JPanel.<init>(JPanel.java:65)
at javax.swing.JPanel.<init>(JPanel.java:92)
at javax.swing.JPanel.<init>(JPanel.java:100)
at skolehjælp.Panelet.<init>(Panelet.java:9)
at skolehjælp.Main.main(Main.java:11)
Caused by: java.lang.ClassNotFoundException: ItemEvent
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 17 more
Java Result: 1

Thanks in advance. Sorry I havent been able to get to my computer for a while
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.NoClassDefFoundError: ItemEvent


From the JavaDoc for java.lang.NoClassDefFoundError:

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class ... and no definition of the class could be found.


Simply put, The JVM doesn't know where ItemEvent is located? Why? You have neither imported it (adding import java.awt.event.ItemEvent; to the top of your class file) nor have you used the fully qualified class name (everywhere you see ItemEvent, change it to java.awt.event.ItemEvent).

This is one of the most basic errors in Java. You should be able to fix it in your sleep (no kidding). I suggest you go back to the tutorials, and start working without an IDE, at least until you get the basics of how to compile, run, package, import, reference out-of-package objects, and so on by yourself. This isn't meant as criticism, if you do this now it will save you a lot of trouble later on.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic