JTable单元字体? (Java) [英] JTable Cell Font? (Java)
问题描述
我正在创建的程序 应该与Microsoft Excel
相似,但在JAVA
中除外.它应该还支持单元格格式化(这是我的问题).我有用于检测单击哪个单元格以及使用哪种字体才能正常工作的代码-我无法弄清楚如何将Font
应用于该单元格! Google
给了我CellRenderers
,但是似乎单元格渲染器仅在条件为true
时才格式化该单元格.我希望它在调用时使用指定的Font
进行格式化!
有人 请 可以帮助我,我 真的很困惑 !!!
我已经看过 Java教程. /p>
如果之前曾问过这个问题,我深表歉意!
这就是您要寻找的内容,此代码段更改了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;
}
}
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;
}
}
这篇关于JTable单元字体? (Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!