Despite a lot of research, I cannot find the answer or decide how to get the selected text element inside the JList for the variable. So I would help a little. I tried to select the index of the selected item and delete the items using this code, and this works fine, but as I wrote, I want the selected text to change after clicking the button. Thanks!
int index = list.getSelectedIndex(); model.removeElementAt(index);
Parts of my JList code:
model = new DefaultListModel(); list = new JList(model); list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); JScrollPane listScroller = new JScrollPane(list); listScroller.setPreferredSize(new Dimension(430, 60));
Parts of my actionlistener code:
// Select customer if(event.getSource() == buttonSelectCustomer){ int index = list.getSelectedIndex(); // Just for test model.removeElementAt(index); // Just for test int number = model.getSize(); // Just for test //String selectedText = list.getSelectedValue(); // Not working! }
source share