Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTable change Column Font

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.

like image 468
Frakcool Avatar asked Apr 19 '13 21:04

Frakcool


1 Answers

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);
like image 138
kleopatra Avatar answered Nov 14 '22 22:11

kleopatra