【问题标题】:JComboBox in a JTable cellJTable 单元格中的 JComboBox
【发布时间】:2011-03-16 10:14:06
【问题描述】:

我有一个使用模型创建的 JTable,该模型基于对象矩阵。 对于每一行,我想使用 JComboBox 在特定列(第 5 列)中放入一些信息。我尝试了以下方法:

for(int i=0; i < n ; i++) {  
    .....  
    data[i][5] = new JComboBox(aux); // aux is a Vector of elements I wanna insert  
}  
table.setModel(new MyTableModel()); // MyTableModel() already takes into consideration the data[][] object  

问题是data[i][5] = new JComboBox(aux); 没有在 JTable 的特定单元格中创建 JComboBox 对象,而是将代码粘贴到行中。我该怎么做才能解决这个问题?

【问题讨论】:

标签: java swing jtable jcombobox


【解决方案1】:

要显示JComboBox,您必须使用TableCellRenderer。看看Using a Combo Box as an Editor

【讨论】:

    【解决方案2】:

    呵呵,不能像你说的那样用。

    您必须创建自定义 TableCellRenderer 或 TableCellEditor。 然后你可以指定它将用于哪个类:

    JTable.setDefaultRenderer(Class<?> columnClass, TableCellRenderer renderer)
    JTable.setDefaultEditor(Class<?> columnClass, TableCellEditor editor)
    

    详细描述可以在这里找到:http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/components/table.html#combobox

    对于特定行和列中的自定义渲染器,您可以简单地使用:

    final int specialRow = 1;
    final int specialColumn = 5;
    
    JTable table = new JTable(myModel) {
       private TableCellEditor mySpecialCellEditor = new SpecialCellEditor( ... );
    
       public TableCellEditor getCellEditor(int row, int column) {
          int modelColumn = convertColumnIndexToModel(column);
          int modelRow = convertRowIndexToModel(row);
          if (modelColumn == specialColumn && row == specialRow ) {
             return mySpecialCellEditor;
          } else {
             return super.getCellEditor(row, column);
          }
       }
    };
    

    【讨论】:

    • 谢谢我一直在分析您在互联网上发布的所有这些示例以及其他示例……但它们不适合。我只需要在 JTable 中的特定位置创建一个 JComboBox,例如 data[1][5] 。盒子已经创建好了,我只需要在那个地方链接它。在这些示例中,我看到他们获得了一个完整的列并从中创建了一个 JComboBox,但我看不到他们如何引用表中的特定位置,例如第 2 行、第 5 列......等等......谢谢
    【解决方案3】:

    一种方法是重写 getCellEditor() 方法以返回适当的编辑器。下面是一个帮助您入门的示例:

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

    编辑:更新代码以使用垃圾上帝的建议。

    【讨论】:

    • 非常简洁。作为替代方案,请考虑List&lt;DefaultCellEditor&gt; editors = new ArrayList&lt;DefaultCellEditor&gt;(3)
    • 是的,我在泛型存在之前编写了示例代码。我想我应该更新它以更新:)
    • 经过反思,List&lt;TableCellEditor&gt; editors = new ArrayList&lt;TableCellEditor&gt;(3) 可能会更好。它更通用,消除了getCellEditor() 中的演员表并允许更改实现。对流浪汉感到抱歉;还在学习中。
    【解决方案4】:

    试试这样的:

     public void example(){  
    
          TableColumn tmpColum =table.getColumnModel().getColumn(1);
          String[] DATA = { "Data 1", "Data 2", "Data 3", "Data 4" };
          JComboBox comboBox = new JComboBox(DATA);
    
          DefaultCellEditor defaultCellEditor=new DefaultCellEditor(comboBox);
          tmpColum.setCellEditor(defaultCellEditor);
          tmpColum.setCellRenderer(new CheckBoxCellRenderer(comboBox));
          table.repaint();
       }
    
    
    /**
       Custom class for adding elements in the JComboBox.
    */
    class CheckBoxCellRenderer implements TableCellRenderer {
            JComboBox combo;
            public CheckBoxCellRenderer(JComboBox comboBox) {
                this.combo = new JComboBox();
                for (int i=0; i<comboBox.getItemCount(); i++){
                    combo.addItem(comboBox.getItemAt(i));
                }
            }
            public Component getTableCellRendererComponent(JTable jtable, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                combo.setSelectedItem(value);
                return combo;
            }
        }
    

    【讨论】:

    • 这会将tmpColum 中的所有单元格更改为JComboBoxes。如果您只想使用其行和列更改特定单元格怎么办?
    猜你喜欢
    • 2015-03-07
    • 1970-01-01
    • 2012-03-05
    • 2021-06-20
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    相关资源
    最近更新 更多