0

I have the following method.

           DefaultListModel getModelForCabin(Cabin cabin) {

 List<Camper> listAdded= new ArrayList<Camper>(getOrCreateGroup(cabin));

 DefaultListModel<Camper> dfm= new DefaultListModel<Camper>();
    for(Camper c: listAdded){
        if(!dfm.contains(c)){
            dfm.addElement(c);
        }

    }
    //System.out.println(listAdded);
    //System.out.println(dfm);

    return dfm;
}

Then, I set this method inside the JList like this...

 JList list = new JList(getModelForCabin((Cabin)comboBox.getSelectedItem()));
scrollPane_1.setViewportView(list);

In the method, if I print the dfm and listAdded as shown in the system print line, it shows the both.

If I type this...

System.out.println(getModelForCabin((Cabin)comboBox.getSelectedItem());   

It prints out the model as well.

What it will not do though, is add the model to the JList. I've tried changing around the code, deleting the JList and making a new one, and rearranging the code.

No matter what I do, it will not work.

So my list prints fine, my DefaultListModel prints fine, my HashMap that prints the Cabin and Campers works fine, but the JList will not print the model.

Added:

     JScrollPane scrollPane_1 = new JScrollPane();
    scrollPane_1.setBounds(361, 205, 296, 339);
    getContentPane().add(scrollPane_1);

     list = new JList(getModelForCabin((Cabin)comboBox.getSelectedItem()));
    scrollPane_1.setViewportView(list);
Nathan777
  • 79
  • 8
  • 2
    I would verify that the `getModelForCabin` is actually adding elements to the `DefaultListModel`. Failing that, consider providing a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – MadProgrammer Apr 30 '18 at 22:43
  • 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! .. – Andrew Thompson May 01 '18 at 01:54
  • .. 3) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson May 01 '18 at 01:54
  • I already answered that question that it is adding elements in the post though. I printed the default list model to the screen in plain text, and it shows it, but on the JList panel, it stays blank. – Nathan777 May 01 '18 at 06:44
  • Okay, I edited the question to include as little code as possible to know what the question is. The model and the Array are working just fine. The method works fine and returns the right result. However, the JList simply will not print it. – Nathan777 May 01 '18 at 08:39
  • Where and how do you use the scrollPane_1? Show us the code that adds it to the dialog (or whatever you add it to). – Paco Abato May 01 '18 at 08:41
  • I just added it at the bottom. – Nathan777 May 01 '18 at 10:16
  • I'm wondering if maybe a JList cannot reference a local list. – Nathan777 May 01 '18 at 12:17

1 Answers1

0

I figured out the problem was because I wasn't setting the model. If the model is a local DefaultListModel, it has to be set in the main class using the setModel method.

I needed to reference the JList, then do the setModel like this....

            list.setModel(getModelForCabin((Cabin)comboBox.getSelectedItem()));

Where list is the variable of the JList, and getModelForCabin(Cabin cabin) is the method which returns the DefaultListModel.

It was added to both the action listener for the add button, as well as a refresh button that was created when opening the class.

Nathan777
  • 79
  • 8