views:

38

answers:

1

Hi,

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

+1  A: 

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.

camickr
@camickr: Thank you VERY much for your help!!!!So the way to do it is to implement custom versions of getCellRenderer and getCellEditor? The anonymous class table you create overrides these? One more question since I am beginner in swing. From your example, I should understand that complex components can not be created via designers?
Based on your original description where you only have two rows of data you may only need to override the getCellEditor(...) method. The first solution is easier in this case but I gave you different approaches so you can experiment. I don't use an IDE to design my GUI's so I don't know how much you can customize the code generated.
camickr
@camickr: I followed your example code and have implemented what I wanted. Thanks a lot!!!!!!!!!!