Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are using the <code>DefaultListModel</code> which uses a <code>DefaultListCellRenderer</code>. I don't see any part in your code that actually makes use of your <code>HumanRenderer</code>. You have to write your own Model.</p> <pre><code>public class HumanListModel extends DefaultListModel { private ArrayList&lt;Human&gt; data; public HumanListModel() { super(); data = new ArrayList&lt;Human&gt;(); } public void addHuman(Human h) { // add new human to the model data.add(h); fireTableStructureChanged(); } public void removeHuman(Human h) { data.remove(h); fireTableStructureChanged(); } @Override public int getColumnCount() { // the number of columns you want to display return 1; } @Override public int getRowCount() { return data.size(); } @Override public Object getValueAt(int row, int col) { return (row &lt; data.size()) ? data.get(row) : null; } @Override public String getColumnName(int col) { return "Human"; } @Override public Class getColumnClass(int col) { return Human.class; } } </code></pre> <p>For your <code>JTable</code> you just have to set the <code>HumanListModel</code> and define your renderer. All changes to your data should afterwards directely made at the model. Ergo use: <code>model.addHuman()</code> and <code>model.removeHuman()</code>. They fire the necessary Events which the <code>JTable</code> listens for in order to repaint.</p> <pre><code>HumanListModel model = new HumanListModel(); JTable newbies = new JTable(model); newbies.setDefaultRenderer(Human.class, new HumanRenderer()); </code></pre> <p>I hope it works...</p>
 

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