gpt4 book ai didi

java - Jtable 单元格上的 Jbutton 只能工作一次

转载 作者:行者123 更新时间:2023-11-30 05:07:56 25 4
gpt4 key购买 nike

我对 Java Swing 还很陌生,并且在 Jtable 单元格上使用 Jbutton 时遇到一些问题。

问题是 Jbutton 只能工作一次,然后就无法正常工作。第一次之后,当我再次单击时没有任何反应, getTableCellEditorComponent 仅被调用一次。我希望它像普通按钮一样工作。

我的按钮编辑器和按钮渲染器类如下:

package buttonontable;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;
import java.util.HashMap;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.TableCellEditor;


public class MyButtonEditor extends JButton implements TableCellEditor {
protected JButton button;
ButtonOnJtableAction buttonOnJtableAction;

String labelStr;




public MyButtonEditor(String buttonStr,ButtonOnJtableAction _buttonOnJtableAction) {
super(buttonStr);
buttonOnJtableAction = _buttonOnJtableAction;
button = new JButton();
button.setOpaque(true);
System.out.println("MyButtonEditor constructed");

}

public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {


System.out.println("getTableCellEditorComponent");
JOptionPane.showMessageDialog(table, "Pressed at " + row + "x" + column);
buttonOnJtableAction.buttonPressed(table,row,column);
return this;
}

public void cancelCellEditing() {
System.out.println("cancelCellEditing");
System.out.println("Cancel");
}

public boolean stopCellEditing() {
System.out.println("stopCellEditing");
return true;
}

public Object getCellEditorValue() {
System.out.println("getCellEditorValue");
return null;
}

public boolean isCellEditable(EventObject anEvent) {
return true;
}

public boolean shouldSelectCell(EventObject anEvent) {
return true;
}

public void addCellEditorListener(CellEditorListener l) {
}

public void removeCellEditorListener(CellEditorListener l) {
}

protected void fireCellEditing(ChangeEvent e){

}

}



package buttonontable;

import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;


public class MyButtonRenderer extends JButton implements TableCellRenderer {

String buttonStr;
public MyButtonRenderer(String _buttonStr) {
setOpaque(true);
buttonStr = _buttonStr;
setText(buttonStr);
}

public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) {

System.out.println("getTableCellRendererComponent");

if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else{
setForeground(table.getForeground());
setBackground(UIManager.getColor("Button.background"));
}
setText( (value ==null) ? buttonStr : value.toString() );
return this;
}
}

最佳答案

通常,当我必须这样做时,我会引用 http://download.oracle.com/javase/6/docs/api/然后转到页面左侧的 JTable,在 JTable javadoc 页面的开头有一个指向“如何使用表”的链接。靠近底部的是如何在 JTable 中放置按钮的示例。看起来该页面中的部分是“使用其他编辑器”。我通常遵循SUN指定的内容。希望这会有所帮助...

关于java - Jtable 单元格上的 Jbutton 只能工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4461184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com