求救!!!如何在JTable的单元格尾部加入一个按钮

yema55 2004-08-25 09:58:38
如题,按钮用于接受事件后显示一个对话框,用于显示单元格内容
...全文
493 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
bradwoo8621 2004-08-27
  • 打赏
  • 举报
回复
如果写Editor的话, 实现理念是一样的. 不过应该写在Editor中isCellEditable方法, 例如
public class ColorEditor extends AbstractCellEditor implements TableCellEditor {
private JLabel label = new JLabel();

private Color color = null;

public ColorEditor() {
}

public Object getCellEditorValue() {
return color;
}

public boolean isCellEditable(EventObject e) {
if (e instanceof MouseEvent) {
MouseEvent me = (MouseEvent) e;
JTable table = (JTable) e.getSource();
int row = table.rowAtPoint(me.getPoint());
int col = table.columnAtPoint(me.getPoint());
Color color = (Color) table.getValueAt(row, col);

color = JColorChooser.showDialog(MainWindow.WINDOW_ANCESTOR,
"Color Chooser",
color);
if (color != null) {
this.color = color;
table.setValueAt(color, row, col);
}
}
return false;
}

public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
return label;
}
}
这是一个颜色编译器, 当然这里是点到cell就可以了, 按照上次问答的方法同样可以实现你需要的效果
bradwoo8621 2004-08-27
  • 打赏
  • 举报
回复
如果只写Renderer的话, 可以通过以下步骤来实现:
1, 利用你写好的包含Label和Button的Renderer. 注意, 鼠标监听器不是加载在Renderer上的
2, 在table上加载鼠标监听器, 捕捉到事件以后用
int row = table.rowAtPoint(e.getPoint());
int column = table.columnAtPoint(e.getPoint());
方法获取到点击的行和列, 如果是你需要的那一列则继续, 否则退出.
Rectangle rect = table.getCellRect(row, column, true);
获取这个Renderer的坐标和大小, 然后将鼠标的坐标换算到Cell里面, 这个相信应该没有问题吧.
获取到最终的坐标以后判断是否在button的坐标范围内, 如果在的话, 弹出对话框.
tangyongtgyg 2004-08-27
  • 打赏
  • 举报
回复
没有做过,顶一下!
yema55 2004-08-27
  • 打赏
  • 举报
回复
根据楼上的,我将MyCellRenderer改写如下:
class MyCellRenderer extends JPanel implements TableCellEditor, ActionListener
老样子,不能响应
监听器有注册,绝对不是疏忽所致
无奈,到处搜索,现得到的最有用的信息如下:

You have a fundamental misunderstanding of rendering.

The component returned from a cell renderer is merely
"rubber stamped" or painted into the component using
the cell renderer. The renderer component is NOT
actually placed into the component using the cell
renderer.

In order to do what you want to do you need to implement
a cell editor.

Also, note that DefaultTableCellRenderer *extends* JLabel.
The getTableCellRendererComponent returns a JLabel, which
is actually *this*.

Your cell renderer (or editor) should NOT create a new
component each time getTableCellRenderer() is called. It
should re-use one instance of each type of component that
might be returned, modifying it as needed, before returning
it. I cannot say exactly why, but if you don't heed this
advice, your code will have a memory leak, and your JVM
could eventually run out of memory.

但是不知道怎么写这个CellEditor,千万请高手相助!!!
maowu 2004-08-27
  • 打赏
  • 举报
回复
同学,用DefaultCellRenderer怎么可能又会响应?用TableCellEditor。
yema55 2004-08-27
  • 打赏
  • 举报
回复
最后一顶!
yema55 2004-08-27
  • 打赏
  • 举报
回复
我的代码:
class MyTableCellEditor extends DefaultCellEditor implements ActionListener
{
JPanel _panel = new JPanel();
JLabel _label = new JLabel();
JButton _button = new JButton();

public MyTableCellEditor()
{
super(new JCheckBox());
_label.setBackground(Color.WHITE);
_button.setText("...");
_button.setPreferredSize(new Dimension(16, 8));
_button.addActionListener(this);
_panel.setBackground(Color.WHITE);
_panel.setLayout(new BorderLayout());
_panel.add(_label);
_panel.add(_button, BorderLayout.EAST);
}

public Object getCellEditorValue()
{
return null;
}

public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
{
if(row == 6 && column == 1)
{
return _button;
}
return table.getDefaultEditor(Object.class).getTableCellEditorComponent(table, value, isSelected, row, column);
}

public void actionPerformed(ActionEvent e)
{
System.out.println("ok");
}

}

class MyCellRenderer extends JPanel implements TableCellRenderer,ActionListener
{
JLabel _label = new JLabel();
JButton _button = new JButton();

public MyCellRenderer()
{
_label.setBackground(Color.WHITE);
_button.setText("...");
_button.setPreferredSize(new Dimension(16, 8));
_button.addActionListener(this);
setBackground(Color.WHITE);
setLayout(new BorderLayout());
add(_label);
add(_button, BorderLayout.EAST);
}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
if(row == 6 && column == 1)
{
if(value != null )
{
String s = value.toString();
int pos = s.indexOf(MibNodeProp.DELIM);
if(pos > 0)
{
_label.setText(s.substring(0, pos));
s = s.substring(pos + MibNodeProp.DELIM.length());
}
setToolTipText(s);
}
if(isSelected)
{
setBackground(table.getSelectionBackground());
setForeground(table.getSelectionForeground());
}
else
{
setBackground(table.getBackground());
setForeground(table.getForeground());
}
return this;
}
return new ValueRenderer().getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}

public void actionPerformed(ActionEvent e)
{
System.out.println("ok");
}

}

怎么会不行?
table.setDefaultRenderer(cls, new MyCellRenderer());
table.getColumnModel().getColumn(1).setCellEditor(new MyTableCellEditor());

外观显示正常,为什么就是不能响应。
Editor有什么问题吗?
有的话,请改正!谢谢啦!
yiqiangyang 2004-08-26
  • 打赏
  • 举报
回复
final JButton btnArrow = (JButton) jcmbPurview.getComponent(0);
btnArrow.addActionListener(new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent event) {
Object object = event.getSource();
if (object == btnArrow) {
// 你的具体处理
}
}
});
yema55 2004-08-26
  • 打赏
  • 举报
回复
颜色问题已经解决了,现在就是按钮不能响应事件的问题了
千万请高手帮忙啊!!!
yema55 2004-08-26
  • 打赏
  • 举报
回复
谢谢帮顶,顶者有分
yema55 2004-08-26
  • 打赏
  • 举报
回复
救命啊!!!!!!!!!!!
yema55 2004-08-26
  • 打赏
  • 举报
回复
这个jcmbPurview想必是个JComboBox,
我不是用这个的,
我用的是class MyCellRenderer extends DefaultCellRenderer
里面用一个JPanel放入一个JLabel和一个JButton,
这样子外观就出来了,
但是我那个JButton 已经addActionListener了
public void actionPerformed(ActionEvent e)
{
System.out.println("ok");
}
可是按按钮却没有任何响应,怎么办?????
这么久了,难道csdn没什么高手?!!!!
yiqiangyang 2004-08-25
  • 打赏
  • 举报
回复
呵呵刚才忘了说明,下拉框去掉,将下拉框的标志改成普通按钮形状。但感觉这样不是很好。你可以试着自己写一个CellEditor和CellRenderer。如果你需要我的做法的程序请留言。另对此表示关注,如果楼主实现了,拿出来大家共享一下。
yema55 2004-08-25
  • 打赏
  • 举报
回复
没人做过吗?大虾快帮忙啊
yiqiangyang 2004-08-25
  • 打赏
  • 举报
回复
呵呵这个问题我也碰到。我是把JComboBox进行改造。也就说把它里面的下拉框去掉。不知道这个对你又帮助否
射天狼 2004-08-25
  • 打赏
  • 举报
回复
UP
xuyang821225 2004-08-25
  • 打赏
  • 举报
回复
直接生成一个Button 啊
Frank1982 2004-08-25
  • 打赏
  • 举报
回复
没做过,帮顶.
vongood 2004-08-25
  • 打赏
  • 举报
回复
up
yema55 2004-08-25
  • 打赏
  • 举报
回复
高手都跑到哪里去了?
加载更多回复(2)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧