Ok so lets say I have any GUI that needs its contents changed while it is running, so that it will re-display the new contents. An example would be in my defaultlistmodel it would have:

Exactors Diggers

But once the user selects "Exactors" and moves to a new GUI which is displayed such as

Mini
Cars

And then goes back and selects Diggers - both Cheese and Beans remain in every further category that i add but i want each category to have its own seperate subCategories

Hi there,

Please post your code, screen shots if necessary, and the process you are currently employing that isn't working for you.

We all want to help but we need you to be clearer otherwise we won't be able to/

Good luck!

When running my program, My first GUI comes up which contains a jlist and a textfield which the user can add categories or select categories that from that Jlist, so if they add

Exactors
Diggers,

and if they select one of them ie Exactors - it would then bring up another GUI which has another Jlist and text field - which should represent Exactors subcategories

So the hierarch should be
Exactors
-- now its subCategories
Digger
-- now its subcategories

but whenever I add subcategories - those same subcategories remain in the list regardles of what I select from the category - im unsure on how i can change it so that each category would have its own subcategories

and why do you think that someone from us is mentalist and see over your arms to your screen

map<String, Item> map  = new HashMap<String, Item>();
        public String selected;
        private ItemList categoryList;
    
    // A variable to represent the "current list" being processed
    private ItemList currentList;
    
    // A variable to represent the "current item" (in the "current list")
    // being processed
    private Item currentItem;
    private MySystem system;
   
    DefaultListModel CatsModel = new DefaultListModel();
    DefaultListModel SubCatModel = new DefaultListModel();
    DefaultListModel equipModel = new DefaultListModel();
    final JList list = new JList(CatsModel)


addCategory = new JButton("Add Category");
        addCategory.setToolTipText("To add a New Category to the list");
       addCategory.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                String name = addCategoryText.getText();
                addCategoryText.setText("");
                system.addCategory(name);
                CatsModel.addElement(name);
               ItemList temp = new ItemList(name);
            // Add the "category" (with this "title") to the (top-level) list of
            // "categories" in the hierarchy
            categoryList.addItem(temp);
            // Set "current item" appropriately, ie as this new "category"
            currentItem = temp;
            map.put(name, currentItem);
         }
        });
       
        buttonPanel.add(addCategory);
        
        selCategory = new JButton("Select Category");
        selCategory.setToolTipText("Select A Category from the list");
        selCategory.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
            selected = list.getSelectedValue().toString();
            system.setCurrentItem(list.getSelectedIndex());
            system.printCurrentList();
            system.printCurrentItem();
            system.advanceCurrentList();
            handleAddSubCategory();
            }
        });
        buttonPanel.add(selCategory);

My SubCategory Method

final JList sublist = new JList(SubCatModel);

 addSubCategory = new JButton("Add A SubCategory");
                addSubCategory.setToolTipText("To add a New SubCategory to the list");
                addSubCategory.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent event) {
                        // re-populate model sub
                         
                        map.containsKey(selected); // finding the values for the key. 
                        map.get(selected);  // getting the selected variable so that
                        String name = addSubCategoryText.getText();
                        addSubCategoryText.setText("");
                        system.addSubcategory(name);
                        SubCatModel.addElement(name);
                        map.put(name, currentItem);
        
                      }
                        });
                        buttonPanel.add(addSubCategory)

Edited 4 Years Ago by Ezzaral: Fixed code tags

It seems as if you are using the same object when adding subcategories. Hard to tell without seeing your code. Maybe you need to store the subcategories for each top level category and then when one is clicked, delete all items from the list and load the appropriate sub-categories.

Please post your code so we can help you easier!

Here is your code:

map<String, Item> map = new HashMap<String, Item>();
public String selected;
private ItemList categoryList;

// A variable to represent the "current list" being processed
private ItemList currentList;

// A variable to represent the "current item" (in the "current list")
// being processed
private Item currentItem;
private MySystem system;

DefaultListModel CatsModel = new DefaultListModel();
DefaultListModel SubCatModel = new DefaultListModel();
DefaultListModel equipModel = new DefaultListModel();
final JList list = new JList(CatsModel)


addCategory = new JButton("Add Category");
addCategory.setToolTipText("To add a New Category to the list");
addCategory.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String name = addCategoryText.getText();
addCategoryText.setText("");
system.addCategory(name);
CatsModel.addElement(name);
ItemList temp = new ItemList(name);
// Add the "category" (with this "title") to the (top-level) list of
// "categories" in the hierarchy
categoryList.addItem(temp);
// Set "current item" appropriately, ie as this new "category"
currentItem = temp;
map.put(name, currentItem);
}
});

buttonPanel.add(addCategory);

selCategory = new JButton("Select Category");
selCategory.setToolTipText("Select A Category from the list");
selCategory.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
selected = list.getSelectedValue().toString();
system.setCurrentItem(list.getSelectedIndex());
system.printCurrentList();
system.printCurrentItem();
system.advanceCurrentList();
handleAddSubCategory();
}
});
buttonPanel.add(selCategory);

My SubCategory Method

final JList sublist = new JList(SubCatModel);

addSubCategory = new JButton("Add A SubCategory");
addSubCategory.setToolTipText("To add a New SubCategory to the list");
addSubCategory.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// re-populate model sub

map.containsKey(selected); // finding the values for the key. 
map.get(selected); // getting the selected variable so that
String name = addSubCategoryText.getText();
addSubCategoryText.setText("");
system.addSubcategory(name);
SubCatModel.addElement(name);
map.put(name, currentItem);

}
});
buttonPanel.add(addSubCategory)
This article has been dead for over six months. Start a new discussion instead.