Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>But the <code>JTable</code> stays empty; not even plain text is displayed.</p> </blockquote> <p>You need a renderer that corresponds to your editor, as suggested in this <a href="http://sites.google.com/site/drjohnbmatthews/table" rel="nofollow noreferrer">example</a> that contains both a <code>ValueRenderer</code> <em>and</em> a <code>ValueEditor</code>.</p> <blockquote> <p>I just need to put a <code>JComboBox</code> in some cells of the <code>JTable</code>:</p> </blockquote> <p>As discussed in <a href="http://download.oracle.com/javase/tutorial/uiswing/components/table.html#editrender" rel="nofollow noreferrer"><em>How to Use Tables: Concepts: Editors and Renderers</em></a>, "a single <em>cell renderer</em> is generally used to draw all of the cells that contain the same type of data."</p> <blockquote> <p>I specified both row and column, but nothing is happening</p> </blockquote> <p>Your code appears to work when I add the new <code>JTable</code> to a container.</p> <p><img src="https://i.stack.imgur.com/lYIWF.png" alt="TableTest"></p> <pre><code>import java.awt.Dimension; import java.awt.EventQueue; import javax.swing.DefaultCellEditor; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableCellEditor; /** @see http://stackoverflow.com/questions/7200500 */ public class TableTest extends JPanel { public TableTest() { this.setPreferredSize(new Dimension(320, 240)); final JComboBox myEditor = new JComboBox( new String[]{"yes", "no", "maybe"}); String[][] data = new String[10][4]; data[0][0] = "0,0"; data[0][5] = "0,1"; data[1][0] = "1,0"; data[1][6] = "1,1"; data[2][0] = "2,0"; data[2][7] = "2,1"; String[] columnNames = {"Nom", "Valeur"}; DefaultTableModel model = new DefaultTableModel(data, columnNames); JTable table = new JTable(model) { DefaultCellEditor myCellEditor = new DefaultCellEditor(myEditor); @Override public TableCellEditor getCellEditor(int row, int column) { int modelColumn = convertColumnIndexToModel(column); int modelRow = convertRowIndexToModel(row); if (modelColumn == 1 &amp;&amp; modelRow == 1) { return myCellEditor; } else { return super.getCellEditor(row, column); } } }; this.add(table); } private void display() { JFrame f = new JFrame("TableTest"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(this); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new TableTest().display(); } }); } } </code></pre>
 

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