I create variables at the beginning of the class:

int currentItem; String[] idNumbers; JComboBox combo = new JComboBox(); ArrayList<Employee> list = new ArrayList<Employee>(); 

Further, since the id generated by itself, I use the method that updates the ID when the employee data is deleted or a “new employee” is created.

 public void updateIDNumbers() { idNumbers = new String[list.size()]; int i = 0; for (Employee e : list) { idNumbers[i++] = e.getId() + ""; } combo.setModel(new DefaultComboBoxModel(idNumbers)); } 

There is also a simple method that displays updated data.

 public void displayDetails(int currentItem) { idTextField.setText(list.get(currentItem).getId() + ""); ppsTextField.setText(list.get(currentItem).getPps()); nameTextField.setText(list.get(currentItem).getName()); surnameTextField.setText(list.get(currentItem).getSurname()); genderTextField.setText(list.get(currentItem).getGender() + ""); depTextField.setText(list.get(currentItem).getDep()); salaryTextField.setText(list.get(currentItem).getSalary() + ""); fulltimeTextField.setText(list.get(currentItem).getFulltime() + ""); } 

And now I call the method that should open the dialog and call to select id , and then display on the employee page with the selected id .

 byIdItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (modifyItemButton.getText().equals("Save") ) { // Here we make sure that any updated values are saved to the record before // we display the next record. // This behaviour is performed by next, prev and edit, so we move it into a // separate method so as to avoid unnecessary repetition of code. saveOpenValues(); } updateIDNumbers(); String[] options = { "OK", "Cancel"}; String title = "Search an ID"; int selection = JOptionPane.showOptionDialog(null, combo, title, JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]); if (selection > 0) { System.out.println("selection is: " + options[selection]); } // currentItem = ((JComboBox)(e.getSource())).getSelectedIndex(); currentItem = (Integer) combo.getSelectedItem(); if (currentItem != 0) { displayDetails(currentItem); System.out.println("hey"); } } }); 

The error is quite understandable - Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer however Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer However, I can’t figure out how to convert a string to an integer , tried different options .. . Thank you in advance.

    1 answer 1

    You transferred an array of strings to a combo box:

     String[] idNumbers; ... combo.setModel(new DefaultComboBoxModel(idNumbers)); 

    The getSelectedItem() method returns the selected item, which is a string (some string from the passed array). How do you want to get a number from it? If you need the number of the selected item, use combo.getSelectedIndex(); if in the string array you have string representations of numbers (such as "123"), then use Integer.parseInt(combo.getSelectedItem())