简体   繁体   中英

How to update jList when other jList is selected

I am trying to make a jList which should update when another item is selected on a different jList . When a property is selected from lstProperty then lstLandLord dispalys the landlord but I want to be able to update it without having to press a button. Can anyone help with this code?

Tried:

Implementing MouseListener on a jList

Here is the code for the method that updates the list

    public void showLandLordFromProperty() throws SQLException{        
    conn = SingletonDatabase.connectDatabase();

    Property p;
    p = (Property) lstProperty.getSelectedValue();

    int landlord_id = p.getLandlord_id();
    LandLord l = new LandLord();
    String sql = ("SELECT * FROM LANDLORD WHERE LANDLORD_ID =" + landlord_id); 
    PreparedStatement statement = conn.prepareStatement(sql);
    ResultSet rs = statement.executeQuery();

     if(rs.next()){
         String forename = rs.getString("FORENAME");
         String surname = rs.getString("SURNAME");
        displayLandLords.addElement(l.getName(forename, surname));          
     }
     lstLandLord.setModel(displayLandLords);

}

Here is the action listener

  lstLandLord.addMouseListener(new MouseAdapter(){
        @Override
        public void mouseClicked(MouseEvent e){
                try {
        showLandLordFromProperty();
    } catch (SQLException ex) {
        Logger.getLogger(HomePage.class.getName()).log(Level.SEVERE, null, ex);
    }

"I am trying to make a jList which should update when another item is selected on a different jList. "

You should not be trying to use a MouseListener . The correct listener to use, as Sean Bright noted is the ListSelectionListener .

the list fires list selection events whenever the selection changes. You can process these events by adding a ListSelectionListener to the list with the addListSelectionListener method. A list selection listener must implement one method: valueChanged

A simple implementation can be tested below.

You can also see more at How to Use Lists and How to Write ListSelectionListeners

import java.awt.Dimension;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class DynamicList {

    public DynamicList() {
        JList list1 = createList();
        final JList list2 = new JList(new DefaultListModel());
        list1.addListSelectionListener(new ListSelectionListener(){
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting()) {
                    JList list = (JList)e.getSource();
                    String item = (String)list.getSelectedValue();
                    DefaultListModel model = (DefaultListModel)list2.getModel();
                    model.addElement(item);
                }
            }  
        });
        JPanel panel = new JPanel();
        JScrollPane pane1 = new JScrollPane(list1);
        pane1.setPreferredSize(new Dimension(150, 200));
        JScrollPane pane2 = new JScrollPane(list2);
        pane2.setPreferredSize(new Dimension(150, 200));
        panel.add(pane1);
        panel.add(pane2);
        JFrame frame = new JFrame("List");
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new DynamicList();
            }
        });
    }

    private JList createList() {
        DefaultListModel<String> model = new DefaultListModel<>();
        JList list = new JList(model);
        for (int i = 0; i < 15; i++) {
            model.addElement("Hello" + i);
        }
        return list;
    }
}

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