java在表格单元中加入button按钮_java Jtable 如何动态添加button按钮

展开全部

有时候我们需要在JTable中嵌入JButton,使得点击某个JTable单元格时,用户感觉通过JButton触发了响应事件。下面我们看看为JTable单元格添加按钮32313133353236313431303231363533e59b9ee7ad9431333431343134效果和响应事件的方法。以下示例实现的效果为:点击jtable中的button,button上的数字自动加1,并将加1后得到的数字正确显示在button上。

一、添加按钮显示效果

JTable中,单元格的数据显示默认是JTable的效果。如果我们想要按钮显示的效果的话,需要实现swing接口:javax.swing.table.TableCellRenderer,来改变单元格默认的渲染方法。package TableButton;

import java.awt.BorderLayout;

import java.awt.Component;

import javax.swing.JButton;

import javax.swing.JPanel;

import javax.swing.JTable;

import javax.swing.table.TableCellRenderer;

public class MyButtonRenderer implements TableCellRenderer {

private JPanel panel;

private JButton button;

private int num;

public MyButtonRenderer() {

initButton();

initPanel();

panel.add(button, BorderLayout.CENTER);

}

private void initButton() {

button = new JButton();

}

private void initPanel() {

panel = new JPanel();

panel.setLayout(new BorderLayout());

}

public Component getTableCellRendererComponent(JTable table, Object value,

boolean isSelected, boolean hasFocus, int row, int column) {

num = (Integer) value;

button.setText(value == null ? "" : String.valueOf(value));

return panel;

}

}

二、添加按钮响应事件

第1步中我们为表格添加了渲染器,但是渲染器只负责显示效果,要想点击”按钮“之后有响应,还得跟单元格的编辑器有关。点击表格是会触发表格的编辑时间,所以我们按钮的响应事件可以写在编辑器中,我们需要修改表格的默认编辑器实现。

swing中有个类javax.swing.DefaultCellEditor提供对单元格内插入JTextField/JComboBox/JCheckbox这3种控件,但是不提供对JButton的支持(1.7里不支持,不代表以后不会支持),所以我们得用到另外一个类自己写TableCellEditor,这个类是avax.swing.AbstractCellEditor。package TableButton;

import java.awt.BorderLayout;

import java.awt.Component;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.AbstractCellEditor;

import javax.swing.JButton;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTable;

import javax.swing.table.TableCellEditor;

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();

panel.add(this.button, BorderLayout.CENTER);

}

private void initButton() {

button = new JButton();

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

int res = JOptionPane.showConfirmDialog(null,

"Do you want to add 1 to it?", "choose one",

JOptionPane.YES_NO_OPTION);

if(res ==  JOptionPane.YES_OPTION){

num++;

}

//stopped!!!!

fireEditingStopped();

}

});

}

private void initPanel() {

panel = new JPanel();

panel.setLayout(new BorderLayout());

}

@Override

public Component getTableCellEditorComponent(JTable table, Object value,

boolean isSelected, int row, int column) {

num = (Integer) value;

button.setText(value == null ? "" : String.valueOf(value));

return panel;

}

@Override

public Object getCellEditorValue() {

return num;

}

}

需要注意的是得重写TableModel的isCellEditable方法,因为只有为重写Editor的列开启了可编辑功能,单元格才能被编辑,也就才能出发按钮单击事件。在下一段代码中有体现。

另外要注意得禁止掉JTable的行选中功能。否则我们再点击按钮时,JTable不知道是响应为”行选中“还是”按钮单击事件“。

三、测试代码package TableButton;

import java.awt.Color;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.table.DefaultTableModel;

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) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

TestTable window = new TestTable();

window.frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

public TestTable() {

frame = new JFrame();

frame.setBounds(100, 100, 450, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

JPanel panel = new JPanel();

panel.setBounds(10, 10, 414, 242);

frame.getContentPane().add(panel);

panel.setLayout(null);

JScrollPane scrollPane = new JScrollPane();

scrollPane.setBounds(10, 10, 394, 222);

panel.add(scrollPane);

table = new JTable();

scrollPane.setViewportView(table);

table.setModel(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;

}

}

});

table.getColumnModel().getColumn(2).setCellEditor(

new MyButtonEditor());

table.getColumnModel().getColumn(2).setCellRenderer(

new MyButtonRenderer());

table.setRowSelectionAllowed(false);

}

}

使用 AbstractTableModel 构建Table 在表格添加JButton按钮,之前在网上找了2天没有找到好用的程序,最终终于找到一个好用的例子。 不要使,我退你们分。。 sing the Swing JTable class can quickly become a sticky business when you want to customize it to your specific needs. First you must become familiar with how the JTable class is organized. Individual cells are rendered by TableCellRenderer implementations. The table contents are represented by an implementation of the TableModel interface. By default, JTable uses DefaultTableCellRenderer to draw its cells. DefaultTableCellRenderer recognizes a few primitive types, rendering them as strings, and can even display Boolean types as checkboxes. But it defaults to displaying the value returned by toString() for types it does not specifically handle. You have to provide your own TableCellRenderer implementation if you want to display buttons in a JTable. The TableCellRenderer interface contains only one method, getTableCellRendererComponent(...), which returns a java.awt.Component that knows how to draw the contents of a specific cell. Usually, getTableCellRendererComponent() will return the same component for every cell of a column, to avoid the unnecessary use of extra memory. But when the contents of a cell is itself a component, it is all right to return that component as the renderer. Therefore, the first step towards having JButtons display correctly in a JTable is to create a TableCellRenderer implementation that returns the JButton contained in the cell being rendered. In the accompanying code listing, JTableButtonRenderer demonstrates how to do this. Even after creating a custom TableCellRenderer, you're still not done. The TableModel associated with a given JTable does not only keep track of the contents of each cell, but it also keeps track of the class of data stored in each column. DefaultTableModel is designed to work with DefaultTableCellRenderer and will return java.lang.String.class for columns containing data types that it does not specifically handle. The exact method that does this is getColumnClass(int column). Your second step is to create a TableModel implementation that returns JButton.class for cells that contain JButtons. JTableButtonModel shows one way to do this. It just returns the result of getClass() for each piece of cell data. At this point, you're almost done, but not quite. What's the use of putting a JButton in a JTable if you can't press the darn thing? By default, JTable will not forward mouse events to components contained in its cells. If you want to be able to press the buttons you add to JTable, you have to create your own MouseListener that forwards events to the JButton cells. JTableButtonMouseListener demonstrates how you could do this.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值