简体   繁体   English

Java JList问题

[英]Java JList problems

Ok what am trying to do is allow the user to make a list for themselves, what ever they type in in the TextField the output of that will be shown in the Jlist but my problem here is that if i type in another word to the TextField the output of that is either appending or replacing the other word that was already there it suppose to go beneath the other word and save there can anyone help me please?? 好吧,我想做的是允许用户自己创建一个列表,无论他们在TextField中键入什么内容,其输出都将显示在Jlist中,但是我的问题是,如果我在TextField中键入另一个单词输出的结果是追加或替换了已经存在的另一个单词,它应该放在另一个单词下面并保存在那里,任何人都可以帮我吗?

    public lala(){

    b2 = new JButton("ADD");
    b2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        model.removeAllElements();
        list1.setModel(model);

       }
    });

    b3 = new JButton("MOVE");
    b3.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
          model = new DefaultListModel<String>(); 
          model.addElement(field.getText());
          list.setModel(model);
          field.setText("");

        }
    });

    list = new JList<String>();
    list.setFixedCellHeight(10);
    list.setFixedCellWidth(10);
    list.setVisibleRowCount(10);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    scroll = new JScrollPane(list);
    scroll.setPreferredSize(new Dimension(100,100));

    field = new JTextField(19);
    field.setToolTipText("Input Text Area Here");
    field.setFont(new Font("Corier",Font.BOLD,20));
    field.setBackground(Color.BLACK);
    field.setForeground(Color.RED);
    field.setDragEnabled(true);

    panel = new JPanel();
    panel.setBackground(Color.BLACK);

    panel.add(b3);
    //panel.add(b2);
    panel.add(field);
    panel.add(scroll);
    add(panel);

      } 
    }

Your issue is that you are creating a whole new DefaultListModel on each Action in the event listener. 您的问题是,您正在事件侦听器中的每个Action上创建一个全新的DefaultListModel

You need to declare a global DefaultListModel and addElement() to it as your user presses the button. 您需要在用户按下按钮时声明一个全局DefaultListModel并为其添加addElement()

This might be able to help point you in the right direction: 这可能会帮助您指出正确的方向:

public class Examples {

    private DefaultListModel modelList;
    private JList list;
    private JButton button;
    private JTextField field;

    public Examples() {
        modelList = new DefaultListModel();
        list = new JList(modelList);
        button = new JButton("Add To List");
        field = new JTextField();
        init();
    }

    private void init() {
        button.addActionListener((ActionEvent e) -> {
            modelList.addElement(field.getText());
            // !! list.setModel(modelList);
            field.setText("");
        });
    }

}

Here, we have registered our DefaultListModel as a instance field in our Examples class. 在这里,我们已将DefaultListModel注册为Examples类中的实例字段。

Then we register a new listener using the lambda expression, and have the modelList updated with the field's text, and set the modelList as the model for the JList. 然后,我们使用lambda表达式注册一个新的侦听器,并使用字段的文本更新modelList,并将modelList设置为JList的模型。

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

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