user3027875
user3027875

Reputation: 101

Java JList.getSelectedIndex() always returns -1

This is an excerpt from the whole class, basically I've declared a JList and a DefaultListModel in the class and create a JFrame that holds some buttons and a listbox. When roomsButton is clicked, the listbox will be populated with some options by the hotel.displayRoomsAvailable() function and leftButton will be set to visible and assigned functionality. This listbox is being populated properly with the returns from the function, however, when I click leftButton, the list.getSelectedIndex() always returns -1, I've tried using getSelectedValue(), but it is returning null. What am I doing wrong that isn't recognizing the proper value selected in the list. There's only 2 values in the list, so I don't think it has anything to do with the ScrollPane or being visible, etc

private JList<String> list;
private static DefaultListModel listModel;

listModel = new DefaultListModel();

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel cusPanel = new JPanel();
list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setPreferredSize(new Dimension(400, 200));
cusPanel.add(list);
frame.add(cusPanel, BorderLayout.CENTER);

JButton roomsButton = new JButton("Display Rooms");
roomsButton.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent event)
    {
        listModel.clear();
        String startd = start.getText();
        String endd = end.getText();
        LocalDate startdate = LocalDate.parse(startd, DateTimeFormat.forPattern("MM/dd/yyyy"));
        LocalDate enddate = LocalDate.parse(endd, DateTimeFormat.forPattern("MM/dd/yyyy"));
        hotel.displayRoomsAvailable(startdate,enddate);
        list = new JList(listModel);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setPreferredSize(new Dimension(400, 200));
        leftButton.setText("Reserve Room");
        leftButton.setVisible(true);
        leftButton.addActionListener(new ActionListener()
        {
           public void actionPerformed(ActionEvent event)
           {                
               System.out.println(list.getSelectedIndex());
               //hotel.bookRoom(true, ID, list.getSelectedValue(), startdate, enddate);
           }
        });
    }
});

Upvotes: 0

Views: 557

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

You're creating a new instance of JList in your `actionPerformed method, but never add it to the screen, therefore, it can never be selected...

 public void actionPerformed(ActionEvent event)
    {
        //...
        list = new JList(listModel);
        //...
        leftButton.addActionListener(new ActionListener()
        {
           public void actionPerformed(ActionEvent event)
           {                
               System.out.println(list.getSelectedIndex());
               //hotel.bookRoom(true, ID, list.getSelectedValue(), startdate, enddate);
           }
        });
    }

And frankly, I can't see why you would...

Upvotes: 1

Related Questions