• 大小: 4KB
    文件类型: .java
    金币: 1
    下载: 0 次
    发布日期: 2021-05-07
  • 语言: Java
  • 标签: Table  JButton  按钮  

资源简介

使用 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

资源截图

代码片段和文件信息

package jTable;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.frame;
import java.awt.Window;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.Jframe;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;

public final class JTableButton extends Jframe {
private static final long serialVersionUID = 1L;
private JTable __table;
private JScrollPane __scrollPane;

public JTableButton() {
super(“JTableButton Demo“);
TableCellRenderer defaultRenderer;

__table = new JTable(new JTableButtonModel());
defaultRenderer = __table.getDefaultRenderer(JButton.class);
__table.setDefaultRenderer(JButton.class new JTableButtonRenderer(
defaultRenderer));

__table.setPreferredScrollableViewportSize(new Dimension(400 200));
__table.addMouseListener(new JTableButtonMouseListener(__table));

__scrollPane = new JScrollPane(__table);
setContentPane(__scrollPane);
}

public static void main(String[] args) {
frame frame;
WindowListener exitListener;

exitListener = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Window window = e.getWindow();
window.setVisible(false);
window.dispose();
System.exit(0);
}
};

frame = new JTableButton();
frame.addWindowListener(exitListener);
frame.pack();
frame.setVisible(true);
}
}

class JTableButtonRenderer implements TableCellRenderer {
private TableCellRenderer __defaultRenderer;

public JTableButtonRenderer(TableCellRenderer renderer) {
__defaultRenderer = renderer;
}

public Component getTableCellRendererComponent(JTable table object value
boolean isSelected boolean hasFocus int row int column) {
if (value instanceof Component) {
return (Component) value;

} else {
return __defaultRenderer.getTableCellRendererComponent(table
value isSelected hasFocus row column);
}
}
}

clas

评论

共有 条评论