简体   繁体   English

我想在可编辑JTable中的单元格中写入时更改字体大小

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

for this, I tried this method 为此,我尝试了这种方法

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

but by using this I cannot find the appropriate Font at the time of Writing in the cell of the JTable . 但是通过使用它,我在JTable的单元格中在写入时找不到合适的Font。

I attach an image so you can find my question clearly .in this image u can see that at the time of writing font is small but after going to the next cell it becomes to change but I want that big font you can see that in image at the time of writing in the cell. 我附加了一个图像,以便您可以清楚地找到我的问题。在此图像中,您可以看到在编写字体时该字体很小,但是转到下一个单元格后它会发生变化,但是我希望那个大字体可以在该图像中看到在单元格中写入时。

单元格图片

The problem is that table.setFont(new Font("Lucida Console", Font.PLAIN, 18)); 问题是table.setFont(new Font("Lucida Console", Font.PLAIN, 18)); will set the font of the cells when not being edited, it will not even set the font of the header. 将在不编辑时设置单元格的字体,甚至不设置页眉的字体。 When editing, you have override the default setup by defining your own DefaultCellEditor . 编辑时,您可以通过定义自己的DefaultCellEditor覆盖默认设置。 There are two ways to do so, the first (easier and cleaner) way is to create a JTextField and customize it the way you like and then pass it to the DefaultCellEditor constructor. 这样做有两种方法,第一种(更简便的方法)是创建一个JTextField并以您喜欢的方式对其进行自定义,然后将其传递给DefaultCellEditor构造函数。 The second (longer and not as clean) way is to override the getTableCellEditorComponent in the DefaultCellEditor and achieve the same result. 第二种方法(较长且不太干净)是重写DefaultCellEditorgetTableCellEditorComponent并获得相同的结果。 I have included both solutions in a MCVE : 我已经在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();
    }

Maybe you could check how many characters have been typed in and calculate approximately how big the font should be. 也许您可以检查输入了多少个字符并计算出大约应该的字体大小。 Calculation shouldn't be hard, is it? 计算不难,不是吗? And then just change the size in this cell. 然后只需更改此单元格的大小即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM