2010-07-15 122 views
3

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

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 

的问题是,数据[I] [5] =新的JComboBox(AUX);不会在JTable的特定单元中创建JComboBox对象,但会将代码粘贴到行中。 我能做些什么来解决这个问题?

谢谢。

回答

1

呵呵,它不会像你提出的那样使用。

您必须创建一个自定义的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); 
     } 
    } 
}; 
+0

谢谢 我一直在分析所有这些例子中你张贴和其他人,在互联网上..但他们不适合。 我只需要在JTable的特定位置创建一个JComboBox,比如data [1] [5]。 该框已经创建好了,我只需要将它链接到那个地方。 在这些例子中,我看到他们获得了一个完整的列,并从中创建了一个JComboBox,并且我看不到他们如何引用表中的特定位置,例如row-2,column-5等。 。 谢谢 – 2010-07-15 14:26:03

8

一种方法是重写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); 
    } 
} 

编辑:代码更新为使用垃圾建议。

+0

非常简洁。作为替代方案,考虑'List editors = new ArrayList (3)'。 – trashgod 2010-07-15 15:27:26

+0

是的,我在泛型存在之前编写了示例代码。我想我应该更新它是更当前:) – camickr 2010-07-15 18:08:45

+0

反思,'List 编辑=新的ArrayList (3)'可能会更好。更通用的做法是避免'getCellEditor()'中的强制转换,并允许更改实现。对不起,这个流浪汉仍在学习。 – trashgod 2010-07-16 03:36:00

1

尝试这样:

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; 
     } 
    } 
相关问题