如何在Netbeans中自定义jTable标头列的字体大小? [英] how to customize jTable header column font size in Netbeans?

查看:265
本文介绍了如何在Netbeans中自定义jTable标头列的字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更改Netbeans中的jtable标头字体大小.但还不能.无论如何,表行字体大小已成功更改.

I tried to change jtable header font size in Netbeans. but couldn't yet. anyway the table rows font size is changed successfully.

这是我使用的方式:

更改后的输出:

问题:标题字体大小未更改.但我也想改变它.所以请帮我怎么做.

Problem : The Header font size is not changed. but I want to change that also. so pls help me how to do.

推荐答案

一种方法是使用UIManager并将默认的Font替换为所需的

One way would be to use the UIManager and replace the default Font with the one you want

Font font = UIManager.getFont("TableHeader.font");
font = font.deriveFont(48f);
UIManager.put("TableHeader.font", font);

将替换系统中所有表使用的字体

Which will replace the font used by all the tables in the system

另一种方法是为要更改的列提供自定义TableCellRenderer,这虽然需要做更多的工作,但是可以提供更大的灵活性,因为您可以决定要在何处应用它们.您可以将其包装在自己的自定义JTableHeader中,但我只是提供一些基本想法.

Another way is to provide a custom TableCellRenderer for the columns you want to change, it's a little more work, but provides more flexibility as you can decide where you want to apply them. You could wrap this inside your own custom JTableHeader, but I'm just providing some basic ideas.

public class HeaderRenderer implements UIResource, TableCellRenderer {

    private TableCellRenderer original;

    public HeaderRenderer(TableCellRenderer original) {
        this.original = original;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table,
                                                                                                 Object value, boolean isSelected, boolean hasFocus, int row,
                                                                                                 int column) {
        Component comp = original.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        comp.setFont(comp.getFont().deriveFont(Font.BOLD));
        return comp;
    }

}

哪个安装方式类似于...

Which is installed using something like...

HeaderRenderer header = new HeaderRenderer(table.getTableHeader().getDefaultRenderer());
TableColumnModel columnModel = table.getColumnModel();
columnModel.getColumn(0).setHeaderRenderer(header);

并产生类似...

为此想法致谢Kleopatra

总而言之,您将不得不动手并编写一些代码,表单编辑器将无法为您做任何事情

The long and short of it is, you're going to have to get your hands dirty and write some code, the form editor won't do everything for you

这篇关于如何在Netbeans中自定义jTable标头列的字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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