如何在JTable中为一列添加不同的单元格编辑器? [英] how to add different cell editors for one column in JTable?

查看:88
本文介绍了如何在JTable中为一列添加不同的单元格编辑器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两列的JTable,两者都是JComboBox,为此我实现了自己的Model和overrode方法。我覆盖的方法之一是:

I have a JTable with two columns and both are JComboBox, for this purpose I implemented my own Model and overrode methods. One of the method which I overrode is:

public Class getColumnClass(int index) {
  return JComboBox.class;
}

还创建了我自己的ComboBoxEditor和ComboBoxRender类,并设置了cellEditor和cellRenderer:

Also created my own ComboBoxEditor and ComboBoxRender classes, and set cellEditor and cellRenderer:

column.setCellEditor(new ComboBoxEditor());
column.setCellRenderer(new ComboBoxRenderer());

现在我想进行更改,因此对于第一列,一些单元格是JComboBox,一些单元格是标准的文本数据。

Now I want to make changes, so that for column one some cells are JComboBox and some cells are standard textual data.

我如何实现这一目标?

欢迎任何有用的建议

推荐答案

你需要实现自己的 javax.swing.table.TableCellRenderer ,其中
根据您的业务逻辑,在 getTableCellRendererComponent(JTable表,Object值,boolean isSelected,boolean hasFocus,int row,int column)返回不同的渲染器。 TableCellEditor 接口相同。

You need to implement your own javax.swing.table.TableCellRenderer, where return different renderers at getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) according to your business logic. The same for TableCellEditor Interface.

这篇关于如何在JTable中为一列添加不同的单元格编辑器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆