简体   繁体   中英

adding multiple items on jlist netbeans

I use netbeans. I want add only one item but it add multiple times. When I push button, JList is like this:

item1
item2
item2
item3
item3
item3

private void jAddItemActionPerformed(java.awt.event.ActionEvent evt) {                                         

    frame.setSize(200, 300);
    jList.setModel(listmodel);
    textarea.setSize(100, 50);
    textarea.setLocation(50, 20);
    frame.add(textarea);
    addButton.setSize(100,50);
    addButton.setLocation(50, 150);

    frame.add(addButton);
    frame.setVisible(true);

    addButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {                               
                            listmodel.addElement(textarea.getText(););
                            frame.dispose();
        }
    });
  textarea.setText("");
} 

I guess you should call:

listmodel.set(0, textarea.getText()) 

instead of addElement.

And somewhere before have this call (once):

listmodel.setSize(1)

I guess GUI code generation with Netbeans is good only for simple projects.

Nevertheless please read http://www.sscce.org/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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