本文主要是介绍JTable中嵌套JButton并加入JButton的事件处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用JTable还是挺麻烦的,特别是当需要嵌套其他的控件时,现在想在button上添加事件每次按钮一点击就会有获取到点击到的行和列,废话不多说,直接进入正题:
原理
要在JTable中嵌套其他的控件需要修改TableColumnModel的渲染器和编辑器,所以只要实现TableCellRenderer和TableCellEditor,然后通过表格获取TableColumnModel,再通过TableColumnModel设置渲染器和编辑器,分别是这两个方法,一个是setCellRenderer()方法,一个是setCellEditor()方法。
代码
TableCellRenderer的实现类MyButtonRender.java:
package test6;import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;public class MyButtonRender implements TableCellRenderer{private JButton button;public MyButtonRender() {button = new JButton("确认");}@Overridepublic Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) {return button;}}
上面的没有什么需要介绍的,重点是下面的编辑器:MyButtonEditor.java:
package test6;import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultCellEditor;
import javax.swing.JTable;
import javax.swing.JTextField;public class MyButtonEditor extends DefaultCellEditor{private MyButton button;private MyEvent event;public MyButtonEditor() {super(new JTextField());button = new MyButton("确认");button.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//这里调用自定义的事件处理方法event.invoke(e);}});}public MyButtonEditor(MyEvent e) {this();this.event = e;}/*重写编辑器方法,返回一个按钮给JTable*/@Overridepublic Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected, int row, int column) {
// setClickCountToStart(1);
//将这个被点击的按钮所在的行和列放进button里面button.setRow(row);button.setColumn(column);return button;}}
在那里面的,我引用了我自定义的MyButton,从JButton继承而来的,里面我方了两个属性,分别记录了每一个按钮的行和列。
MyButton.java:
package test6;import javax.swing.JButton;public class MyButton extends JButton{private int row;private int column;public int getRow() {return row;}public void setRow(int row) {this.row = row;}public int getColumn() {return column;}public void setColumn(int column) {this.column = column;}public MyButton() {}public MyButton(String name) {super(name);}}
在MyButtonEditor中还有一个我自己定义的MyEvent,里面只定义了一个方法,public abstract void invoke(ActionEvent e);这个方法是要在按钮的事件处理方法被调用,接下来是主函数:
Main.java;
package test6;import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTable;public class Main {public static void main(String[] args) {JFrame frame = new JFrame("test");JTable table = new JTable(5, 4);//实例化MyEvent,这里的invoke写你自己处理按钮被点击的逻辑MyEvent e = new MyEvent() {@Overridepublic void invoke(ActionEvent e) {MyButton button = (MyButton)e.getSource();//打印被点击的行和列System.out.println("row:" + button.getRow() + "column :" + button.getColumn());}};//设置表格的渲染器table.getColumnModel().getColumn(2).setCellRenderer(new MyButtonRender());MyButtonEditor editor = new MyButtonEditor(e);//设置表格的编辑器table.getColumnModel().getColumn(2).setCellEditor(editor);frame.getContentPane().add(table);frame.setSize(400, 300);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}
}
点击之后的运行结果:
row:0column :2
row:0column :2
row:1column :2
row:1column :2
row:2column :2
row:2column :2
row:3column :2
row:3column :2
row:4column :2
row:4column :2
row:4column :2
这篇关于JTable中嵌套JButton并加入JButton的事件处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!