簡體   English   中英

我想在可編輯JTable中的單元格中寫入時更改字體大小

[英]I want to change the Font Size at the time of writing in a cell in the Editable JTable

為此,我嘗試了這種方法

 table.setFont(new Font("Lucida Console", Font.PLAIN, 18));

但是通過使用它,我在JTable的單元格中在寫入時找不到合適的Font。

我附加了一個圖像,以便您可以清楚地找到我的問題。在此圖像中,您可以看到在編寫字體時該字體很小,但是轉到下一個單元格后它會發生變化,但是我希望那個大字體可以在該圖像中看到在單元格中寫入時。

單元格圖片

問題是table.setFont(new Font("Lucida Console", Font.PLAIN, 18)); 將在不編輯時設置單元格的字體,甚至不設置頁眉的字體。 編輯時,您可以通過定義自己的DefaultCellEditor覆蓋默認設置。 這樣做有兩種方法,第一種(更簡便的方法)是創建一個JTextField並以您喜歡的方式對其進行自定義,然后將其傳遞給DefaultCellEditor構造函數。 第二種方法(較長且不太干凈)是重寫DefaultCellEditorgetTableCellEditorComponent並獲得相同的結果。 我已經在MCVE中包括了兩種解決方案:

在此處輸入圖片說明

import java.awt.Color;
import java.awt.Font;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class Example extends JFrame {

    private final JTable table;
    private final String[] header = new String[]{"Column 0", "Column 1", "Column 2", "Column 3"};
    private String[][] data = new String[][]{
        {"(0,0)", "(1,0)", "(2,0)", "(3,0)"},
        {"(0,1)", "(1,1)", "(2,1)", "(3,1)"},
        {"(0,2)", "(1,2)", "(2,2)", "(3,2)"},
        {"(0,3)", "(1,3)", "(2,3)", "(3,3)"}};
    private final Font tableFont = new Font("Lucida Console", Font.PLAIN, 18);

    public Example() {
        table = new JTable(data, header);
        table.getTableHeader().setFont(tableFont);//font of the header
        table.setFont(tableFont);//set the font of the whole table

        //Since each cell is editable, you could think about it as a JTextField. You can create a
        //new JTextField and customize it. Then, you pass it as the new cell editor to the columns
        //of the JTable.
        JTextField textField = new JTextField();
        textField.setFont(tableFont);//this is what you need.
        //Extra changes, no boarder and selection colour is yellow... just to get the point across.
        textField.setBorder(null);
        textField.setSelectionColor(Color.YELLOW);

        //Create DefaultCellEditor and pass the textfield to the constructor.
        DefaultCellEditor customCellEditor = new DefaultCellEditor(textField);
        //Loop through all the columns and set the cell editor as the customized one.
        for (int i = 0; i < table.getColumnCount(); i++) {
            table.getColumnModel().getColumn(i).setCellEditor(customCellEditor);
        }
        /*
        OR, don't create a JTextField and use the following instead:
        DefaultCellEditor customCellEditor2 = new DefaultCellEditor(new JTextField()) {

            @Override
            public java.awt.Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {
                JTextField result = (JTextField) super.getTableCellEditorComponent(table, value,
                    isSelected, row, column);
                result.setFont(tableFont);//this is what you need.
                result.setBorder(null);
                result.setSelectionColor(Color.YELLOW);
                return result;
            }
        };

        //Loop through all the columns and set the cell editor as the customized one.
        for (int i = 0; i < table.getColumnCount(); i++) {
            table.getColumnModel().getColumn(i).setCellEditor(customCellEditor2);
        }
         */

        //probably, you should make the height of the cells larger.
        for (int i = 0; i < table.getRowCount(); i++) {
            table.setRowHeight(i, 25);
        }

        add(new JScrollPane(table));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new Example();
    }

也許您可以檢查輸入了多少個字符並計算出大約應該的字體大小。 計算不難,不是嗎? 然后只需更改此單元格的大小即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM