java jtable 按钮_java web 怎么在jtable中添加按钮?

本文展示了如何在Java Web的JTable组件中添加按钮,并处理按钮点击事件。通过创建自定义的TableModel和CellRenderer,将按钮集成到表格的特定列中,并在用户点击按钮时触发相应操作。
摘要由CSDN通过智能技术生成

展开全部

java web在jtable中添加按钮的示例如下:import java.awt.BorderLayout;

import 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=0;i

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 !e68a843231313335323631343130323136353331333337623364= 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;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 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、付费专栏及课程。

余额充值