Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing JComboBox with custom renderer as JTable cell renderer/editor (architecture)
    primarykey
    data
    text
    <p>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 <code>JTable</code> with rows of <code>Employee</code>, which itself has datatypes of <code>String</code> and <code>ArrayList&lt;Cert&gt;</code>. <code>Cert</code> contains a <code>String</code>.</p> <p>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 <code>JComboBox</code> for selection of a <code>String</code> from a set of valid strings, as well as color each option differently (different background colors in the <code>JComboBox</code>).</p> <p>Also, the <code>ArrayList&lt;Cert&gt;</code> currently displays in a cell as [xxx, xxx, ...] where XXX is the return from the <code>toString()</code> function for each item in the <code>ArrayList</code>. I think I'd like to display that <code>ArrayList&lt;Cert&gt;</code> as a read-only <code>JComboBox</code>, but I'm not as concerned with this item.</p> <p>I'm questioning how many classes I need to create to make this happen. I already have a custom model for the <code>JTable</code> extending <code>AbstractTableModel</code>. Do I need to write an extension of <code>JComboBox</code> or do I just need to extend the appropriate renderer for a <code>JComboBox</code> as a cell and do the magic there, then assign that custom renderer to the cell renderer for the <code>String</code> cell?</p> <p>Here's what I have so far, lightly abridged:</p> <pre><code>public class EmployeeTableModel extends AbstractTableModel { ... private ArrayList&lt;Employee&gt; myDataObjects = new ArrayList&lt;Employee&gt;(); ... @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 ""; } } } </code></pre> <p>Employees:</p> <pre><code>public class Employee { private String name; private String shift; private ArrayList&lt;Cert&gt; certs; ... public String getName() { return name; } public String getShift() { return shift; } public ArrayList&lt;Cert&gt; getCerts() { return certs; } ... } </code></pre> <p>And the initializations:</p> <pre><code>EmployeeTableModel etm = new EmployeeTableModel(); JTable employeeTable = new JTable(); employeeTable.setModel( etm ); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload