I want to use JComboBox object in JButton like if I select the desired text from JComboBox and click on button the image appear accorndingly, is it either possible or not, need help because I cant even make object of JCommbo in JButton.
Asked
Active
Viewed 100 times
0

Cardinal System
- 2,749
- 3
- 21
- 42

John Snow
- 3
- 5
-
Your question is vague and a bit confusing. Can you paste the actual code in a Short, Self Contained, Correct (Compilable), Example (http://sscce.org/) on StackOverflow? But yes, it's entirely possible to reference a JComboBox in the ActionListener for a JButton. – Riaan Nel Dec 28 '17 at 07:31
-
@RiaanNel I just edited the post, and in the second picture like if I click on desired time and click on Button the image of desired route open but I can't make object of JComboBox in Button if condition – John Snow Dec 28 '17 at 07:37
-
@JohnSnow so if the user selects "6.A.M." in the combo box, then the button's text will change to "6.A.M."? – Cardinal System Dec 28 '17 at 07:40
-
@CardinalSystem no the Button will open image of routes – John Snow Dec 28 '17 at 08:19
-
@JohnSnow so what do you want the relation between the combo box and button to be? – Cardinal System Dec 28 '17 at 08:22
-
The relation will be if the user clicks like "6.A.M" and clicks on JButton then an image of 6 A.M routes appear, I've tried your code but its not useful.. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { if("6.A.M.".equals((String)jComboBox1.getSelectedItem())) { SixAMRoute sam=new SixAMRoute(); sam.setVisible(true); this.dispose(); } else System.out.print("Invalid"); }~~ it always goes in else block @CardinalSystem – John Snow Dec 28 '17 at 08:26
-
@JohnSnow I get it now. Where is your image file for 6 am routes, and where is the component that will contain it? – Cardinal System Dec 28 '17 at 08:31
-
@CardinalSystem the image is in my E drive, I've also tried to give the link of image them I've made another Panel on which the image is stored and then Call the image class like this ` SixAMRoute sam=new SixAMRoute(); sam.setVisible(true); this.dispose();` but that's not the problem, there's something wrong with my 'IF' condition – John Snow Dec 28 '17 at 08:38
-
@JohnSnow I updated my answer. – Cardinal System Dec 28 '17 at 11:02
1 Answers
0
You must make your JComomboBox
accessible from the ActionListener
in order to get the selected text. You also need to use the proper method for getting the selected text, such as JComboBox#getSelectedItem()
. Consider this example:
JComboBox<String> myComobBox = new JComboBox<String>();
JButton myButton = new JButton("jButton");
myComboBox.addItem("6.A.M");
// Add button listener
myButton.addActionListener(e -> {
// Use getSelectedItem instead of getText
if(((String) myComboBox.getSelectedItem) == "6.A.M") {
SixAMRoute sam = new SixAMRoute();
sam.setVisible(true);
this.dispose();
}
});
If you want your action listener to be it's own class, you will need to use access modifiers to make the JComboBox accessible from the listener.

Cardinal System
- 2,749
- 3
- 21
- 42