Results 1 to 3 of 3
Thread: Comparing two combo boxes
- 03-26-2011, 08:44 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Comparing two combo boxes
Okay, so I have two combo boxes formed from the same array of strings. I need the user to select a to and from from each combo box. I need to make sure that the to and from values aren't the same when a button is pressed and I've tried using == and .equals() to make sure but every time, regardless of the choices, it thinks that the values from both combo boxes are the same
Any ideas?Java Code:/* A rough working for a GUI interface */ import javax.swing.*; import java.awt.*; import java.awt.event.*; // Import the GUI packages public class Layout { public static void main(String[] args) { // Setting up JFrame and such String[] places = {"Leicester", "Loughborough", "Nottingham", "Derby", "Lincoln"}; JComboBox to = new JComboBox(places); JComboBox from = new JComboBox(places); // Declare the drop down menus and add the options final JLabel result = new JLabel(""); final Object fromResult = from.getSelectedItem(); final Object toResult = to.getSelectedItem(); // Declare the label to show time/price/stops JLabel fromText = new JLabel("From: "); JLabel toText = new JLabel(" To: "); // Declare the labels for the drop down menus final JButton price = new JButton("Route Price"); final JButton time = new JButton("Route Time"); final JButton stops = new JButton("Route Stops"); final JButton admin = new JButton("Admin"); final JButton exit = new JButton("Exit"); // Declare the main menu buttons class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { JButton clicked = (JButton) e.getSource(); if (clicked == price) { if (toResult.equals(fromResult)) { result.setText("You can't travel between the same place"); } // Check if the to and from depots are the same else { result.setText("You want the price from " + fromResult.toString() + " to " + toResult.toString()); } } if (clicked == time) { if (toResult.equals(fromResult)) { result.setText("You can't travel between the same place"); } // Check if the to and from depots are the same else { result.setText("You want the time from " + fromResult.toString() + " to " + toResult.toString()); } } if (clicked == stops) { if (toResult.equals(fromResult)) { result.setText("You can't travel between the same place"); } // Check if the to and from depots are the same else { result.setText("You want the stops from " + fromResult.toString() + " to " + toResult.toString()); } } if (clicked == admin) { result.setText("You want the admin menu"); } if (clicked == exit) { System.exit(0);} // Close on click } } // Create a class to handle the buttons // Code continues } }
-
You must get your to and from results in the action listener's actionPerformed method, not in some method or constructor that is only called once. This makes sense because you need to know what was selected at the time the button is pressed not just what is selected at the beginning of the program. e.g.,
Java Code:public class Layout { public static void main(String[] args) { // Setting up JFrame and such // ..... // !!! don't get to and from results here! final Object fromResult = from.getSelectedItem(); final Object toResult = to.getSelectedItem(); // etc... class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { // !! get your to and from results here!! JButton clicked = (JButton) e.getSource();
In the future, consider creating and posting an SSCCE if you want helpful advice quicker.Last edited by Fubarable; 03-26-2011 at 09:36 PM.
- 03-27-2011, 12:28 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
dynamically populate the city combo box based on the values of state combo +ajax+jsp
By sandy1000 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-29-2010, 10:00 AM -
multiple combo boxes
By simo_mon in forum AWT / SwingReplies: 1Last Post: 07-22-2010, 11:57 AM -
dialog boxes
By gedas in forum Java 2DReplies: 1Last Post: 02-19-2010, 11:23 AM -
[SOLVED] Combo Boxes Automation
By dbashby in forum New To JavaReplies: 3Last Post: 03-27-2009, 12:39 AM -
creation of one combo box form another combo box
By er.tyagigaurav in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-09-2008, 03:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks