문제

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.

도움이 되었습니까?

해결책

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top