-
June 3rd, 2010, 12:12 AM
#1
Font is changing While editing cell in JTable - Need a fix
Hi all,
The font type of the cell is set to 'Arial Unicode MS'. While editing the cell during the runtime, the font of the cell is changed to the default font.
Thanks.
-
June 3rd, 2010, 04:23 AM
#2
Re: Font is changing While editing cell in JTable - Need a fix
How do you set the font type of the cell?
JTable uses two different components to show cell contents - a TableCellRenderer to display the contents when not being edited and a TableCellEditor to edit the contents. You need to set the font for both if you want them both to be different from the default. See Editors & Renderers.
In the particular is contained the universal...
J. Joyce
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
June 3rd, 2010, 06:00 AM
#3
Re: Font is changing While editing cell in JTable - Need a fix
Thanks for your reply.
I found the solution. 've set the font of the Textfield.
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;
}
-
June 3rd, 2010, 06:10 AM
#4
Re: Font is changing While editing cell in JTable - Need a fix
Originally Posted by dlorde
How do you set the font type of the cell?
Sorry, My question was wrong. I didn't set the font for a single cell but for a column.
-
June 3rd, 2010, 06:24 AM
#5
Re: Font is changing While editing cell in JTable - Need a fix
Originally Posted by dlorde
How do you set the font type of the cell?
for (int i = 0; i < jTable1.getColumnCount(); i ++) {
TableColumn col = jTable1.getColumnModel().getColumn(i);
col.setCellEditor(new MyTableCellEditor());
col.setCellRenderer(new MyTableCellRenderer());
}
public class MyTableCellRenderer
extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (row == 0 && column == 0) {
component.setFont(new java.awt.Font("Arial Unicode MS", 0, 12));
}
else {
// Some other font.
}
return component;
}
}
-
June 4th, 2010, 04:04 AM
#6
Re: Font is changing While editing cell in JTable - Need a fix
If you're setting the same renderer and editor for every column, you can just call the default mutators:
Code:
table.setDefaultRenderer(Object.class, new MyTableCellRenderer());
table.setDefaultEditor(Object.class, new MyTableCellEditor());
This simplifies and saves you creating multiple copies of renderer and editor when you only need one.
Controlling complexity is the essence of computer programming...
B. Kernighan
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
June 4th, 2010, 10:26 AM
#7
Re: Font is changing While editing cell in JTable - Need a fix
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|