Results 1 to 12 of 12
  1. #1
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    84
    Rep Power
    0

    Default Comboboxes lock on to eachother

    My code is long so I will try to explain this.

    I have a GUI application which gets two dates from a user via multiple combo boxes (month/day/year for each, so 6 total). The assembly and processing of these dates (including the days changing based on the month) is correct.

    That said, however, when interacting with the program and changing the dates back-and-fourth on the frame, the 'days' combo boxes occasionally lock to one another. For instance, if you select 5 in one, 5 mimics in the other and this cannot be undone.

    Any idea why this would be happening or what I should be looking for?

    EDIT: Here's a small bulk of code that may help. These are the last relevant bits I believe before the 'SUBMIT' button is clicked and it is all processed.

    Controller ... (same set-up for the departure date)
    Java Code:
    // MONTH COMBO BOX LISTENER -- ARRIVAL
    class ArriveCBListener implements ActionListener {  
    
        @Override
        public void actionPerformed(ActionEvent e) {
    
            // Arrive Dates
            if(view.getArriveMonth() == "January" || view.getArriveMonth() == "March" || view.getArriveMonth() == "May" || view.getArriveMonth() == "July" || 
                     view.getArriveMonth() == "August" || view.getArriveMonth() == "October" || view.getArriveMonth() == "December") {
                view.changeArrivalDays("A31"); // 31 days
            }
            else if(view.getArriveMonth() == "April" || view.getArriveMonth() == "June" || view.getArriveMonth() == "September" || view.getArriveMonth() == "November") {
    
                view.changeArrivalDays("A30"); // 30 days
            }
            else if(view.getArriveMonth() == "February"){
    
                view.changeArrivalDays("A28"); // 28 days
            }   
    
        }// End of Actions
    
    } // End of Month combo box listener
    View:
    Java Code:
      // --- Changes to DAYS Combo Box --- //
    @SuppressWarnings("unchecked")
    public void changeArrivalDays(String cmd){
    
        if (cmd == "D31") { // 31 days
            jcbArriveDay.setModel(models[0]);
        }
        else if (cmd == "A30") {  // 30 days
            jcbArriveDay.setModel(models[1]);
        }
        else {  // 28 days
            jcbArriveDay.setModel(models[2]);
        }
    
    } // End change arrive days
    
    @SuppressWarnings("unchecked")
    public void changeDepartureDays(String cmd){
    
        if (cmd == "D31") { // 31 days
            jcbDepartDay.setModel(models[0]);
        }
        else if (cmd == "D30") {  // 30 days
            jcbDepartDay.setModel(models[1]);
        }
        else {  // 28 days
            jcbDepartDay.setModel(models[2]);
        }
    
    } // End change depart days

  2. #2
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Comboboxes lock on to eachother

    Hard to tell... there's not enough to go with without seeing more of the code. Just a guess... perhaps the combo boxes are sharing the same model? Your code snippets indicate this: setModel(models[0]). You should be able to attach a listener to the model to see what's changing it.

  3. #3
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    84
    Rep Power
    0

    Default Re: Comboboxes lock on to eachother

    They are actually; I take it they shouldn't be.

    I don't have time to mess around with it at the moment but I could create a second later. How would you attach a listener to it?

    Below is the creation of the model.

    Java Code:
    	// Combo Box Model
    	@SuppressWarnings("rawtypes")
    	protected ComboBoxModel[] models = new ComboBoxModel[3];
    ...

    Java Code:
    	// METHOD HOLDING INITIAL PROMPT
    	public Hotel_GUI() {
    		
    		// Define Models
    		models[0] = new DefaultComboBoxModel<Object>(days31);
    		models[1] = new DefaultComboBoxModel<Object>(days30);
    		models[2] = new DefaultComboBoxModel<Object>(days28);
    After this it is mentioned in the code posted above with the listeners for the Month combobox.

  4. #4
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Comboboxes lock on to eachother

    Take a look at ComboBoxModel (Java Platform SE 7 ). You'd find you can use the addListDataListener method.

    Good hunting!

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,785
    Rep Power
    5

    Default Re: Comboboxes lock on to eachother

    Don't use == to compare strings. Use equals().

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    84
    Rep Power
    0

    Default Re: Comboboxes lock on to eachother

    Jim - I've heard that before now that you mention it. I'll swap it out.

    Thank you all for the replies and help. I'll look into this issue a bit deeper with the tips mentioned above and let you know.

    EDIT: It appears that the addition of a second ComboBoxModel[] has resolved the issue. I just changed the dates repeatedly and have not been able to recreate the issue. I also added combobox.setSelectedIndex(0) following each change of the model to reset the the day to the first of the month.
    Last edited by mallorz; 02-22-2014 at 11:58 PM.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,785
    Rep Power
    5

    Default Re: Comboboxes lock on to eachother

    Quote Originally Posted by mallorz View Post
    I've heard that before now that you mention it. I'll swap it out.
    Good. But do you know why you are supposed to use equals()? It is an important concept.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    84
    Rep Power
    0

    Default Re: Comboboxes lock on to eachother

    Quote Originally Posted by jim829 View Post
    Good. But do you know why you are supposed to use equals()? It is an important concept.

    Regards,
    Jim
    I don't believe I've heard the reason behind it?

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,785
    Rep Power
    5

    Default Re: Comboboxes lock on to eachother

    Equals compares what the references point too. == compares the values of the references themselves.
    Here is an example that demonstrates the concept.

    Java Code:
    public class StringTest {
       public static void main(String[] args) {
          String a = "Hello";
          String b = "Hello";
          System.out.println(a == b); // prints true - uses interned (cached) values
                                      // so the references are the same (i.e. a and b refer to the same object)
          System.out.println(a.equals(b));// prints true - because what the references refer
                                          // to are equal, independent of the references
                                          // themselves.
          a = new String("Hello");
          b = new String("Hello");
          System.out.println(a == b); // prints false - because of different references
          System.out.println(a.equals(b)); // prints true - because what the references
                                           // refer to are equal 
       }
    }
    When comparing objects, always use equals. The only exception is that you can use == to check to see if the reference is null or not.

    Regards,
    Jim
    Last edited by jim829; 02-23-2014 at 12:33 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    84
    Rep Power
    0

    Default Re: Comboboxes lock on to eachother

    That mostly makes sense. The second (a == b) printing false when the first printed true I don't 100% understand to be honest as the strings are defined the same however I get the concept.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,785
    Rep Power
    5

    Default Re: Comboboxes lock on to eachother

    Quote Originally Posted by mallorz View Post
    The second (a == b) printing false when the first printed true I don't 100% understand to be honest as the strings are defined the same however I get the concept.
    Imagine two buckets, one red and one blue. They both hold exactly the same amounts of water. == compares the color of the buckets (which are different just like references are different). equals() compares their contents or the amount of water.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    84
    Rep Power
    0

    Default Re: Comboboxes lock on to eachother

    Thanks, Jim. I'll do my best to keep this in mind!

Similar Threads

  1. Need help getting two loopslined up beside eachother
    By MrPosotive in forum New To Java
    Replies: 9
    Last Post: 10-05-2012, 05:23 AM
  2. Battleship Game - Pieces lay on top of eachother.
    By lolerskates2695 in forum New To Java
    Replies: 7
    Last Post: 04-25-2011, 01:05 AM
  3. Comboboxes and JTables
    By Hugo in forum AWT / Swing
    Replies: 4
    Last Post: 04-24-2011, 06:50 PM
  4. How to make two jlists import objects from eachother
    By Embercloud in forum New To Java
    Replies: 0
    Last Post: 12-30-2010, 05:24 PM
  5. comboboxes
    By duncan913 in forum New To Java
    Replies: 3
    Last Post: 12-09-2010, 06:26 AM

Posting Permissions

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