我只想在选择表格中的特定单元格时将defaultCellEditor更改为JComboBox。如何将JComboBox添加到JTable中的特定单元格
1
A
回答
2
只需让您的TableModel
从该单元格的isCellEditable()
返回true即可。
4
重写JTable的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);
// 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);
}
}
1
如果您使用netbeans,请在执行的jbutton操作下键入此代码。
你应该有jtable和jcombobox几个项目。试试这个单一的代码:
jTable1.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(jComboBox1));
相关问题
- 1. 将JComboBox添加到JTable单元格。选择的项目不留
- 2. JTable单元格中的JComboBox
- 3. 特定JTable单元中的JComboBox
- 4. 将JComboBox添加到JTable
- 5. 将JComboBox添加到每行特定数据的jTable中
- 6. JTable单元格内的JComboBox
- 7. 将JButton添加到JTable单元格+ DB
- 8. 如何将工具提示添加到jtable中的单元格?
- 9. 如何将特定的tableview单元格添加到tableview中?
- 10. 如何添加JTable中进入的JComboBox
- 11. 如何在JTable中的特定单元格中添加dropdownbox,如microsoft excel?
- 12. 如何将UndoableEditListener添加到JTable单元格?
- 13. 将按钮添加到Jtable中的netbeans中的单元格
- 14. 在JTable中添加JcomboBox
- 15. 如何停止将已添加的值从JCombobox添加到JTable中
- 16. Jtable jcombobox和单元格编辑
- 17. 如何增加iPhone中的特定单元格的行高,并将按钮添加到增加的单元格
- 18. Ext Js如何将组合框添加到网格中的特定单元格?
- 19. 如何将图像添加到DataGridView中的单个特定单元格?
- 20. 如何让JTable中选定的单元格(JComboBox中)的当前位置
- 21. 将行注释添加到datagridview中的特定单元格
- 22. JTable的特定行中的Celleditor(JComboBox)
- 23. 如何添加的JComboBox到JTable中列在NetBeans
- 24. 如何在jTable的某个单元格上获取jComboBox?
- 25. 如何修复JTable的java.awt.IllegalComponentStateException和JComboBox单元格编辑器?
- 26. 如何将下拉菜单添加到JTable单元
- 27. Excel(VBA,Userform)如何将Listbox中的数据添加到特定的单元格?
- 28. 如何将NSTableView单元格添加到单元格的填充?
- 29. 将显示jComboBox的呈现器链接到jTable中的单元格
- 30. 在特定单元格中添加节