问题描述
我已经在JTable中设置了默认字体,如下所示
I have set the default font in my JTable as show below
myTable.setFont(new java.awt.Font("Verdana", 1, 10));
我想在JTable中显示更大的字体,同时在单元格中键入一些数据.因此,我使用了 MyTableCellEditor 自定义类.
I wanted to show a bigger font in my JTable,while some data is being typed into the cells.So I used MyTableCellEditor custom class.
public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {
JComponent component = new JTextField();
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
int rowIndex, int vColIndex) {
((JTextField) component).setText((String) value);
((JTextField) component).setFont(new Font("Verdana", 1, 12));
return component;
}
public Object getCellEditorValue() {
return ((JTextField) component).getText();
}
}
下面是我将CustomCellEditor附加到表中的代码.
Below is the code where I attached the CustomCellEditor to my table.
myTable.getColumnModel().getColumn(1).setCellEditor(new MyTableCellEditor());
但是此代码似乎不起作用.在编辑时,单元字体变小了,一旦我完成编辑并按Enter,我设置的默认JTable字体(Verdana 10)就生效了.为什么会这样?我已经将CustomCellEditor字体设置为(Verdana 12).
But this code do not seem to work.The cells font becomes small while editing and once I finish editing and hit enter,the default JTable font which I set ( Verdana 10 ) takes effect.Why is this happening ? I have already set CustomCellEditor font as ( Verdana 12 ) to my cells.
推荐答案
请勿为此创建新类.只需更改DefaultCellEditor的属性即可:
Don't create a new class for this. Just change the property of the DefaultCellEditor:
JTextField textField = new JTextField();
textField.setFont(new Font("Verdana", 1, 12));
textField.setBorder(new LineBorder(Color.BLACK));
DefaultCellEditor dce = new DefaultCellEditor( textField );
myTable.getColumnModel().getColumn(1).setCellEditor(dce);
这篇关于在编辑时更改JTable单元格的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!