将 Jtable 行字体更改为粗体 [英] changing Jtable row font to bold

查看:59
本文介绍了将 Jtable 行字体更改为粗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Java 中有一个 JTable,我想知道如何根据整数值将字体修改为粗体.我目前有这张表,我想根据教练中的人数更改行的字体.我还是个新手,我不知道如何做到这一点.我需要一个可以执行以下操作的解决方案:

I had a JTable in java and i want to know how i can modify the font to bold depending on a integer value. I Currently have this table and i want to change the font of the row depending on the number of people in the coach. i am still new to swing and i have no idea about how to do this. i need a solution that would do something like:

 if (num_people >17 && num_people<26){
     //change row font to bold
}

这是我需要的示例,如果按照我已经拥有的图像进行操作:

this is an example of what i would need if following the image i have below of what i already have:

链接到我当前的代码:https://gist.github.com/anonymous/015022f7ad55ff9664e2edaea6a6a5>

Link to my current code: https://gist.github.com/anonymous/015022f7ad55ff9664e2edaea6a58d5a

推荐答案

public class CellRenderer extends DefaultTableCellRenderer {

private static final long serialVersionUID = 1L;

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
        int column) {
    super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

    if (value>17 value<26) {
        this.setValue(table.getValueAt(row, column));
        this.setFont(this.getFont().deriveFont(Font.BOLD));
    }
    return this;
}
}

这是带有粗体字体的单元格渲染器的外观.我没有测试它.您在初始化 JTable 后调用 table.getColumnModel().getColumn(x).setCellRenderer(new CellRenderer()); 并为要应用该字体的每一列调用它.

this is how a cell renderer would look like with bold font. i didn't test it. you call table.getColumnModel().getColumn(x).setCellRenderer(new CellRenderer()); after you initialized your JTable and call it for every column you want that font to apply.

这篇关于将 Jtable 行字体更改为粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆