JTable change Column Font

I am making a table where I want to make the first column with a higher font size.

For example, in column 0, I want the font size to be 30, and in columns 1-3, the font size is 13.

Here is my code

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import javax.swing.table.*; public class kanji_list extends JFrame { kanji_list(){ JTable table = new JTable(); JScrollPane scroll = new JScrollPane(); Image icon = Toolkit.getDefaultToolkit().getImage("JLPT.jpg"); ImageIcon ima = new ImageIcon("JLPT.jpg"); DefaultTableModel model = new DefaultTableModel(get_data(), get_header()); table = new JTable(model){ public boolean isCellEditable(int rowIndex, int vColIndex){ return false; } }; JTableHeader th = table.getTableHeader(); TableColumnModel tcm = th.getColumnModel(); TableColumn column = null; table.setFont(new Font("Microsoft JhengHei", Font.BOLD, 13)); for (int i = 0; i < 4; i++) { column = table.getColumnModel().getColumn(i); DefaultTableCellRenderer tcr = new DefaultTableCellRenderer(); tcr.setHorizontalAlignment(SwingConstants.CENTER); column.setCellRenderer(tcr); if (i==0) { column.setPreferredWidth(50); } else{ if(i==1){ column.setPreferredWidth(175); } else{ if(i==2){ column.setPreferredWidth(200); } else{ column.setPreferredWidth(875); } } } } table.setRowHeight(table.getRowHeight()+30); table.setModel(model); scroll.add(table); this.add(scroll); this.setTitle("Katakana"); this.setSize(1350, 700); this.setIconImage(icon); this.setVisible(true); this.setLocationRelativeTo(null); scroll.setViewportView(table); } Object [][]get_data(){ Object data[][] = new Object[][]{ {"\u4e00", "Uno, 1", "ICHI, ITSU", "hito-, hitotsu"}, {"\u4e8c", "Dos, 2", "NI, JI", "futa, futatsu, futatabi"}, {"\u4e09", "Tres, 3", "SAN, JOU", "mi, mitsu, mittsu"}, {"\u99c5", "EstaciΓ³n", "EKI", ""} }; return data; } String []get_header(){ String header [] = new String[]{"KANJI", "SIGNIFICADO", "LECTURA ON", "LECTURA KUN"}; return header; } } 

This is a Japanese language learning system, and Unicode kanji in the 1st column are not visible at all with my font size 13, but if I increase the size of the table, all the other columns will become larger and it will not look good.

+5
source share
1 answer

In the main JTable, you basically need a custom renderer that sets the font to something other than the table font, fi in the DefaultTableCellRenderer subclass. Please note that setting the font to DefaultTableCellRenderer once after creating the instance will not work, because it will reset each time getTableCellRendererComponent is called.

 JTable table = new JTable(new AncientSwingTeam()); // the default renderer uses the table font, // so set it as appropriate table.setFont(fontToUseForAllColumnsExceptFirst); // a custom renderer which uses a special font DefaultTableCellRenderer r = new DefaultTableCellRenderer() { Font font = fontToUseForFirstColumn; @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); setFont(font); return this; } }; // doesn't work because the default renderer font is reset // to the table font always // r.setFont(font); // set the custom renderer for first column table.getColumnModel().getColumn(0).setCellRenderer(r); 

An alternative is the rendering decoration approach supported in the SwingX project (the offset doesn’t hold me back :-) Then there will be a two-line above (it is assumed that the table is of type JXTable):

 Highlighter hl = new FontHighlighter(font); table.getColumnExt(0).setHighlighter(hl); 
+12
source

All Articles