[英]Changing JTable Cell's Font while editing it
I have set the default font in my JTable as show below 我在JTable中设置了默认字体,如下所示
myTable.setFont(new java.awt.Font("Verdana", 1, 10));
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. 我想在我的JTable中显示一个更大的字体,而一些数据正在输入到单元格中。所以我使用了MyTableCellEditor自定义类。
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();
}
}
Below is the code where I attached the CustomCellEditor to my table. 下面是我将CustomCellEditor附加到我的表的代码。
myTable.getColumnModel().getColumn(1).setCellEditor(new MyTableCellEditor());
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 ? 但是这个代码似乎不起作用。单元格字体在编辑时变小,一旦我完成编辑并点击回车,我设置的默认JTable字体(Verdana 10)生效。为什么会发生这种情况? I have already set CustomCellEditor font as ( Verdana 12 ) to my cells. 我已经将CustomCellEditor字体设置为(Verdana 12)到我的单元格。
Don't create a new class for this. 不要为此创建新类。 Just change the property of the DefaultCellEditor: 只需更改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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.