Jtable 单元格上的 Jbutton 只能工作一次
我对 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;
}
}
I am quite new with Java Swing and have some problems with Jbutton on a Jtable cell.
The problem is Jbutton works only once and then it is not functioning.
After first time, when I click on again nothing happens, getTableCellEditorComponent is called only once.
I want it to work as a regular button.
My Button editor and button renderer classes are as follows:
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,当我必须这样做时,我会参考 http://download.oracle.com/ javase/6/docs/api/ 然后转到页面左侧的 JTable,在 JTable javadoc 页面的开头有一个指向“如何使用表”的链接。靠近底部的是如何在 JTable 中放置按钮的示例。看起来该页面中的部分是“使用其他编辑器”。我通常遵循SUN指定的内容。希望这会有所帮助......
Usually when I have to do this I refer to http://download.oracle.com/javase/6/docs/api/ then go to JTable on the left of the page and at the beginning of the JTable javadoc page there is a link to "How to use Tables". Near the bottom is an example of how to put a button in a JTable. Looks like the section in that page is "Using Other Editors". I usually follow what SUN specified. Hopefully that helps a bit...
如果这意味着您想在按下按钮时调用操作,那么您可以查看 表格按钮列是一种方法。
If that means you want to invoke an Action when the button is pressed then you can check out Table Button Column for one approach.
尝试按
Esc
并再次单击。如果现在有效,那就是焦点问题。不要解决这个问题,请按照 camickr 的回答 或其衍生产品。
基本大纲应该是这样的:
渲染器:
编辑器:
用法:
Try to
Esc
and click again. If it works now, it's a focus issue.Don't fix that, follow camickr's answer or a spin-off of it.
A basic outline should like this:
Renderer:
Editor:
Usage: