且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 JTable 中居中单元格

更新时间:2023-12-03 11:30:46

我已将您的示例缩减为基本要素:

I've pared your example down to the essentials:

  • 您的自定义渲染器 r 应调节对齐方式以及大小和颜色.

  • Your custom renderer, r, should condition the alignment, as well as size and color.

覆盖模型中的模型方法,而不是视图中的方法.

Override model methods in the model, not in the view.

Swing GUI 对象应该事件调度线程.

Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

根据需要使用 deriveFont().

另请参阅本教程部分,了解如何渲染器 被选中.

See also this tutorial section on how renderers are selected.

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.*;
import java.awt.Color;
import javax.swing.JLabel;

class ver_his_sign extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ver_his_sign();
            }
        });
    }

    ver_his_sign() {

        DefaultTableModel model = new DefaultTableModel(get_data(), get_header()) {
            @Override
            public boolean isCellEditable(int row, int col) {
                switch (col) {
                    case 0:
                        return false;
                    case 1:
                        return false;
                    case 2:
                        return true;
                    default:
                        return false;
                }
            }

            @Override
            public Class getColumnClass(int column) {
                switch (column) {
                    case 0:
                        return String.class;
                    case 1:
                        return String.class;
                    case 2:
                        return Boolean.class;
                    default:
                        return Boolean.class;
                }
            }
        };
        DefaultTableCellRenderer r = new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object
                value, boolean isSelected, boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(
                    table, value, isSelected, hasFocus, row, column);
                setForeground(Color.blue);
                setHorizontalAlignment(JLabel.CENTER);
                setFont(getFont().deriveFont(50f));
                return this;
            }
        };
        JTable table = new JTable(model);
        table.setRowHeight(table.getRowHeight() + 50);
        table.getColumnModel().getColumn(1).setCellRenderer(r);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.add(new JScrollPane(table));
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    Object[][] get_data() {
        Object data[][] = new Object[][]{
            {"Uno, 1", "u4e00", true},
            {"Uno, 1", "u4e01", true},
            {"Uno, 1", "u4e02", true},
            {"Uno, 1", "u4e03", true},
            {"Uno, 1", "u4e04", true}
        };
        return data;
    }

    String[] get_header() {
        String header[] = new String[]{"SIGNIFICADO", "KANJI", "Agregar"};
        return header;
    }
}