我正在尝试创建一个在一列中具有可单击按钮的JTable。我一直在关注这里找到的教程:https://www.youtube.com/watch?v=3LiSHPqbuic。基本上,当您单击其中一个按钮时,会弹出一个消息框。我的代码几乎可以工作,但我必须双击按钮而不是单击。我已经玩了很长一段时间并没有取得多大进展。谁能看到我出错的地方?
包按钮列;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellRenderer;
public class ButtonColumn extends JFrame {
public ButtonColumn() {
super("Button Column Example");
//Data for table
Object[][] data=
{
{"1","Man Utd",new Integer(2013),"21"},
{"2","Man City",new Integer(2014),"3"},
{"3","Chelsea",new Integer(2015),"7"},
{"4","Arsenal",new Integer(1999),"10"},
{"5","Liverpool",new Integer(1990),"19"},
{"6","Everton",new Integer(1974),"1"},
};
//Column header
String columnHeaders[]={"Position","Team","Last Year Won","Trophies"};
//Create table object
JTable table=new JTable(data,columnHeaders);
//Set custom renderer to teams column
table.getColumnModel().getColumn(1).setCellRenderer(new ButtonRenderer());;
//Set custom editor to team column
table.getColumnModel().getColumn(1).setCellEditor(new ButtonEditor(new JTextField()));
//Scroll pane
JScrollPane pane=new JScrollPane(table);
getContentPane().add(pane);
setSize(450,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
ButtonColumn bc = new ButtonColumn();
bc.setVisible(true);
}
}
//Button renderer class
class ButtonRenderer extends JButton implements TableCellRenderer
{
//Constructir
public ButtonRenderer(){
setOpaque(true);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object obj,
boolean selected, boolean focused, int row, int col) {
setText((obj==null) ? "":obj.toString());
return this;
}
}
//Button editor class
class ButtonEditor extends DefaultCellEditor
{
protected JButton btn;
private String lbl;
private Boolean clicked;
public ButtonEditor(JTextField txt){
super(txt);
btn=new JButton();
btn.setOpaque(true);
//When button is clicked
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
fireEditingStopped();
}
});
}
//Overide a couple of methods
public Component getTableCellEditorComponent(JTable table, Object obj,
boolean selected, int row, int col) {
//Set button text
lbl=(obj==null) ? "":obj.toString();
btn.setText(lbl);
clicked=true;
return btn;
}
//If button cell value changes (clicked)
@Override
public Object getCellEditorValue(){
if(clicked)
{
JOptionPane.showMessageDialog(btn, lbl+" Clicked");
}
clicked = false;
return new String(lbl);
}
@Override
public boolean stopCellEditing(){
clicked=false;
return super.stopCellEditing();
}
@Override
protected void fireEditingStopped(){
super.fireEditingStopped();
}
}
答案 0 :(得分:2)
请务必查阅文档以获取有关您正在创建的课程的更多信息。
有关详细信息,请查看JavaDocs for DefaultCellEditor
和Using Other Editors。
基本上,您需要修改isCellEditable
DefaultCellEditor
方法
class ButtonEditor extends DefaultCellEditor {
//...
@Override
public boolean isCellEditable(EventObject anEvent) {
return anEvent instanceof MouseEvent && ((MouseEvent) anEvent).getClickCount() == 1 && SwingUtilities.isLeftMouseButton((MouseEvent) anEvent);
}
}
或者,如果您真的想要简单,只需使用setClickCountToStart
class ButtonEditor extends DefaultCellEditor {
//...
public ButtonEditor(JTextField txt) {
super(txt);
setClickCountToStart(1);
//...
}
}