Swing:TableCellRenderer 对某些单元格使用粗体
简单的问题,但我似乎无法在网上找到答案。
如何使用自定义 TableCellRenderer 以粗体显示某些表格单元格?
我知道如何使用 TableCellRenderer 逐个单元格设置背景颜色。您执行以下操作:
public class MyTableCellRenderer extends DefaultTableCellRenderer
{
@Override public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component c = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
// modify the component "c" to have whatever attributes you like
// for this particular cell
}
}
我假设更改渲染文本样式类似,但如何将字体设置为与默认表格字体相同但为粗体?
Simple question, but I can't seem to find the answer anywhere online.
How do you use a custom TableCellRenderer to render some of the table cells in boldface?
I know how to use TableCellRenderer to set the background color on a cell-by-cell basis. You do something like:
public class MyTableCellRenderer extends DefaultTableCellRenderer
{
@Override public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component c = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
// modify the component "c" to have whatever attributes you like
// for this particular cell
}
}
I would assume changing the rendering text style is similar, but how do you set the font to be the same as the default table font but in boldface?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您已经可以获得默认的表格字体(我想是
c.getFont()
),那么只需使用deriveFont(Font.BOLD)
就可以了。If you can already get the default table font (which I suppose would be
c.getFont()
), then just usederiveFont(Font.BOLD)
on it.您可能还需要考虑表格行渲染方法这可以让您更灵活地控制要更改字体的单元格。我用它来将所选行的所有列中的文本加粗。
You may also want to consider the Table Row Rendering Approach which may give you a little more flexibility in controlling which cells you change the font for. I've used it for bolding the text in all columns of the selected row.
如此处所述,通过缓存将字体设置为粗体是可行的。
如果您只需将部分文本设置为粗体 - 请使用 HTML。表格单元格渲染器基于
JLabel
(或者您可以返回一个)。将文本转换为 html 将允许几乎任何文本属性更改。我们广泛使用这种技术,没有发现任何明显的性能下降。
Setting font to bold with caching, as described already here, will work.
In case you need to set only part of text in bold - use HTML. Table cell renderers are based on
JLabel
(or you can return one). Converting your text to html will allow almost any text attribute change.We use this technique extensively and did not see any significant performance degradation.
下面是懒人的方法:使用
DefaultTableCellRenderer
(它是JLabel
的子类)并使用 HTML 来指定何时使用粗体字体。它的性能不如定义自己的自定义渲染器并直接控制字体,但代码通常更紧凑,因此适合简单的应用程序。
Here's the lazy man's approach: Use
DefaultTableCellRenderer
(which is a subclass ofJLabel
) and use HTML to specify when you wish to use a bold typeface.It won't be as performant as defining your own custom renderer and controlling the fonts directly, but the code is generally more compact so is good for simple applications.
你也可以用这个..
you can use this also..