2013-03-31 77 views
0

我不知道如何設置特定行中的jcombobox ...現在我已經爲所有行設置了這個jcombobox,但我只需要一個行:JTable的特定行中的Celleditor(JComboBox)

JComboBox cc = new JComboBox(); 
cc.addItem(jComboBox5.getSelectedItem()+"/"+jComboBox6.getSelectedItem()+"/"+jComboBox7.getSelectedItem()+" "+jComboBox1.getSelectedItem()+"."+jComboBox2.getSelectedItem()); 
jTable1.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(cc)); 
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(); 
    renderer.setToolTipText("CLICCA PER LE DATE"); 
jTable1.getColumnModel().getColumn(3).setCellRenderer(renderer); 

回答

2

更新:正如我在測試我的(可能是不完全的)回答了,我碰到一個很好的SO問題,我認爲這將有助於比我好多了出來:Putting JComboBox into JTable

另一個更新:我再次讀你的問題,我意識到你問了一個特定行。我能想到這樣做的唯一方法是創建一個自定義CellEditor的,是這樣的:

private static class MyCellEditor extends AbstractCellEditor implements TableCellEditor { 

    DefaultCellEditor other = new DefaultCellEditor(new JTextField()); 
    DefaultCellEditor checkbox = new DefaultCellEditor(new JComboBox(new Object[] {"abc"})); 

    private DefaultCellEditor lastSelected; 

    @Override 
    public Object getCellEditorValue() { 

     return lastSelected.getCellEditorValue(); 
    } 

    @Override 
    public Component getTableCellEditorComponent(JTable table, 
      Object value, boolean isSelected, int row, int column) { 
     if(row == 0) { 
      lastSelected = checkbox; 
      return checkbox.getTableCellEditorComponent(table, value, isSelected, row, column); 
     } 
     lastSelected = other; 
     return other.getTableCellEditorComponent(table, value, isSelected, row, column); 
    } 

} 

在這個例子中,自定義CellEditor的實際上是兩個編輯,並根據選擇的行,特定的編輯器將得到呼籲(形象上和字面上)。我承認lastSelected似乎有點混亂,但我真的找不到一個更簡單的方法來知道要返回哪個編輯器值(因爲getCellEditorValue沒有參數)。

爲了讓你的表格看起來「正確」,你可能也必須對Renderer做一些事情(因爲Renderer可能會或可能不知道將JComboBox的選定值顯示爲初始值)。這取決於你如何初始化實際表中的數據。


爲了完整起見,我原來的答覆是如下:

您可以將JComboBox組件添加到在TableModel的使用addRow如下所示:也How to add row in JTable?

參見: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

我認爲主要問題是你在混合Column Editors/R的想法確定將存儲在每一行中的實際數據。

5

,但我想它只有一行:

覆蓋的getCellEditor(...)方法返回給定的行指定的編輯器:

import java.awt.*; 
import java.awt.event.*; 
import java.util.List; 
import java.util.ArrayList; 
import javax.swing.*; 
import javax.swing.border.*; 
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); 
      } 
     }; 

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

此代碼可能是放置在官方的Oracles教程中, – mKorbel