[英]Modify text alignment in a JTable cell
I have found a way to allocate the cell in a table, which seems to be a Component
我找到了一种分配表中单元格的方法,该表似乎是一个
Component
JTable table = new JTable(...)
{
public Component prepareRenderer(
TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
// add custom rendering here
return c;
}
};
In the code, c
is the Component, but I cannot find a method in Component
to modify text alignment. 在代码中,
c
是Component,但是我无法在Component
找到用于修改文本对齐方式的方法。 Anything wrong about this approach? 这种方法有什么问题吗?
Use DefaultTableCellRenderer
for that purposes, it has setHorizontalAlignment()
method : 为此使用
DefaultTableCellRenderer
,它具有setHorizontalAlignment()
方法:
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(){
@Override
public Component getTableCellRendererComponent(JTable arg0,Object arg1, boolean arg2, boolean arg3, int arg4, int arg5) {
Component tableCellRendererComponent = super.getTableCellRendererComponent(arg0, arg1, arg2, arg3, arg4, arg5);
int align = DefaultTableCellRenderer.CENTER;
if(condition){
align = DefaultTableCellRenderer.LEFT;
}
((DefaultTableCellRenderer)tableCellRendererComponent).setHorizontalAlignment(align);
return tableCellRendererComponent;
}
};
t.getColumnModel().getColumn(COLUMN).setCellRenderer(renderer);
COLUMN
is target column, condition
is condition for switching. COLUMN
是目标列, condition
是切换条件。
(J)Component
havent this method(s) implemented in API (J)Component
没有此方法在API中实现
there are two ways 有两种方法
by casting (J)Component
to JLabel
通过将
(J)Component
为JLabel
set this value for class
managed by getColumnClass
为
getColumnClass
管理的class
设置此值
. 。
DefaultTableCellRenderer stringRenderer = (DefaultTableCellRenderer)
table.getDefaultRenderer(String.class);
stringRenderer.setHorizontalAlignment(SwingConstants.CENTER);
I am going to make a conclusion. 我将得出一个结论。
For this specifiec problem, we can divide it into two: 对于此特定问题,我们可以将其分为两个部分:
To allocate the cell, my originally way and the way alex2410 should be both useful. 要分配单元,我最初的方式和alex2410的方式都应该有用。
JTable table = new JTable(...)
{
public Component prepareRenderer(
TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
// add custom rendering here
return c;
}
};
another 另一个
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(){
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(arg0, arg1, arg2, arg3, arg4, arg5);
// add custom rendering here
return c;
}
};
t.getColumnModel().getColumn(COLUMN).setCellRenderer(renderer);
And for alignment, the only method I found useful is: 对于对齐,我发现唯一有用的方法是:
((DefaultTableCellRenderer)c).setHorizontalAlignment(SwingConstants.center);
In mKorbel's suggestion, you can cast the Component
c to JLabel
, but I failed to to that. 在mKorbel的建议中,您可以将
Component
c强制转换为JLabel
,但我未能做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.