Changing JTable Cell's Font while editing it

微笑、不失礼 提交于 2019-12-11 13:15:55

问题


I have set the default font in my JTable as show below

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.

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.

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 ? I have already set CustomCellEditor font as ( Verdana 12 ) to my cells.


回答1:


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);


来源:https://stackoverflow.com/questions/15953422/changing-jtable-cells-font-while-editing-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!