有时候我们需要在JTable中嵌入JButton,使得点击某个JTable单元格时,用户感觉通过JButton触发了响应事件。下面我们看看为JTable单元格添加按钮效果和响应事件的方法。以下示例实现的效果为:点击jtable中的button,button上的数字自动加1,并将加1后得到的数字正确显示在button上。
一、添加按钮显示效果
JTable中,单元格的数据显示默认是JTable的效果。如果我们想要按钮显示的效果的话,需要实现swing接口:javax.swing.table.TableCellRenderer,来改变单元格默认的渲染方法。
package TableButtonimport 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 TableButtonimport 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 TableButtonimport 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)
}
}
java web在jtable中添加按钮的示例如下:
import java.awt.BorderLayoutimport java.awt.Color
import java.awt.Component
import java.awt.Dimension
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import java.io.File
import javax.swing.BorderFactory
import javax.swing.ImageIcon
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.JScrollPane
import javax.swing.JTable
import javax.swing.UIManager
import javax.swing.border.Border
import javax.swing.border.EmptyBorder
import javax.swing.table.AbstractTableModel
import javax.swing.table.TableCellRenderer
public class JTableButton extends JPanel {
private JTable table
private JScrollPane scrollPane
private JButton[] buttons
private String path = System.getProperty("user.dir") + File.separator
+ "images" + File.separator
public JTableButton() {
setBorder(BorderFactory.createLineBorder(Color.red, 1))
init()
}
private void init() {
String headName[] = { "Name", "age", "sex", "adress", "image" }
buttons = new JButton[5]
for(int i=0i<buttons.lengthi++){
buttons[i] = new JButton(""+i)
}
Object obj[][] = {
{ "LiMing", 23, Boolean.TRUE, buttons[0],
new ImageIcon(path + "icon.png") },
{ "ZhangSan", 25, Boolean.TRUE,buttons[1],
new ImageIcon(path + "icon.png") },
{ "WangWu", 21, Boolean.FALSE, buttons[2],
new ImageIcon(path + "icon.png") },
{ "LiSi", 28, Boolean.TRUE, buttons[3],
new ImageIcon(path + "icon.png") },
{ "LuBo", 20, Boolean.FALSE, buttons[4],
new ImageIcon(path + "icon.png") }, }
table = new JTable(new MyTableModel(headName,obj))
table.setDefaultRenderer(JButton.class, new ComboBoxCellRenderer())
scrollPane = new JScrollPane(table)
setLayout(new BorderLayout())
add(scrollPane, BorderLayout.CENTER)
addHandler()
}
private void addHandler(){
//添加事件
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
System.out.println("table")
int row = table.getSelectedRow()
int column = table.getSelectedColumn()
System.out.println("row="+row+":"+"column="+column)
if(column==3){
//处理button事件写在这里...
System.out.println(((JButton)table.getValueAt(row, column)).getText())
}
}
})
}
public static void main(String[] args) {
JFrame frame = new JFrame()
frame.add(new JTableButton())
frame.setSize(new Dimension(800, 400))
frame.setVisible(true)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}
class MyTableModel extends AbstractTableModel {
private String headName[]
private Object obj[][]
public MyTableModel() {
super()
}
public MyTableModel(String[] headName, Object[][] obj) {
this()
this.headName = headName
this.obj = obj
}
public int getColumnCount() {
return headName.length
}
public int getRowCount() {
return obj.length
}
public Object getValueAt(int r, int c) {
return obj[r][c]
}
public String getColumnName(int c) {
return headName[c]
}
public Class<?> getColumnClass(int columnIndex) {
return obj[0][columnIndex].getClass()
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
if (columnIndex == 3 || columnIndex == 4) {
return false
}
return true
}
}
}
class ComboBoxCellRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
JButton cmb = (JButton) value
if (isSelected) {
cmb.setForeground(table.getSelectionForeground())
cmb.setBackground(table.getSelectionBackground())
} else {
cmb
.setForeground((unselectedForeground != null) ? unselectedForeground
: table.getForeground())
cmb
.setBackground((unselectedBackground != null) ? unselectedBackground
: table.getBackground())
}
cmb.setFont(table.getFont())
if (hasFocus) {
cmb
.setBorder(UIManager
.getBorder("Table.focusCellHighlightBorder"))
if (!isSelected && table.isCellEditable(row, column)) {
Color col
col = UIManager.getColor("Table.focusCellForeground")
if (col != null) {
cmb.setForeground(col)
}
col = UIManager.getColor("Table.focusCellBackground")
if (col != null) {
cmb.setBackground(col)
}
}
} else {
cmb.setBorder(noFocusBorder)
}
return cmb
}
protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1)
private Color unselectedForeground
private Color unselectedBackground
}
你写的按钮计算吧,这个类是一个Applet,其中有一个按钮,这个类本身也是按钮的动作监听器,所以实现了ActionListener 接口用来给按钮调用(也就是 actionPerformed方法),其中的参数e是事件参数,当点击按钮时会发送给按钮使用。e.getSource() == b 就是如果点击是b这个按钮,当监听器给一个按钮使用时没有必要加此判断,e.getSource就是获取发生事件的源对象,比如c = new JButton("点我有次数哦")
f.getContentPane().add(c)
c.setVisible(true)
c.addActionListener(this)
此时又增加了一个按钮,就可以用e.getSource() 判断点击的是哪一个按钮。
建议你把面向对象搞懂在学swing编程吧,很容易看懂的
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)