
怎么在JTable表格中加入如JComboBox之类的控件?有注释加分。
怎么在JTable生成的表格单元格中加入如JComboBox的下拉列表框,JCheckBox复选框,JRadioButton单选框这些组件?我搜索了一下网上,提到什么Ta...
怎么在JTable生成的表格单元格中加入 如JComboBox的下拉列表框,JCheckBox复选框,JRadioButton单选框 这些组件?
我搜索了一下网上,提到什么TableCellRenderer,TableModel,TableCellEditor这些我没学过的类。 没有例子我也不知道怎么用。
哪位朋友给各最简单的代码例子吧,加上注释最好,有加分。谢谢。 展开
我搜索了一下网上,提到什么TableCellRenderer,TableModel,TableCellEditor这些我没学过的类。 没有例子我也不知道怎么用。
哪位朋友给各最简单的代码例子吧,加上注释最好,有加分。谢谢。 展开
1个回答
展开全部
你去看看官方文档吧 :
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
下面是另一个例子:
JTable table = new JTable();
DefaultTableModel model = (DefaultTableModel)table.getModel();
// Add some columns
model.addColumn("A", new Object[]{"item1"});
model.addColumn("B", new Object[]{"item2"});
// These are the combobox values
String[] values = new String[]{"item1", "item2", "item3"};
// Set the combobox editor on the 1st visible column
int vColIndex = 0;
TableColumn col = table.getColumnModel().getColumn(vColIndex);
col.setCellEditor(new MyComboBoxEditor(values));
// If the cell should appear like a combobox in its
// non-editing state, also set the combobox renderer
col.setCellRenderer(new MyComboBoxRenderer(values));
public class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
public MyComboBoxRenderer(String[] items) {
super(items);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
// Select the current value
setSelectedItem(value);
return this;
}
}
public class MyComboBoxEditor extends DefaultCellEditor {
public MyComboBoxEditor(String[] items) {
super(new JComboBox(items));
}
}
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
下面是另一个例子:
JTable table = new JTable();
DefaultTableModel model = (DefaultTableModel)table.getModel();
// Add some columns
model.addColumn("A", new Object[]{"item1"});
model.addColumn("B", new Object[]{"item2"});
// These are the combobox values
String[] values = new String[]{"item1", "item2", "item3"};
// Set the combobox editor on the 1st visible column
int vColIndex = 0;
TableColumn col = table.getColumnModel().getColumn(vColIndex);
col.setCellEditor(new MyComboBoxEditor(values));
// If the cell should appear like a combobox in its
// non-editing state, also set the combobox renderer
col.setCellRenderer(new MyComboBoxRenderer(values));
public class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
public MyComboBoxRenderer(String[] items) {
super(items);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
// Select the current value
setSelectedItem(value);
return this;
}
}
public class MyComboBoxEditor extends DefaultCellEditor {
public MyComboBoxEditor(String[] items) {
super(new JComboBox(items));
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询