代码之家  ›  专栏  ›  技术社区  ›  Cratylus

jtable和自定义组合

  •  1
  • Cratylus  · 技术社区  · 14 年前

    我想在jtable(swing)中实现一个属性表。 我想要一个。

    ** 2列表格

    属性文本|值组合框 A、 B,C.第3行:属性文本| Txtbox。

    **

    我不明白怎么开始。我正在使用Netbeans。 我需要实现一个celltableeditor或者什么?

    谢谢

    1 回复  |  直到 14 年前
        1
  •  1
  •   camickr    14 年前

    第一个示例显示如何为表中的每一行指定不同的编辑器:

    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);
        }
    }
    

    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);
        }
    }
    

    你应该能够通过使用上面一个或两个例子的建议来实现你想要的。