简体   繁体   English

如何在Netbeans中自定义jTable标头列的字体大小?

[英]how to customize jTable header column font size in Netbeans?

I tried to change jtable header font size in Netbeans . 我试图在Netbeans更改jtable标头字体大小。 but couldn't yet. 但还不能。 anyway the table rows font size is changed successfully. 无论如何,表行字体大小已成功更改。

Here is the way I used: 这是我使用的方式:

?

The output after changes: 更改后的输出:

?

Problem : The Header font size is not changed. 问题:标题字体大小未更改。 but I want to change that also. 但我也想改变它。 so pls help me how to do. 所以请帮我怎么做。

One way would be to use the UIManager and replace the default Font with the one you want 一种方法是使用UIManager并将默认Font替换为所需的Font

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 它将替换系统中所有表使用的字体

列标题

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. 另一种方法是为要更改的列提供自定义TableCellRenderer ,这虽然需要做更多的工作,但是可以提供更大的灵活性,因为您可以决定要在何处应用它们。 You could wrap this inside your own custom JTableHeader , but I'm just providing some basic ideas. 您可以将其包装在您自己的自定义JTableHeader ,但我只是提供一些基本思想。

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);

And produces something like... 并产生类似...

自定义列标题

Credit to Kleopatra for this idea 感谢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 总而言之,您将不得不动手并编写一些代码,表单编辑器不会为您做任何事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM