Search code examples
javadatabasejcombobox

JComboBox Help to delete data from database


I have a table in database employee composed by two columns ID and NameLastName.

I arrived to add data in the second column to a JComboBox like in the snapshot down!

enter image description here

Now how can I do to delete the selected employee in the JComboBox from DB?

I thought to add the ID with name like this I122-Name and use split method to extract the ID but I don't want to show the ID.

Is there any way to associate with each name in the JComboBox a hidden value that contains the employee ID?


Solution

  • I don't want to show the ID.

    This question has already received 2 good answers, but I wanted to add this 3rd one if only to address the question of whether to show the ID (which was not part of the question, but should have been).

    Screen Shot

    Which John Smith are you going to fire?

    enter image description here

    SackEmployee.java

    import java.awt.*;
    import javax.swing.*;
    
    class SackEmployee {
        public static void main(String[] args) {
            SwingUtilities.invokeLater( new Runnable() {
                public void run() {
                    Employee[] workforce = {
                        new Employee("Ali Ben Messaoud", 9823),
                        new Employee("Jane Brewer", 6348),
                        new Employee("John Smith", 1247),
                        new Employee("John Smith", 4385)
                    };
    
                    JComboBox employeeCombo = new JComboBox(workforce);
    
                    EmployeeCellRenderer employeeCellRenderer = new EmployeeCellRenderer();
                    employeeCombo.setRenderer(employeeCellRenderer);
    
                    int result = JOptionPane.showConfirmDialog(
                        null,
                        employeeCombo,
                        "Fire Employee?",
                        JOptionPane.OK_CANCEL_OPTION);
                    // cast selected item back to Employee.
                    Employee employee = (Employee)employeeCombo.getSelectedItem();
                    System.out.println( "Fire '" + employee + "' now?" );
                    System.out.println( "Proceed: " + (result==JOptionPane.OK_OPTION) );
                }
            });
        }
    }
    
    class Employee {
    
        int id;
        String name;
    
        Employee(String name, int id) {
            this.id = id;
            this.name = name;
        }
    
        public String getIdString() {
            return "ID-" + id;
        }
    
        public String toString() {
            return getIdString() + ": " + name;
        }
    }
    
    class EmployeeCellRenderer implements ListCellRenderer {
    
        JLabel label = new JLabel();
    
        public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
    
            Employee employee = (Employee)value;
            // distinguish between people of same name by adding ID.
            label.setText(employee.name + " (" + employee.getIdString() + ")");
    
            return label;
        }
    }
    

    E.G. I/O

    prompt>java SackEmployee
    Fire 'ID-9823: Ali Ben Messaoud' now?
    Proceed: false
    
    prompt>java SackEmployee
    Fire 'ID-1247: John Smith' now?
    Proceed: true
    
    prompt>java SackEmployee
    Fire 'ID-4385: John Smith' now?
    Proceed: false
    
    prompt>