为此,我尝试了这种方法
table.setFont(new Font("Lucida Console", Font.PLAIN, 18));
,但是使用此方法,我在JTable
的单元格中写入时找不到合适的字体。
我附上一张图片,这样您就可以清楚地找到我的问题。在这张图片中,您可以看到在编写字体时该字体很小,但是转到下一个单元格后它会发生变化,但是我希望该字体较大,您可以看到在单元格中写入时图像中的值。
答案 0 :(得分:2)
问题在于table.setFont(new Font("Lucida Console", Font.PLAIN, 18));
将在不被编辑时设置单元格的字体,甚至不设置标题的字体。编辑时,您可以通过定义自己的DefaultCellEditor
覆盖默认设置。这样做有两种方法,第一种(更简便的方法)是创建一个JTextField并以您喜欢的方式对其进行自定义,然后将其传递给DefaultCellEditor
构造函数。第二种方法(较长且不太干净)是覆盖getTableCellEditorComponent
中的DefaultCellEditor
并获得相同的结果。我在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();
}
答案 1 :(得分:0)
也许您可以检查输入了多少个字符并计算出大概的字体大小。 计算不难,不是吗?然后只需更改此单元格的大小即可。