JTable change Column Font

15,115

In core JTable you basically need a custom renderer which sets the Font to something different from the table's font, f.i. in a subclass of DefaultTableCellRenderer. Note that setting the font on DefaultTableCellRenderer once after instantiation won't work because it's reset on each call to getTableCellRendererComponent.

JTable table = new JTable(new AncientSwingTeam());
// the default renderer uses the table's 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's font is reset
// to the table's font always
// r.setFont(font);
// set the custom renderer for first column
table.getColumnModel().getColumn(0).setCellRenderer(r);

An alternative is the renderer decoration approach, supported in the SwingX project (biased me can't resist :-) Then the above would be a two-liner (assuming table is of type JXTable):

Highlighter hl = new FontHighlighter(font);
table.getColumnExt(0).setHighlighter(hl);
Share:
15,115
Frakcool
Author by

Frakcool

Sr Developer with 7+ years of experience Java Swing / Desktop apps fanatic Trying to become an iOS developer, have 1 year of experience on it and trying to increase that experience I love learning new things and working with new technologies. I really enjoy hard problems and challenges.

Updated on June 04, 2022

Comments

  • Frakcool
    Frakcool almost 2 years

    I'm making a table where I want to make the first column with a higher Font Size.

    For example in column 0 I want Font size of 30 and on columns 1-3 y want Font size of 13.

    Here's 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 learning system, and Kanjis on unicode on 1st column aren't visible at all with my 13 size font, but if I make all the table on a higher size, all the other columns get bigger and it doesn't looks fine.

  • mKorbel
    mKorbel about 11 years
    please whats wrong (excluding Nimbus) with table.getTableHeader().setFont(new Font(null, Font.PLAIN, 23));
  • kleopatra
    kleopatra about 11 years
    @mKorbel nothing wrong :-) Only that's unrelated to the question as I understand it: it's about a per-column font, that's different from the table's font.
  • camickr
    camickr about 11 years
    +1, Sorry, I didn't even see your answer. I'll delete mine so yours appears first.
  • Frakcool
    Frakcool about 11 years
    @kleopatra I tried this but went all wrong, I'm still a student from java, can you help me getting that code into my code? or maybe explain me with simple words how it works? 'cause I still can't understand very well how it works
  • kleopatra
    kleopatra about 11 years
    @Frakcool hmm.. there is not much more to explain, it's rather basic and all there's to do. If you have problems with it, you might consider reading the chapter about rendering in the tutorial (link is in the swing tag wiki, that is the info tab above)