我想在可编辑 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
的单元格中写入时,我无法通过使用它找到合适的字体。
我附上了一张图片,这样你就可以清楚地找到我的问题。在这张图片中你可以看到在写的时候字体很小但是在进入下一个单元格之后它就变了但是我想要那个大字体你可以在单元格中写入时在图像中看到。
也许你可以检查输入了多少个字符并计算大约字体应该有多大。
计算应该不难吧?然后只需更改此单元格中的大小。
问题是table.setFont(new Font("Lucida Console", Font.PLAIN, 18));
在不编辑的时候会设置单元格的字体,甚至不会设置表头的字体。编辑时,您通过定义自己的 DefaultCellEditor
覆盖了默认设置。有两种方法可以做到这一点,第一种(更简单和更简洁)的方法是创建一个 JTextField 并按照您喜欢的方式对其进行自定义,然后将其传递给 DefaultCellEditor
构造函数。第二种(更长但不那么干净)的方法是覆盖 DefaultCellEditor
中的 getTableCellEditorComponent
并获得相同的结果。我在 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();
}
为此,我尝试了这个方法
table.setFont(new Font("Lucida Console", Font.PLAIN, 18));
但是在 JTable
的单元格中写入时,我无法通过使用它找到合适的字体。
我附上了一张图片,这样你就可以清楚地找到我的问题。在这张图片中你可以看到在写的时候字体很小但是在进入下一个单元格之后它就变了但是我想要那个大字体你可以在单元格中写入时在图像中看到。
也许你可以检查输入了多少个字符并计算大约字体应该有多大。 计算应该不难吧?然后只需更改此单元格中的大小。
问题是table.setFont(new Font("Lucida Console", Font.PLAIN, 18));
在不编辑的时候会设置单元格的字体,甚至不会设置表头的字体。编辑时,您通过定义自己的 DefaultCellEditor
覆盖了默认设置。有两种方法可以做到这一点,第一种(更简单和更简洁)的方法是创建一个 JTextField 并按照您喜欢的方式对其进行自定义,然后将其传递给 DefaultCellEditor
构造函数。第二种(更长但不那么干净)的方法是覆盖 DefaultCellEditor
中的 getTableCellEditorComponent
并获得相同的结果。我在 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();
}