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 called!
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]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.