Question

I have a JList in a large gui, I cut out all other code not relevant. I can initialize the list but I can not add/remove items from it when I use a button. Everything I have read says to use a DefaultListModel. Then it will automatically update the list when you re initialize it.
I have also tried refocusing and repainting the panels and frame and that doesn't work either.

public static void main(String[] args) {
        Play worker=new Play();
        worker.GUI();
    }

    public void GUI(){
        frame=new JFrame();
        mainPanel=new JPanel(new GridBagLayout());
        filePanel=new JPanel(new GridBagLayout());
        allFilePanel=new JPanel(new GridBagLayout());
        fileInputLabel=new JLabel("Enter the directory of the csv file:");
        fileNameField=new JTextField(25);

        readFileButton=new JButton("Read File");
        ReadFileButtonListener buttonListener=new ReadFileButtonListener();
        readFileButton.addActionListener(buttonListener);


        addItem(filePanel,fileInputLabel , 0, 0, 1, 1, GridBagConstraints.CENTER);
        addItem(filePanel,fileNameField , 1, 0, 2, 1, GridBagConstraints.CENTER);
        addItem(filePanel,readFileButton , 3, 0, 1, 1, GridBagConstraints.CENTER);

        allFileLabel=new JLabel("List of all Data");    
        String [] name={"joe"};
        allFileList=new JList<String>(name);
        allFileList.setVisibleRowCount(10);


        addItem(allFilePanel,allFileLabel , 0, 0, 1, 1, GridBagConstraints.CENTER);
        addItem(allFilePanel,allFileList , 0, 1, 1, 1, GridBagConstraints.CENTER);

        addItem(mainPanel, filePanel, 0, 0, 4, 1, GridBagConstraints.CENTER);
        addItem(mainPanel, allFilePanel, 1, 2, 1, 1, GridBagConstraints.CENTER);

        frame.setSize(1000,800);
        frame.add(mainPanel);
        frame.setVisible(true); 
    }

    /**configures the panels
     * @param p
     * @param c
     * @param x
     * @param y
     * @param width
     * @param height
     * @param align
     */
    private void addItem(JPanel p,JComponent c, int x, int y , int width,int height, int align){
        GridBagConstraints gc=new GridBagConstraints();
        gc.gridx=x;
        gc.gridy=y;
        gc.gridwidth=width;
        gc.gridheight=height;
        gc.weightx=100;
        gc.weighty=100;
        gc.insets=new Insets(5,5,5,5);
        gc.fill=GridBagConstraints.NONE;
        p.add(c,gc);
    }

        private class ReadFileButtonListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent arg0) {
            generateAllFileList();
        }

    }

    public void generateAllFileList(){
        DefaultListModel<String> list=new DefaultListModel<String>();
        list.addElement("Fred");
        allFileList=new JList<String>(list);    
    }
}

No correct solution

OTHER TIPS

With your current code you're not updating the JList's list model but you're creating a new JList. Then you just assign this new JList to the allFileList member which has no effect on the panel. The JList instance, that is displayed on the panel, is not affected by this operation.

Roughly spoken (and untested), it should go like this:

public void generateAllFileList(){
    DefaultListModel<String> list = allFileList.getModel();
    list.addElement("Fred"); 
}

(and, of course, you'll have to create the JList with a default list model instead of an array)

You should not reinitialize the JList and DefaultListModel every time an element is added, you will lose the old elements. Keep a refrence to the DefaltListModel and just call addElement(), it will automatically update the list by listening to events, generated by the model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top