2013-04-11 53 views
1

我已经设置了默认的字体在我的JTable作为展示如下更改JTable中单元格的字体,而编辑它

myTable.setFont(new java.awt.Font("Verdana", 1, 10)); 

我想表明我的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(); 
    } 
} 

下面是我将CustomCellEditor附加到我的表的代码。

myTable.getColumnModel().getColumn(1).setCellEditor(new MyTableCellEditor()); 

但这个代码似乎并不work.The细胞字体变小,而编辑的,一旦我完成编辑并回车,将默认的JTable字体,我设定(宋体10)呈effect.Why出现这样的情况?我已经将CustomCellEditor字体设置为(Verdana 12)给我的单元格。

+0

对我来说就像一个魅力。当我编辑单元格时,字体变得更粗大。考虑发布[SSCCE](http://sscce.org)以了解您的问题的底部。 – 2013-04-11 16:04:45

+0

要小心:cellEditor的实现是**无效**(因为它不通知它的听众) – kleopatra 2013-04-19 13:16:02

回答

3

不要为此创建新类。只需更改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); 
+0

你的答案伎俩! – cherit 2013-04-11 16:12:42