简体   繁体   English

JTable 单元格字体? (爪哇)

[英]JTable Cell Font? (Java)

The program I am creating should work like Microsoft Excel , except in JAVA .我正在创建的程序应该Microsoft Excel一样工作,除了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!我有用于检测单击了哪个单元格以及使用什么字体正常工作的代码 - 我只是知道如何将Font应用于单元格! Google gave me CellRenderers , but it seems that cell renderers format the cell only when a condition is true . Google给了我CellRenderers ,但似乎只有当条件为true时,单元格渲染器才会格式化单元格。 I want it to format with the specified Font it when it is called!我希望它在调用时使用指定的Font格式化!

Can someone please help me, I am really confused !!!谁能帮帮我,我真的很困惑

I have already looked at the Java Tutorials .我已经看过Java 教程

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.这就是您要查找的内容,此代码片段会更改 jTable 中所有列的字体。我相信稍作修改应该可以涵盖您的场景。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM