Netbeans 与 CustomTableCellRenderer 相关的问题
我正在尝试向 JTable 添加不同的数据类型(以使 JTable 以正确的方式对整数列进行排序),同时渲染表以查看奇数行比偶数行更暗。
我的 TableCellRenderer 的代码如下:
public class MyCustomTableCellRenderer extends DefaultTableCellRenderer
{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean lected, boolean hasFocus, int row, int column)
{
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (isSelected)
cell.setBackground( new Color(180,180 ,255) );
else
if( row%2 == 0)
cell.setBackground( new Color(220,220 ,255) );
else
cell.setBackground( Color.white );
return cell;
}
}
DataModel 代码如下:
DefaultTableModel MydataModel = new DefaultTableModel()
{
@Override
public boolean isCellEditable(int row, int col)
{
return false;
}
@Override
public Class getColumnClass(int c)
{
String colname = getColumnName(c);
if (colname.contains("INT")) return Integer.class;
return String.class;
}
};
我使用 new Integer( some_integer ) 添加新的整数值到表中。
运行代码时,表格以正确的方式呈现(奇数行和偶数行具有不同的颜色),具有整数值的列除外。它们只是白色的,就像它们没有受到渲染器的影响一样。
请向我解释一下,如何解决这个问题。 提前致谢。 最大限度。
I am trying to add a different data types (to make JTable sort integer columns in a proper way) to my JTable and at the same time to render the table to see odd rows darker than even rows.
I have the following code of my TableCellRenderer:
public class MyCustomTableCellRenderer extends DefaultTableCellRenderer
{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean lected, boolean hasFocus, int row, int column)
{
Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (isSelected)
cell.setBackground( new Color(180,180 ,255) );
else
if( row%2 == 0)
cell.setBackground( new Color(220,220 ,255) );
else
cell.setBackground( Color.white );
return cell;
}
}
And the DataModel code is like this:
DefaultTableModel MydataModel = new DefaultTableModel()
{
@Override
public boolean isCellEditable(int row, int col)
{
return false;
}
@Override
public Class getColumnClass(int c)
{
String colname = getColumnName(c);
if (colname.contains("INT")) return Integer.class;
return String.class;
}
};
I am adding new integer values using new Integer( some_integer ) to the table.
When running the code, the table is rendered in a proper way (odd and even rows have different colors), except columns with integer values. They are just white, like they were not affected by the Renderer.
Please, explain to me, how this problem can be solved.
Thanks in advance.
Max.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于您如何设置渲染器。
使用传递 Integer 类和渲染器的方法
。
It depend on how you set the renderer.
Use the method
passing Integer class and your renderer.
通过使用
prepareRenderer
,您可以通过int modelRow = ConvertRowIndexToModel(row);
消除将表视图与表模型同步的问题,这也可以解决损坏的问题剥离的背景。您可以在此处查看一个示例,或者最好的例子是 @camickr 的 表格行渲染。By using
prepareRenderer
, you can eliminate the issue of synchronizing the table view with the table model viaint modelRow = convertRowIndexToModel(row);
this would also solve your issues with the broken stripped background. There's an example that you can see here, or the best example around is @camickr's Table Row Rendering.