Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default 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
    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
    	}
    }
    Any ideas?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  3. #3
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

Similar Threads

  1. dynamically populate the city combo box based on the values of state combo +ajax+jsp
    By sandy1000 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-29-2010, 10:00 AM
  2. multiple combo boxes
    By simo_mon in forum AWT / Swing
    Replies: 1
    Last Post: 07-22-2010, 11:57 AM
  3. dialog boxes
    By gedas in forum Java 2D
    Replies: 1
    Last Post: 02-19-2010, 11:23 AM
  4. [SOLVED] Combo Boxes Automation
    By dbashby in forum New To Java
    Replies: 3
    Last Post: 03-27-2009, 12:39 AM
  5. creation of one combo box form another combo box
    By er.tyagigaurav in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-09-2008, 03:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •