简体   繁体   English

Java Jlist不更新

[英]Java Jlist doesn't update

I have a JList in a large gui, I cut out all other code not relevant. 我在一个大gui中有一个JList,我剪掉了所有其他无关的代码。 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. 我读过的所有内容都说要使用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);    
    }
}

With your current code you're not updating the JList's list model but you're creating a new JList. 使用当前代码,您不会更新JList的列表模型,但正在创建一个新的JList。 Then you just assign this new JList to the allFileList member which has no effect on the panel. 然后,您只需将此新的JList分配给allFileList成员即可,该成员对面板没有影响。 The JList instance, that is displayed on the panel, is not affected by this operation. 面板上显示的JList实例不受此操作的影响。

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) (当然,您必须使用默认列表模型而不是数组创建JList)

You should not reinitialize the JList and DefaultListModel every time an element is added, you will lose the old elements. 您不应在每次添加元素时都重新初始化JListDefaultListModel ,否则会丢失旧元素。 Keep a refrence to the DefaltListModel and just call addElement() , it will automatically update the list by listening to events, generated by the model. 保留对DefaltListModel ,只需调用addElement() ,它将通过侦听由模型生成的事件来自动更新列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM