Results 1 to 9 of 9
  1. #1
    rookiecoder is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default JComboBox false duplication

    I'm hoping someone can shed some light on why this keeps occuring. I just finished creating two rows of two jComboBoxes. The first column, when selecting a certain item, causes the second to vary its output. This is currently working wonderfully, unfortunately, when choosing an item in the first row, (for example out of fruit, vegetable, and mineral; where vegetable is selected), the second JCB in the column copies the selection of the first.

    [vegetable] [choose veggie]

    [vegetable] [empty]

    What's even more confusing is that although it "displays" vegetable in the second row, it isn't actually selecting it, so the second column in the row doesn't correlate. If I go ahead and select fruit it will "reset" to the correct corresponding items, but sure enough when the first row is again changed, it happens again.

    [vegetable] [choose veggie]

    [vegetable] [choose fruit]

    Here is a snippet of the code:

    Java Code:
            //Row 1
            jLabelRows[0] = new JLabel();
            jLabelRows[0].setText("1:");
            jCombo[0] = new JComboBox();
            jCombo[0].setModel(departmentModel);
            jCombo[0].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt) {
                    jComboActionPerformed(0);
                }            
            });
            jCombo[1] = new JComboBox();
            jCombo[1].setModel(accountModel);
            jTFRows[0] = new JTextField();
            jTFRows[1] = new JTextField();
            jTFRows[2] = new JTextField();
            
            //Row 2
            jLabelRows[1] = new JLabel();
            jLabelRows[1].setText("2:");
            jCombo[2] = new JComboBox();
            jCombo[2].setModel(departmentModel);
            jCombo[2].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt) {
                    jComboActionPerformed(2);
                }            
            });
            jCombo[3] = new JComboBox();
            jCombo[3].setModel(accountModel);
            jTFRows[3] = new JTextField();
            jTFRows[4] = new JTextField();
            jTFRows[5] = new JTextField();
    
            ...
    
        private void jComboActionPerformed(int i) {
            Object department = jCombo[i].getSelectedItem();
            String departmentNumber = (String) department;
            MyComboBoxModel accountModel = 
                    new MyComboBoxModel(property.getAccountNumbers(departmentNumber), 1);
            jCombo[i + 1].setModel(accountModel);
        }
    
        ...
    
    private class MyComboBoxModel extends AbstractListModel implements ComboBoxModel {
    
            private Object selectedItem;
            private ArrayList anArrayList = new ArrayList();
    
            public MyComboBoxModel(Set<Department> departmentSet) {
                for (Department department : departmentSet){
                    anArrayList.add(department.getDepartmentNumber());
                }
            }
            
            public MyComboBoxModel(Set<Account> accountSet, int i) {
                for (Account account : accountSet){
                    anArrayList.add(account.getAccountNumber());
                }
            }
    
            public Object getSelectedItem() {
                return selectedItem;
            }
    
            public void setSelectedItem(Object newValue) {
                selectedItem = newValue;
            }
    
            public int getSize() {
                return anArrayList.size();
            }
    
            public Object getElementAt(int i) {
                return anArrayList.get(i);
            }
    
        }
    I've determined that the mishap occurs somewhere in the ActionListener(according to the debugger), but my hunch is that it is somewhere in myJBCModel that is causing some kind of unconvential hiccup that I don't understand. I'm not very good with models, so any insight into this would be much appreciated.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: JComboBox false duplication

    Can you post a small complete program that compiles, executes and shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    rookiecoder is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Re: JComboBox false duplication

    Okay, I went ahead and created this to simulate what I'm doing on a smaller scale. Its all in one class, so its a bit messy, but it is doing the same thing as my other code.

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Set;
    import javax.swing.*;
    
    public class TestJComboBox {
    
        private static class MarketGUI extends JFrame {
            private JComboBox[] jCombo = new JComboBox[4];
            public MarketGUI() {initComponents();}
    
            private void initComponents() {
                
                createFoodCategory("Fruit");
                createFoodCategory("Vegetable");
                createCategoryType("Apple", "Fruit");
                createCategoryType("Banana", "Fruit");
                createCategoryType("Carrot", "Vegetable");
                createCategoryType("Cabbage", "Vegetable");
                
                MyComboBoxModel foodCategoryModel = new MyComboBoxModel(foodCategorySet);
                MyComboBoxModel categoryTypeModel = 
                            new MyComboBoxModel(categoryTypeSet, 1);
    
                //Row 1
                jCombo[0] = new JComboBox();
                jCombo[0].setModel(foodCategoryModel);
                jCombo[0].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        jComboActionPerformed(0);
                    }            
                });
                jCombo[1] = new JComboBox();
                jCombo[1].setModel(categoryTypeModel);
    
                //Row 2
                jCombo[2] = new JComboBox();
                jCombo[2].setModel(foodCategoryModel);
                jCombo[2].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        jComboActionPerformed(2);
                    }            
                });
                jCombo[3] = new JComboBox();
                jCombo[3].setModel(categoryTypeModel);
                
                setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                setTitle("Add Invoice");
            
                //Create GUI Layout
                GroupLayout layout = new GroupLayout(getContentPane());
                getContentPane().setLayout(layout);
    
                //Horizontal Group
                layout.setHorizontalGroup(
                    layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(jCombo[0], GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCombo[2], GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(jCombo[1], GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCombo[3], GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))
                    .addContainerGap()));
                
                //Vertical Group
                layout.setVerticalGroup(
                    layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(jCombo[0])
                            .addComponent(jCombo[1]))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(jCombo[2])
                            .addComponent(jCombo[3]))
                    .addContainerGap()));
                
                pack();
                setLocationRelativeTo(null);
            }
            
            private void jComboActionPerformed(int i) {
                Object foodType = jCombo[i].getSelectedItem();
                String foodTypeString = (String) foodType;
                for (FoodCategory foodCategory : foodCategorySet) {
                    if (foodTypeString.equals(foodCategory.getFoodType())) {
                        MyComboBoxModel accountModel = 
                                new MyComboBoxModel(foodCategory.getTypeList(), 1);
                        jCombo[i + 1].setModel(accountModel);
                    }
                }
                
            }
        }
        
        private static Set<FoodCategory> foodCategorySet = new HashSet<FoodCategory>();
        private static Set<CategoryType> categoryTypeSet = new HashSet<CategoryType>(); 
        
        public static void createFoodCategory(String foodType) {
            FoodCategory foodCategory = new FoodCategory(foodType);
            foodCategorySet.add(foodCategory);
        }
        
        public static void createCategoryType(String foodName, String foodType) {
            for (FoodCategory foodCategory : foodCategorySet) {
                if (foodType.equals(foodCategory.getFoodType())) {
                    foodCategory.addCategoryType(foodName);
                }
            }
        }
        
        public static void main(String[] args) {
            MarketGUI gui = new MarketGUI();
            gui.setVisible(true);
            
        }
    
        private static class CategoryType {
            private String foodName;
            public CategoryType(String foodName) {this.foodName = foodName;}
            public String getFoodName() {return foodName;}
        }
    
        private static class FoodCategory {
    
            private String foodType;
            private Set<CategoryType> categoryTypeSet = new HashSet<CategoryType>();
            public FoodCategory(String foodType) {this.foodType = foodType;}
    
            public Set getTypeList() {return categoryTypeSet;}
            
            public String getFoodType(){return foodType;}
    
            private void addCategoryType(String foodName) {
                CategoryType categoryType = new CategoryType(foodName);
                categoryTypeSet.add(categoryType);
            }
        }
    
        private static class MyComboBoxModel extends AbstractListModel implements ComboBoxModel {
    
            private Object selectedItem;
            private ArrayList anArrayList = new ArrayList();
    
            public MyComboBoxModel(Set<FoodCategory> foodCategorySet) {
                for (FoodCategory foodCategory : foodCategorySet){
                    anArrayList.add(foodCategory.getFoodType());
                }
            }        
            public MyComboBoxModel(Set<CategoryType> categoryTypeSet, int i) {
                for (CategoryType categoryType : categoryTypeSet){
                    anArrayList.add(categoryType.getFoodName());
                }
            }
            public Object getSelectedItem() {return selectedItem;}
            public void setSelectedItem(Object newValue) {selectedItem = newValue;}
    
            public int getSize() {return anArrayList.size();}
    
            public Object getElementAt(int i) {return anArrayList.get(i);}
    
        }
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: JComboBox false duplication

    When I execute the code I see 2 rows of CB with 2 CB on each row.
    Can you describe the steps the user should take to see the problem?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    rookiecoder is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Re: JComboBox false duplication

    Yes, of course. To quickly label which I am talking about, C1R1 stands for Column1Row1 so the upper most left of the 4, C2R1 = upper most right, C1R2 = lower left, C2R2 = lower right.

    1.) If a selection is made for C1R1 for "Fruit", "Fruit" is displayed in both C1R1 and C1R2. This should not happen. It should only show up in C1R1.
    2.) When "Fruit" in this example is chosen, C2R1 offers the selection of "Apple" or "Banana". This is correct. C2R2, however displays nothing even though C1R2 contains "Fruit"
    3.) When actually selecting "Fruit" in C1R2, C2R2 offers the selection "Apple" or "Banana". This is correct. However, when attempting to switch C1R1 to "Vegetable" either the selection will force "Fruit" and be unchangable, or "Vegetable" is again duplicated into C1R2, while C2R2 remains "Apple"/"Banana". In the second case C2R1 displays "Carrot", "Cabbage" correctly and actually selection "Vegetable" in C1R2 will display "Carrot"/"Cabbage".
    4.) Attempting to switch either C1R1 or C1R2 produces random results of suddenly not allowing the alternative selection for several tries.
    5.) C1R1 always replicates the display to C1R2. The reverse never happens.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: JComboBox false duplication

    Quote Originally Posted by rookiecoder View Post
    1.) If a selection is made for C1R1 for "Fruit", "Fruit" is displayed in both C1R1 and C1R2. This should not happen. It should only show up in C1R1.
    Then don't share the same model between the two combos. Give each one a separate instance of the model.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: JComboBox false duplication

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    rookiecoder is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Re: JComboBox false duplication

    ... I... wow... I feel really dumb now. Thank you, that fixed it completely.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: JComboBox false duplication

    No reason to feel dumb. It can take time to get acquainted with the MVC paradigm: state and data generally reside in the model; the view both mirrors the model, and updates it following interactive change.

    Intentionally sharing a model is a very good way to keep discrete UI elements in sync. That's something you've learned from this mistake that might have taken a lot longer to learn if you had done the right thing first time around :)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Compare two Arrays, checking for duplication
    By Redefine12 in forum New To Java
    Replies: 4
    Last Post: 02-03-2012, 09:57 PM
  2. Set TexField to false
    By globo in forum New To Java
    Replies: 3
    Last Post: 11-18-2010, 08:46 PM
  3. Checking if 2 things are false
    By ile4 in forum New To Java
    Replies: 4
    Last Post: 11-16-2010, 10:40 AM
  4. swirling becomes false,why?
    By arefeh in forum New To Java
    Replies: 2
    Last Post: 01-18-2010, 06:12 PM
  5. Replies: 2
    Last Post: 12-10-2009, 01:58 PM

Tags for this Thread

Posting Permissions

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