Search code examples
javauser-interfaceswingjtablejcombobox

jtable and custom combos


I want to implement a properties table in jtable (swing). I want to have e.g.

** 2 column table

Row1: Property-text| Txtbox. Row2: Property-text| ComboBox of values A,B,C. Row3: Property-text| Txtbox. Row4: Property-text| ComboBox of values E,F,G.

**

I can not understand how to get this started. I am using Netbeans. I need to implement a celltableeditor or what?

Thanks


Solution

  • The first example shows how you can specify a different editor for each row in the table:

    import java.awt.*;
    import java.awt.event.*;
    import java.util.List;
    import java.util.ArrayList;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class TableComboBoxByRow extends JFrame
    {
        List<TableCellEditor> editors = new ArrayList<TableCellEditor>(3);
    
        public TableComboBoxByRow()
        {
            // Create the editors to be used for each row
    
            String[] items1 = { "Red", "Blue", "Green" };
            JComboBox comboBox1 = new JComboBox( items1 );
            DefaultCellEditor dce1 = new DefaultCellEditor( comboBox1 );
            editors.add( dce1 );
    
            String[] items2 = { "Circle", "Square", "Triangle" };
            JComboBox comboBox2 = new JComboBox( items2 );
            DefaultCellEditor dce2 = new DefaultCellEditor( comboBox2 );
            editors.add( dce2 );
    
            String[] items3 = { "Apple", "Orange", "Banana" };
            JComboBox comboBox3 = new JComboBox( items3 );
            DefaultCellEditor dce3 = new DefaultCellEditor( comboBox3 );
            editors.add( dce3 );
    
            //  Create the table with default data
    
            Object[][] data =
            {
                {"Color", "Red"},
                {"Shape", "Square"},
                {"Fruit", "Banana"},
                {"Plain", "Text"}
            };
            String[] columnNames = {"Type","Value"};
            DefaultTableModel model = new DefaultTableModel(data, columnNames);
            JTable table = new JTable(model)
            {
                //  Determine editor to be used by row
                public TableCellEditor getCellEditor(int row, int column)
                {
                    int modelColumn = convertColumnIndexToModel( column );
    
                    if (modelColumn == 1 && row < 3)
                        return editors.get(row);
    //                  return (TableCellEditor)editors.get(row);
                    else
                        return super.getCellEditor(row, column);
                }
            };
            System.out.println(table.getCellEditor());
    
            JScrollPane scrollPane = new JScrollPane( table );
            getContentPane().add( scrollPane );
        }
    
        public static void main(String[] args)
        {
            TableComboBoxByRow frame = new TableComboBoxByRow();
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.pack();
            frame.setVisible(true);
        }
    }
    

    The second example shows how you can create a very basic property editor (although it doesn't support combo boxes):

    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    
    public class TablePropertyEditor extends JFrame
    {
        public TablePropertyEditor()
        {
            String[] columnNames = {"Type", "Value"};
            Object[][] data =
            {
                {"String", "I'm a string"},
                {"Date", new Date()},
                {"Integer", new Integer(123)},
                {"Double", new Double(123.45)},
                {"Boolean", Boolean.TRUE}
            };
    
            JTable table = new JTable(data, columnNames)
            {
                private Class editingClass;
    
                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);
                }
    
                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.
    
                public Class getColumnClass(int column)
                {
                    return editingClass != null ? editingClass : super.getColumnClass(column);
                }
            };
    
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            JScrollPane scrollPane = new JScrollPane( table );
            getContentPane().add( scrollPane );
        }
    
        public static void main(String[] args)
        {
            TablePropertyEditor frame = new TablePropertyEditor();
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.pack();
            frame.setLocationRelativeTo( null );
            frame.setVisible(true);
        }
    }
    

    You should be able achieve what you want by using the suggestions from one or both of the above examples.