怎么在JTable表格中加入如JComboBox之类的控件?有注释加分。

怎么在JTable生成的表格单元格中加入如JComboBox的下拉列表框,JCheckBox复选框,JRadioButton单选框这些组件?我搜索了一下网上,提到什么Ta... 怎么在JTable生成的表格单元格中加入 如JComboBox的下拉列表框,JCheckBox复选框,JRadioButton单选框 这些组件?

我搜索了一下网上,提到什么TableCellRenderer,TableModel,TableCellEditor这些我没学过的类。 没有例子我也不知道怎么用。

哪位朋友给各最简单的代码例子吧,加上注释最好,有加分。谢谢。
展开
 我来答
bearfig
推荐于2016-06-15 · TA获得超过877个赞
知道大有可为答主
回答量:1074
采纳率:0%
帮助的人:1104万
展开全部
你去看看官方文档吧 :

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));
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式