- Your JComboBox appears to be holding String objects that correspond to each Person's name, i.e.,
JComboBox<String>.
- Instead, the JComboBox should hold a collection Person objects,
JComboBox<Person>.
- You can control how the Person object is displayed by giving it a clean
toString() method that returns the Person's name.
- Even better would be to give the JComboBox a custom cell renderer that properly display's the Person's name.
- Give the JComboBox an ActionListener to listen for the user's selection, get the selected object via the JComboBox's
getSelectedItem(), and then call the appropriate setter method to set the selected Person's boolean field.
You state:
I know how to make a method to change each object, but I'd have to make six methods, one which specifically refers to each specific object.
Not so. A single method would work just fine, one that can work with any number of Person objects.
If any of my suggestions are unclear, please let me know via a comment. If you've got more specific questions, please edit your original question, but also consider posting pertinent code as well.
For example:
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class PersonTest {
protected static final Color SELECTED_COLOR = Color.red;
@SuppressWarnings("serial")
private static void createAndShowGui() {
Person[] people = { new Person("Duck", "Donald"),
new Person("Mouse", "Mickey"), new Person("Trump", "Donald"),
new Person("Bunny", "Easter"), new Person("Claus", "Santa"),
new Person("Kringle", "Chris")};
final JComboBox<Person> combo = new JComboBox<>(people);
final JList<Person> personJList = new JList<>(people);
combo.setSelectedIndex(-1);
JPanel mainPanel = new JPanel();
mainPanel.add(new JScrollPane(combo));
mainPanel.add(new JScrollPane(personJList));
combo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
Person selectedPerson = (Person) combo.getSelectedItem();
selectedPerson.setCheck(!selectedPerson.isCheck());
personJList.repaint();
}
});
personJList.setCellRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list,
Object value, int index, boolean isSelected, boolean cellHasFocus) {
Person person = (Person) value;
Component component = super.getListCellRendererComponent(list,
value, index, isSelected, cellHasFocus);
if (person != null) {
if (person.isCheck()) {
component.setForeground(SELECTED_COLOR);
} else {
component.setForeground(null);
}
}
return component;
}
});
JFrame frame = new JFrame("PersonTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class Person {
private String firstName;
private String lastName;
private boolean check;
public Person(String lastName, String firstName) {
this.lastName = lastName;
this.firstName = firstName;
this.check = false;
}
public boolean isCheck() {
return check;
}
public void setCheck(boolean check) {
this.check = check;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
@Override
public String toString() {
return String.format("%s, %s", lastName, firstName);
}
}