java JTable 更改列字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16113950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:56:20  来源:igfitidea点击:

JTable change Column Font

javaswingunicodejtablefont-size

提问by Frakcool

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.

例如,在第 0 列中,我希望字体大小为 30,而在第 1-3 列中,我希望字体大小为 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.

这是一个日语学习系统,使用我的 13 号字体根本看不到第一列 unicode 上的汉字,但是如果我将所有表格都设置为更大的尺寸,所有其他列都会变大并且看起来不美好的。

回答by kleopatra

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 中,您基本上需要一个自定义渲染器,它将字体设置为与表格字体不同的字体,fi 在 DefaultTableCellRenderer 的子类中。请注意,在实例化后在 DefaultTableCellRenderer 上设置一次字体将不起作用,因为它在每次调用 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):

另一种选择是渲染器装饰方法,在 SwingX 项目中支持(偏向我无法抗拒:-) 那么上面的将是一个两行(假设表是 JXTable 类型):

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