Prevent crash if button is clicked before selection in DefaultListModel?

105 views Asked by At

I'm using a DefaultListModel to select different names in a list. To select a name the user click on one of the name in the list so it get highlited and then confirm the selected name by pressing a button. My problem is if the user click on the button before a name is selected in the list, then it crash!

So far I have used this code, but it's not enough to prevent an crash

if(event.getSource() == buttonSelectCustomer && model1.getSize() > 0)

Is there a way to prevent a crash if user click the button without any selection? Thanks!

1

There are 1 answers

0
tenorsax On BEST ANSWER

Add this to you condition (list.getSelectedIndex() != -1)