I’m new to GUI design and I’m trying to plan this out before I go too far the wrong way, any help would be nice. I’m trying to display a JTable with rows of Employee, which itself has datatypes of String and ArrayList<Cert>. Cert contains a String.
I’d like to have the table present the data for editing, but for a few of the columns I’d like to implement a JComboBox for selection of a String from a set of valid strings, as well as color each option differently (different background colors in the JComboBox).
Also, the ArrayList<Cert> currently displays in a cell as [xxx, xxx, …] where XXX is the return from the toString() function for each item in the ArrayList. I think I’d like to display that ArrayList<Cert> as a read-only JComboBox, but I’m not as concerned with this item.
I’m questioning how many classes I need to create to make this happen. I already have a custom model for the JTable extending AbstractTableModel. Do I need to write an extension of JComboBox or do I just need to extend the appropriate renderer for a JComboBox as a cell and do the magic there, then assign that custom renderer to the cell renderer for the String cell?
Here’s what I have so far, lightly abridged:
public class EmployeeTableModel extends AbstractTableModel {
...
private ArrayList<Employee> myDataObjects = new ArrayList<Employee>();
...
@Override
public Object getValueAt(int row, int column) {
Employee emp = myDataObjects.get(row);
switch (column) {
case 0:
return emp.getName();
case 1:
return emp.getShift();
case 2:
return emp.getCertifications();
default:
return "";
}
}
}
Employees:
public class Employee {
private String name;
private String shift;
private ArrayList<Cert> certs;
...
public String getName() {
return name;
}
public String getShift() {
return shift;
}
public ArrayList<Cert> getCerts() {
return certs;
}
...
}
And the initializations:
EmployeeTableModel etm = new EmployeeTableModel();
JTable employeeTable = new JTable();
employeeTable.setModel( etm );
you can to start with, simplest code as is possible, maybe depends if you want to see JComboBox as Renderer too by @aterai