JTable、自定义标题渲染器和排序图标
在 JTable 标题上设置自定义渲染器时,我获得了预期的视觉行为(边框、字体、对齐方式等),但无法获得通常在对行进行排序时出现的 LaF 排序图标。
这是设置自定义标头渲染器的代码:
Enumeration<TableColumn> columns = getColumnModel().getColumns();
while (columns.hasMoreElements())
columns.nextElement().setHeaderRenderer(new XDeliveryTableHeaderRenderer());
这是自定义标头渲染器的摘录:
public class MyTableHeaderRenderer extends JLabel implements TableCellRenderer {
private static final Font labelFont = new Font("Arial", Font.BOLD, 11);
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setFont(labelFont);
setHorizontalAlignment(SwingConstants.CENTER);
setText(value.toString());
setBorder(BorderFactory.createEtchedBorder());
return this;
}
}
有任何提示吗?
While setting a custom renderer on a JTable header I get the expected visual behavior (borders, font, alignment, ...) but I can't manage to get the LaF sorting icons that usually appear when the rows are sorted.
This is the code for setting the custom header renderer:
Enumeration<TableColumn> columns = getColumnModel().getColumns();
while (columns.hasMoreElements())
columns.nextElement().setHeaderRenderer(new XDeliveryTableHeaderRenderer());
This is an excerpt of the custom header renderer:
public class MyTableHeaderRenderer extends JLabel implements TableCellRenderer {
private static final Font labelFont = new Font("Arial", Font.BOLD, 11);
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setFont(labelFont);
setHorizontalAlignment(SwingConstants.CENTER);
setText(value.toString());
setBorder(BorderFactory.createEtchedBorder());
return this;
}
}
Any hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试委托给 L&F 安装的渲染器:
正如 kleopatra 警告的那样,这可能不是最稳定的解决方案,请参阅 这个错误报告,我刚刚在生产中得到。该问题的报告者建议使用自定义 Table/TableColumn 子类来更新 TableColumn#getDefaultRenderer 中的委托渲染器。
Try delegating to the L&F installed renderer:
As kleopatra warned, this might not be the most stable solution, see this bug report which I just got in production. The reporter of that issue suggest using a custom Table/TableColumn sub classes which updating the delegate renderer in TableColumn#getDefaultRenderer.