开发者

JTable Cell Font? (Java)

The program I am creating should work like Microsoft Excel, except in JAVA. It should also support cell formatting (Which is my problem). I have the code for detecting which cell is clicked, and what font to use working properly - I just can not figure out how to apply the Font to the cell! Google gave me CellRenderers, but it seems that cell renderers format the cell only when a condition is true. I want it to format with the specified Font it when it is 开发者_如何学Ccalled!

Can someone please help me, I am really confused!!!

I have already looked at the Java Tutorials.

My apologies if this question has been asked before!


this is what you are looking for,, this code snippet changes the font of all columns in a jTable.. I'm sure a slight modification should get your scenario covered.

for (int i = 0; i < jTable1.getColumnCount(); i ++) {
    TableColumn col = jTable1.getColumnModel().getColumn(i);
    col.setCellEditor(new 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 java.awt.Font("Arial Unicode MS", 0, 12));
        return component;
    }
}


This will change the font for all cells in the table - even when new columns or rows are added:

JTable table;
......
Object dce = table.getDefaultEditor(Object.class);
if(dce instanceof DefaultCellEditor) {
    ((DefaultCellEditor) dce).getComponent().setFont([your font]);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜