Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To make a column editable you have to override the <code>isCellEditable</code> method in the <code>TableModel</code>. Creating a <code>TableModel</code> is fairly easy if you inherit <code>AbstractTableModel</code> and I'd recommend it for all but the most simple <code>JTable</code>s.</p> <p>However, adapting the <code>TableModel</code> is only part of what you need to do. To actually get a custom component in the <code>JTable</code>, you need to set a custom cell renderer. To use an interactive custom component, you need to set a custom cell editor. In some cases, it's enough to use slightly modificated versions of the default classes for this.</p> <p><b>Editors</b></p> <p>If you already have got a custom component is easily done using delegation: Create a new class implementing <code>TableCellEditor</code>, and return a <b>new</b> instance of the component in the <code>getCellEditorComponent</code> method. The paramaters to this method include the current value as well as the cell coordinates, a link back to the table and wether or not the cell is selected.</p> <p>The <code>TableCellEditor</code> also has a method that is called when the user commits a change to the cell contents (where you can validate user input and adjust the model) or cancels an edit. Be sure to call the <code>stopEditing()</code> method on your editor if you ever programmatically abort editing, otherwise the editor component will remain on screen -- this once took me like 2 hours to debug.</p> <p>Note that within a <code>JTable</code> editors and <b>only</b> editors receive events! Displaying a button can be done using a renderer. But to get a functioning button, you need to implement an editor with the correct <code>EventListeners</code> registered. Registering a listener on a renderer does nothing.</p> <p><b>Renderers</b></p> <p>Implementing a renderer is not strictly necessary for what you describe in your question, but you typically end up doing it anyway, if only for minor modifications. Renderers, unlike editors, are speed critical. <i>The <code>getTableCellRendererComponent</code> of a renderer is called once for every cell in the table!</i> The component returned by a renderer is only used to paint the cell, not for interaction, and thus can be "reused" for the next cell. In other words, you should adjust the component (e.g. using <code>setText(...)</code> or <code>setFont(...)</code> if it is a <code>TextComponent</code>) in the renderer, you should <i>not</i> instantiate a new one -- that's an easy way to cripple the performance.</p> <p><b>Caveats</b></p> <p>Note that for renderers and editors to work, you need to tell the <code>JTable</code> when to use a certain renderer/editor. There are basically two ways to do this. You can set the default cell renderer/editor <b>for a certain type</b> using the respective <code>JTable</code> methods. For this way to work, your <code>TableModel</code> needs to return exactly this type in the <code>getColumnClass(...)</code> method! The default table model will <b>not</b> do this for you, it always returns <code>Object.class</code>. I'm sure that one has stumped a lot of people.</p> <p>The other way to set the editor/renderer is by explicitly setting it on the column itself, that is, by getting the <code>TableColumn</code> via the <code>getTableColumn(...)</code> method of the <code>JTable</code>. This is a lot more elaborate, however, it's also the only way to have two different renderers/editors for a single class. E.g. your model might have two columns of class String which are rendered in entirely different ways, maybe once using a <code>JLabel/DefaultRenderer</code> and the other using a <code>JButton</code> to access a more elaborate editor.</p> <p><code>JTable</code> with its custom renderers and editors is <i>extremely</i> versatile, but it is also a lot to take in, and there are a lot of things to do wrong. Good luck!</p> <p><a href="http://java.sun.com/docs/books/tutorial/uiswing/components/table.html" rel="nofollow noreferrer">How to Use Tables</a> in <a href="http://java.sun.com/docs/books/tutorial/uiswing/index.html" rel="nofollow noreferrer">The Swing Tutorial</a> is mandatory reading for anyone customising JTables. In particular, read and reread <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender" rel="nofollow noreferrer">Concepts: Editors and Renderers</a> because it typically takes a while for it to "click". The examples on custom renderers and editors are also very worthwhile.</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