how to customize jTable header column font size in Netbeans?

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

I tried to change jtable header font size in Netbeans. 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.

回答1:

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

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

And produces something like...

Credit to Kleopatra for this idea

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!