Swing:TableCellRenderer 对某些单元格使用粗体

发布于 2024-08-20 07:35:04 字数 691 浏览 5 评论 0原文

简单的问题,但我似乎无法在网上找到答案。

如何使用自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

别靠近我心 2024-08-27 07:35:04

如果您已经可以获得默认的表格字体(我想是 c.getFont()),那么只需使用 deriveFont(Font.BOLD) 就可以了。

If you can already get the default table font (which I suppose would be c.getFont()), then just use deriveFont(Font.BOLD) on it.

执着的年纪 2024-08-27 07:35:04

您可能还需要考虑表格行渲染方法这可以让您更灵活地控制要更改字体的单元格。我用它来将所选行的所有列中的文本加粗。

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.

孤独患者 2024-08-27 07:35:04

如此处所述,通过缓存将字体设置为粗体是可行的。

如果您只需将部分文本设置为粗体 - 请使用 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.

诗笺 2024-08-27 07:35:04

下面是懒人的方法:使用 DefaultTableCellRenderer (它是 JLabel 的子类)并使用 HTML 来指定何时使用粗体字体。

它的性能不如定义自己的自定义渲染器并直接控制字体,但代码通常更紧凑,因此适合简单的应用程序。

/**
 * Renderer implementation for rendering Strings.
 * Strings beginning with 'A' are rendered in bold.
 */
public class MyRenderer extends DefaultTableCellRenderer {
  public Component getTableCellRendererComponent(JTable table,
                                               Object value,
                                               boolean isSelected,
                                               boolean hasFocus,
                                               int row,
                                               int column) {

    String txt = String.valueOf(value);

    if (txt != null && txt.startsWith("A")) {
      // Reassign value as an HTML string.
      // Obviously need to consider replacing HTML special characters
      // if doing this properly.
      value = String.format("<body><b>%s</b></body>", txt);
    }

    // Delegate to superclass which will set the label text, background, etc.
    return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
  }
}

Here's the lazy man's approach: Use DefaultTableCellRenderer (which is a subclass of JLabel) 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.

/**
 * Renderer implementation for rendering Strings.
 * Strings beginning with 'A' are rendered in bold.
 */
public class MyRenderer extends DefaultTableCellRenderer {
  public Component getTableCellRendererComponent(JTable table,
                                               Object value,
                                               boolean isSelected,
                                               boolean hasFocus,
                                               int row,
                                               int column) {

    String txt = String.valueOf(value);

    if (txt != null && txt.startsWith("A")) {
      // Reassign value as an HTML string.
      // Obviously need to consider replacing HTML special characters
      // if doing this properly.
      value = String.format("<body><b>%s</b></body>", txt);
    }

    // Delegate to superclass which will set the label text, background, etc.
    return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
  }
}
爱你是孤单的心事 2024-08-27 07:35:04

你也可以用这个..

        class SampleRenderer extends DefaultTableCellRenderer
        {

        public Component getJtableCellRendererComponent(Jtable table,Object value,boolean     isSelected , boolean hasFocus , int row, int column)

        {

        JLabel c = (JLabel)super.getJtableCellRendererComponent(table,value,isSelected ,hasFocus , row, column);

        Font f = c.getFont();

        c.setFont(f.getName(),Font.BOLD,f.getSize()));

        return c;

    }

}

you can use this also..

        class SampleRenderer extends DefaultTableCellRenderer
        {

        public Component getJtableCellRendererComponent(Jtable table,Object value,boolean     isSelected , boolean hasFocus , int row, int column)

        {

        JLabel c = (JLabel)super.getJtableCellRendererComponent(table,value,isSelected ,hasFocus , row, column);

        Font f = c.getFont();

        c.setFont(f.getName(),Font.BOLD,f.getSize()));

        return c;

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文