如何设置JTable某一个cell的Renderer

colorfish 2004-03-29 11:11:11
我知道table.getColumn("字段").setCellRenderer(new ButtonRenderer())可以设置某一列的Renderer
但如何设置JTable某一个cell的Renderer
...全文
122 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
onefox 2004-04-18
  • 打赏
  • 举报
回复
http://www.javaresearch.org/article/showarticle.jsp?column=331&thread=8684

看中间那段
ac669 2004-04-18
  • 打赏
  • 举报
回复
不明白!!!!! :)
我也正碰到这个问题.
我想改变某个单元格的颜色该怎么做???
Leemaasn 2004-03-29
  • 打赏
  • 举报
回复
不明白。。

先Up
cqandy 2004-03-29
  • 打赏
  • 举报
回复
给你个例子:
thisRenderer = new DefaultTableCellRenderer() {

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected,
boolean hasFocus, int row,
int column)
{
if (column ==0){File thisfile=new File(((File)value).getName());
return super.getTableCellRendererComponent(table, (File)thisfile, isSelected,
hasFocus, row,
column);

}

else
{

JButton bn=new JButton("button");
return bn;
}}
使用 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.
c# xptable NET中最强,最全功能的表格控件 可以定制一个ListView,能够在列中插入图像、下拉框、可上下调整的数字、进度条等等。 [功能] 全定制可视化界面 支持XP风格 轻易添加再定制的控件 可隐藏列 行、列、单元可以被Disable 每个单元、列可以有Tooltip 等等…… [XPTable] XPTable包含下面的组件: 1. Table, 2. ColumnModel 和它的 Columns, 3. TableModel 和它的 Row 和 Cell, 4. Renderer 5. Editor [翻译] Mathew Hall.著XPTable - .NET ListView meets Java's JTable [简介] 由于项目需要,我需要定制一个ListView,它必须能够在列中插入图像、下拉框、可上下调整的数字、进度条等等。由于已经有了一个Java下的背景,我将简单地基于那个JTable封装。 [功能] 全定制可视化界面 支持XP风格 轻易添加再定制的控件 可隐藏列 行、列、单元可以被Disable 每个单元、列可以有Tooltip 等等…… [XPTable] XPTable包含下面的组件: 1. Table, 2. ColumnModel 和它的 Columns, 3. TableModel 和它的 Row 和 Cell, 4. Renderer 5. Editor [控件使用] 首先加载控件到Toolbox上(添加一个Item,引用XPTable.dll) 然后,拖动Table, ColumnModel 和 TableModel到Form上,设置Table的ColumnModel 和 TableModel属性,添加Column到ColumnModel,添加Row 和 Cell到TableModel.

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧