programTableaddMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
showPopup(e);
}
public void mouseReleased(MouseEvent e) {
showPopup(e);
}
private void showPopup(MouseEvent e) {
if (eisPopupTrigger()) {
popupshow(egetComponent(), egetX(), egetY());
int selRow = programTablerowAtPoint(egetPoint());
programTablesetRowSelectionInterval(selRow, selRow);
}
}
});
有时候我们需要在JTable中嵌入JButton,使得点击某个JTable单元格时,用户感觉通过JButton触发了响应事件。下面我们看看为JTable单元格添加按钮效果和响应事件的方法。以下示例实现的效果为:点击jtable中的button,button上的数字自动加1,并将加1后得到的数字正确显示在button上。
一、添加按钮显示效果
JTable中,单元格的数据显示默认是JTable的效果。如果我们想要按钮显示的效果的话,需要实现swing接口:javaxswingtableTableCellRenderer,来改变单元格默认的渲染方法。
package TableButton;import javaawtBorderLayout;
import javaawtComponent;
import javaxswingJButton;
import javaxswingJPanel;
import javaxswingJTable;
import javaxswingtableTableCellRenderer;
public class MyButtonRenderer implements TableCellRenderer {
private JPanel panel;
private JButton button;
private int num;
public MyButtonRenderer() {
initButton();
initPanel();
paneladd(button, BorderLayoutCENTER);
}
private void initButton() {
button = new JButton();
}
private void initPanel() {
panel = new JPanel();
panelsetLayout(new BorderLayout());
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
num = (Integer) value;
buttonsetText(value == null "" : StringvalueOf(value));
return panel;
}
}
二、添加按钮响应事件
第1步中我们为表格添加了渲染器,但是渲染器只负责显示效果,要想点击”按钮“之后有响应,还得跟单元格的编辑器有关。点击表格是会触发表格的编辑时间,所以我们按钮的响应事件可以写在编辑器中,我们需要修改表格的默认编辑器实现。
swing中有个类javaxswingDefaultCellEditor提供对单元格内插入JTextField/JComboBox/JCheckbox这3种控件,但是不提供对JButton的支持(17里不支持,不代表以后不会支持),所以我们得用到另外一个类自己写TableCellEditor,这个类是avaxswingAbstractCellEditor。
package TableButton;import javaawtBorderLayout;
import javaawtComponent;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingAbstractCellEditor;
import javaxswingJButton;
import javaxswingJOptionPane;
import javaxswingJPanel;
import javaxswingJTable;
import javaxswingtableTableCellEditor;
public class MyButtonEditor extends AbstractCellEditor implements
TableCellEditor {
/
serialVersionUID
/
private static final long serialVersionUID = -6546334664166791132L;
private JPanel panel;
private JButton button;
private int num;
public MyButtonEditor() {
initButton();
initPanel();
paneladd(thisbutton, BorderLayoutCENTER);
}
private void initButton() {
button = new JButton();
buttonaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int res = JOptionPaneshowConfirmDialog(null,
"Do you want to add 1 to it", "choose one",
JOptionPaneYES_NO_OPTION);
if(res == JOptionPaneYES_OPTION){
num++;
}
//stopped!!!!
fireEditingStopped();
}
});
}
private void initPanel() {
panel = new JPanel();
panelsetLayout(new BorderLayout());
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
num = (Integer) value;
buttonsetText(value == null "" : StringvalueOf(value));
return panel;
}
@Override
public Object getCellEditorValue() {
return num;
}
}
需要注意的是得重写TableModel的isCellEditable方法,因为只有为重写Editor的列开启了可编辑功能,单元格才能被编辑,也就才能出发按钮单击事件。在下一段代码中有体现。
另外要注意得禁止掉JTable的行选中功能。否则我们再点击按钮时,JTable不知道是响应为”行选中“还是”按钮单击事件“。
三、测试代码
package TableButton;import javaawtColor;
import javaawtEventQueue;
import javaxswingJFrame;
import javaxswingJPanel;
import javaxswingJScrollPane;
import javaxswingJTable;
import javaxswingtableDefaultTableModel;
public class TestTable {
private JFrame frame;
private JTable table;
private Object[][] data = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
public static void main(String[] args) {
EventQueueinvokeLater(new Runnable() {
public void run() {
try {
TestTable window = new TestTable();
windowframesetVisible(true);
} catch (Exception e) {
eprintStackTrace();
}
}
});
}
public TestTable() {
frame = new JFrame();
framesetBounds(100, 100, 450, 300);
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
framegetContentPane()setLayout(null);
JPanel panel = new JPanel();
panelsetBounds(10, 10, 414, 242);
framegetContentPane()add(panel);
panelsetLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPanesetBounds(10, 10, 394, 222);
paneladd(scrollPane);
table = new JTable();
scrollPanesetViewportView(table);
tablesetModel(new DefaultTableModel() {
@Override
public Object getValueAt(int row, int column) {
return data[row][column];
}
@Override
public int getRowCount() {
return 3;
}
@Override
public int getColumnCount() {
return 3;
}
@Override
public void setValueAt(Object aValue, int row, int column){
data[row][column] = aValue;
fireTableCellUpdated(row, column);
}
@Override
public boolean isCellEditable(int row, int column) {
if (column == 2) {
return true;
} else {
return false;
}
}
});
tablegetColumnModel()getColumn(2)setCellEditor(
new MyButtonEditor());
tablegetColumnModel()getColumn(2)setCellRenderer(
new MyButtonRenderer());
tablesetRowSelectionAllowed(false);
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)