hi. I have a jlist on a frame. I want to add items dynamically in the list. So, I am using DefaultListModel..Here's my code:

public class AdminFrame extends javax.swing.JFrame {
    private Statement S;
    private ResultSet R;

    DefaultListModel model = new DefaultListModel();
    JList Employee_list = new JList(model);

/** Creates new form AdminFrame */
    public AdminFrame() {
        initComponents();
        MySQLConnector dbc = new MySQLConnector();
        S = dbc.connectdb();

Employee_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        Employee_list.setSelectedIndex(0);
        Employee_list.addListSelectionListener((ListSelectionListener) this);
        on_load();
    }

I want to get the item selected in the list. How do I do that?
Thanks...

Recommended Answers

All 3 Replies

What's wrong with getSelectedValue() ?

hi. well, nothing wrong with getselecteditem()....jst dint knw where to write that statement...
But hey, it works now.
However, I think its invoking the listener twice.
my code:

private class ListListener implements ListSelectionListener {
        @Override
        public void valueChanged(ListSelectionEvent e) {
             String emp_name = Employee_list.getSelectedValue().toString();
            
        }
    }//class ListListener ends

emp_name is printed twice.
why?

Without seeing all the code, my best answer is "don't know". Sorry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.