我不知道该怎么做才能设置一个特定行的JComboBox ...现在我已经到此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);
更新:由于我是在试探我的(可能是不完全的)回答了,我碰到一个很好的SO问题,我认为这将有助于更好地来到比我能: 把JComboBox中到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
没有参数)。
为了使您的表出现“正确的”,你很可能将不得不做的事渲染,以及(因为渲染可能会或可能不知道,以显示JcomboBox的选择的值作为初始值)。 这取决于你如何在实际的表初始化数据。
为了完整起见,我原来的答复是如下:
您可以将JComboBox组件添加到使用该行 addRow
上的TableModel如下所示: 如何在JTable中添加行?
参见: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
我认为主要的问题是,你混合列编辑/渲染器与将被存储在各个行中的实际数据的想法。
但我想它在只有一行:
覆盖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);
}
}