When adding class in DefaultListModel, did it save the value of toString or the entire class?

StackOverflow https://stackoverflow.com/questions/21811368

Domanda

Newbie here.

When I added an element in the DefaultListModel, I used a class with an overriden toString. Based on the sample code below, I want to display the selected item's ID when I click the button btnid.

The commands under displayID doesn't seem to work. Help please. Thanks!

class SomeClass {
JFrame f = new JFrame("Sample");
JScrollPane sp = new JScrollPane();
DefaultListModel dlm = new DefaultListModel();
JList lst = new JList(dlm);

public SomeClass() {
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton btnadd = new JButton("Add");
    JButton btnid = new JButton("View ID");

    Container p = f.getContentPane();
    sp.getViewport().add(lst,null);    

    p.add(sp, BorderLayout.WEST);
    p.add(btnadd, BorderLayout.EAST);
    p.add(btnid, BorderLayout.SOUTH);

    btnadd.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
          dlm.addElement(new ElementDisplay(dlm.getSize(),"Element " + dlm.getSize()));  
      }
    });

    btnid.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
          displayID();
      }
    });

  f.pack();
  f.setVisible(true);    
 }

private void displayID() {
    ElementDisplay ed;

    ed = dlm.getElementAt(lst.getSelectedIndex());    

    System.out.println(ed.elementID);
}

public static void main(String args[]) {
  SomeClass sc = new SomeClass();
}


class ElementDisplay {
    public int elementID;
    private String elementDescription;

    public ElementDisplay(int pid, String pdesc) {
        elementID=pid;     
        elementDescription=pdesc;
    }

    @Override
    public String toString() {
        return elementDescription;
    }
}
}
È stato utile?

Soluzione

Works fine for me. What makes you think it doesn't work? You need to actually have an item selected in the list for the button press to work, you will get ArrayIndexOutOfBoundException

Instead of depending on a button press, just add a listener to the JList. That way only when the item in the JList is selected, does it print. No need for the button and trying to avoid the ArrayIndexOutOfBoundException

 lst.addListSelectionListener(new ListSelectionListener() {

     @Override
     public void valueChanged(ListSelectionEvent e) {
         if (e.getValueIsAdjusting()) {
              JList list = (JList)e.getSource();
              DefaultListModel model = (DefaultListModel)list.getModel();
              ElementDisplay ed = (ElementDisplay) model.getElementAt(lst.getSelectedIndex());
              System.out.println(ed.elementID);
         }
     }
 });

See How to Write Event Listeners where you will run into possible listeners you can use for different components. As GUIs are event driven, you should take time to learn most of them.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top