8

How can I implement a JTable with different types of cell editors depending on the type of input a particular row is displaying?

For example

  • some rows could be checkboxes (for boolean types)
  • some rows could be comboboxes (if I want to provide a fixed set of options to choose from)
  • some rows could be text fields (if I allow arbitrary data).

Currently I have implemented the AbstractTableModel, which takes a set of custom field objects from my object and adds rows to the table. I would like to further customize my table by setting specific types of cells. I can determine which cell type to use based on the type of field that row contains.

The table model is dynamically created at run-time.

MxLDevs
  • 19,048
  • 36
  • 123
  • 194
  • there no issue to create EachRowRenderer for TableCellRenderer, but never seen, tried (if is there) with TableCellEditor, btw doesn't matter if is value for JTable is stored in Default or AbstractTableModel – mKorbel Jun 06 '13 at 20:02

2 Answers2

17
  • some rows could be checkboxes (for boolean types)
  • some rows could be comboboxes (if I want to provide a fixed set of options to choose from)
  • some rows could be text fields (if I allow arbitrary data).

for example

enter image description here

import java.awt.EventQueue;
import java.util.Date;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;


public class EachRowRendererEditor {

    private JFrame frame = new JFrame("EachRowRendererEditor");
    private  String[] columnNames = {"Type", "Value"};
    private     Object[][] data = {
            {"String", "I'm a string"},
            {"Date", new Date()},
            {"Integer", new Integer(123)},
            {"Double", new Double(123.45)},
            {"Boolean", Boolean.TRUE}};
   private  JScrollPane scrollPane;
   private  JTable table;

    public EachRowRendererEditor() {
        table = new JTable(data, columnNames) {

            private static final long serialVersionUID = 1L;
            private Class editingClass;

            @Override
            public TableCellRenderer getCellRenderer(int row, int column) {
                editingClass = null;
                int modelColumn = convertColumnIndexToModel(column);
                if (modelColumn == 1) {
                    Class rowClass = getModel().getValueAt(row, modelColumn).getClass();
                    return getDefaultRenderer(rowClass);
                } else {
                    return super.getCellRenderer(row, column);
                }
            }

            @Override
            public TableCellEditor getCellEditor(int row, int column) {
                editingClass = null;
                int modelColumn = convertColumnIndexToModel(column);
                if (modelColumn == 1) {
                    editingClass = getModel().getValueAt(row, modelColumn).getClass();
                    return getDefaultEditor(editingClass);
                } else {
                    return super.getCellEditor(row, column);
                }
            }
            //  This method is also invoked by the editor when the value in the editor
            //  component is saved in the TableModel. The class was saved when the
            //  editor was invoked so the proper class can be created.

            @Override
            public Class getColumnClass(int column) {
                return editingClass != null ? editingClass : super.getColumnClass(column);
            }
        };
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        scrollPane = new JScrollPane(table);
        frame.add(scrollPane);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                EachRowRendererEditor eeee = new EachRowRendererEditor();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    +1 for providing an example straight away. Uses `JTable.getDefaultRenderer` which is easy. – TT. Jun 06 '13 at 20:17
  • Using this for reference, I could probably implement combo boxes as well relatively easily using something like http://www.javaworld.com/javaworld/javatips/jw-javatip102.html ? – MxLDevs Jun 06 '13 at 20:39
  • @TT. with this ZOO is there one bug (not in official), can't found that here, remember that only solved in Substance(DefaultTableCellRenderer), now no idea about it is – mKorbel Jun 06 '13 at 20:46
  • @Keikoku [do you meaning](http://stackoverflow.com/a/6261853/714968), is possible put there [AutoComplete JComboBox/JTextField as TableCellEditor](http://stackoverflow.com/a/10901944/714968), notice there no reason to use HashMap, DefaultTableModel can do that and easily:-), Never to put JComponents in the XxxTableModel, for JComboBox (as renderer and editor) is there stored only String value (depends data typer for ComboBoxModel) – mKorbel Jun 06 '13 at 20:52
  • +1 example of the approach outlined [here](http://stackoverflow.com/a/7701420/230513). – trashgod Jun 06 '13 at 21:33
  • @mKorbel so it is better to use the autocomplete combobox/textfield? – MxLDevs Jun 07 '13 at 14:02
  • your index conversion is incomplete ... and unneeded: simply access the cell value with table api (vs. querying the underlying model) `table.getValueAt(row, column)` which is in view coordinates – kleopatra Jun 17 '13 at 09:11
  • @mKorbel : WOW :-), just reading `JTable`, now a days, never thought, that even this thing can also be done :-) +1 – nIcE cOw Jul 17 '13 at 18:35
0

Create a custom class implementing javax.swing.table.TableCellRenderer, which displays the values using the control you want to display with depending on the data type. Use instances of this class as cell renderer (TableColumn.setCellRenderer)

TT.
  • 15,774
  • 6
  • 47
  • 88