Java JList and list problems

蹲街弑〆低调 提交于 2020-01-07 06:06:48

问题


I added list items to JList

numbersList.add("first example");
numbersList.add("second example");
numbersList.add("third example");

and when I add new text or number to field it would would be added into list and showed at JList. Here my code, I just want to make that when I push button new element appear at JList fullList from list numberList which has been added from textfield numberTxtFld.

p.s i have removed some unnecessary code.

package average;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

public class Surface extends JLabel{
    private JTextField numberTxtFld;
    private JLabel topLabel;
    private JLabel sumLabel;
    private JLabel avgLabel;
    private JLabel maxLabel;
    private JLabel minLabel;
    private JLabel amountLabel;
    private JLabel listLabel;
    private JLabel resultLabel;
    private JLabel sumAnswerFld;
    private JLabel avgAnswerFld;
    private JLabel maxAnswerFld;
    private JLabel minAnswerFld;
    private JLabel amountAnswerFld;
    private JLabel fullListLabel;
    private JList fullList;
    final List<String> numbersList = new ArrayList<String>();
    private JButton submitBtn;
    private JButton closeBtn;
    private JPanel panel;

    private int arrayIndex = 0;// used for components adding into array

    public Surface(){
        panel = new JPanel();
        panel.setBackground(Color.WHITE);
        panel.setSize(300, 300);

        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;


        topLabel = new JLabel("Enter your number and push submit");
        gbc.gridwidth = 3;
        gbc.gridx = 0; // column 
        gbc.gridy = 0; // row
        panel.add(topLabel, gbc);
        gbc.gridwidth = 1; // setting grid column with to default

        fullListLabel = new JLabel("Full list");
        gbc.gridx = 2;
        gbc.gridy = 1;
        panel.add(fullListLabel, gbc);          

        amountLabel = new JLabel("Amount");
        gbc.gridx = 0;
        gbc.gridy = 5;
        panel.add(amountLabel, gbc);

        amountAnswerFld = new JLabel("0");
        gbc.gridx = 1;
        gbc.gridy = 5;
        panel.add(amountAnswerFld, gbc);



        numberTxtFld = new JTextField();
        numberTxtFld.setColumns(10);
        gbc.gridx = 0;
        gbc.gridy = 1;
        panel.add(numberTxtFld, gbc);       

        submitBtn = new JButton("Submit");
        gbc.gridx = 1;
        gbc.gridy = 1;
        submitBtn.addActionListener(
                new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent event) {

                        String text = numberTxtFld.getText();
                        numbersList.add(text);


                    }                   
                }
        );      
        panel.add(submitBtn, gbc);

        numbersList.add("first example");
        numbersList.add("second example");
        numbersList.add("third example");

        fullList = new JList(numbersList.toArray());
        fullList.setVisibleRowCount(6);
        fullList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        gbc.gridheight = 6;
        gbc.gridx = 2;
        gbc.gridy = 2;
        panel.add(new JScrollPane(fullList), gbc);
        gbc.gridheight = 1; // setting grid column with to default

        add(panel);


    }
}

回答1:


You will need to create a ListModel. As what you are doing is very simple you can use the DefaultListModel.

you will need to declare

private DefaultListModel defaultListModel;

and instantiate it

defaultListModel = new DefaultListModel();

then attach it to your JList in the JLists constructor

fullList = new JList(defaultListModel);

you will then need to add your items to the defaultListModel, probably in a loop

for (Object o : numbersList) {
   defaultListModel.addElement(o);
}

Finally in your action listener you now need to call the addElement method on the defaultListModel instead

String text = numberTxtFld.getText();
defaultListModel.add(text);

Then you should get everything to work as you desire plus all the update events are handled for you.




回答2:


When you do this:

fullList = new JList(numbersList.toArray());

You are not magically attaching the JList to numbersList, you are simply populating the JList with the content of numbersList at that point, which, of course, does not include any items added later when your button is pressed.

You have at least three options:

The most basic solution is to explicitly add a new item to fullList in your submitBtn action listener. You have a number of options. You can just add the new item to fullList from submitBtn, which is simple and may be adequate from your application. To do this, make sure the list's model is a DefaultListModel, then you can use ((DefaultListModel)getModel()).addElement() to add an item.

DefaultListModel model = new DefaultListModel();
fullList.setModel(model);

// to add an element (during initial population, and action listener):
model.addElement("the element");

A more robust solution would be to completely repopulate fullList based on the current contents of numbersList, and do that from your action listener to resync the GUI to the data. This may not be desirable for other reasons, e.g. it may be unacceptable to clear the list first because of its effect on the current item selection, etc. Depends on your requirements. You could use JList's setListData() for this.

// any time you want to repopulate the list:
fullList.setListData(numbersList.toArray());

But bear in mind that this sets the list model to a read-only model, and you will not be able to use getModel().addElement() later.

An even more robust, and the generally preferred, solution is to implement a ListModel that reflects the contents of numbersList and assign that ListModel to your JList. This can lead to very clean code that lets you perform any operation you want on your list while keeping the GUI and data in sync. Rather than repost code here, you can find a simple implementation in the answer to this question.

Hope that helps.



来源:https://stackoverflow.com/questions/18062334/java-jlist-and-list-problems

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!