资源简介
使用 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
- 上一篇:计算机毕业设计--员工管理系统
- 下一篇:解析AndroidManifest源码
相关资源
- Android 悬浮按钮实现
- Java入门-圆形的按钮(5).zip
- Java 在列表控件中显示单选按钮
- Java 实现按钮控件边框留白
- Java 复选框与单选按钮菜单
- Java 忙碌的按钮控件
- Java 延迟生效的按钮
- Java 实现按钮关键字描红
- Java 单选按钮的简单应用
- Java 禁用MDI窗体控制栏中的“最大化”
- Java 鼠标经过时按钮放大效果
- Android 单击“确定”按钮弹出对话框(
- Android 有背景图片的按钮(Android简单
- Android 按钮的使用技巧(Android简单控
- java swing 表格(JTable)动态添加、删除
- Java 迟到的登录按钮
- Android TableLayout 布局
- Android_Layout 布局 附多种布局(Absolu
- android 自定义表格(tableview)
- 好看的加载按钮界面
- android 应用启动的时弹出的悬浮带有关
- android 仿html table布局可横向 纵向滚动
- 按钮点击的水波纹效果
- android 带删除按钮的输入框
- Android 返回顶部按钮(Android 常用控件
- android ToggleButton 开关按钮
- SwitchButton开关按钮的多种实现方式
- 测试 Netbeans 组件面板内容的简单用法
- JavaFX tableview获取数据demo
-
Java ap
plet 显示图片 并点击按钮 图片
评论
共有 条评论