|
-
December 15th, 2009, 04:32 PM
#1
What to Override?
hey group
I'm using a JTable in an application in which I changed the font of the text written inside the cells and header. My problem, during the 'editing' process of each cell (when the user actually enters text), the font returns back to the normal size. I'm trying to keep the font the same throughout before/after entering text, but I not succeeding.
I pickup through my searching, that I should override getCellRenderer and/or getCellEditor methods. I'm assuming the renderring is fine since my text remains and shows the font I gave it, so I'm working to override getCellEditor().
I'm a bit confused because if I override a method, I then will have to develop the entire routine when all I want to do is adjust the font used within getCellEditor(). So I turned my attention to creating/altering a CellEditor object by creating a custom class that "extends" a TableColumn. Afterall, the cellEditor is used to control a single column of a table. (unless I misread something)
I don't know... I feel I have pieces of ideas but I can't put them all together in a working fashion. Can anybody assist me with this?
Code:
class myCellEditor extends TableColumn
{
public myCellEditor()
{};
public TableCellRenderer getCellRenderer(JTable tbl)
{
Font ftTextCell = new Font("Dialog", Font.PLAIN, 8);
tbl.setFont(ftTextCell);
TableCellRenderer renderer = tbl.getCellRenderer(1,1);
return renderer;
}
protected TableCellEditor cellEditor()
{
Font ftTextCell = new Font("Dialog", Font.PLAIN, 8);
}
}
**my code isn't a piece of art, its a work in progress**
-= the best is yet to come =-
-
December 15th, 2009, 07:27 PM
#2
Re: What to Override?
The cannonical way to do this kind of thing is to supply a custom TableCellRenderer or a custom TableCellEditor, by overriding the getXxxComponent method of the default class to call the superclass method to get the render or edit component, then setting the font on that component before returning it. See Editors & Renderers.
If I had eight hours to chop down a tree, I would spend 6 hours sharpening an axe...
Anon.
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.
-
January 6th, 2010, 03:27 PM
#3
Re: What to Override?
After some time off, I came back to my problem and found a solution. Taken from exampledepot.com, I created a class that overrid only two methods to edit the cell editor of my jtable. Using this code, I was able to alter the text when its being edited but for one column at a time. Since I know the size of my table, I easily recall the line to adjust each column.
Code:
class myTableCellEditor extends AbstractCellEditor implements TableCellEditor{
JComponent comp = new JTextField();
Font ftTxt = new Font("Dialog", Font.PLAIN, 8);
@Override
public Component getTableCellEditorComponent(JTable tbl, Object arg1,
boolean arg2, int rowIndex, int colIndex) {
// TODO Auto-generated method stub
((JTextField)comp).setText((String)arg1);
((JTextField)comp).setFont(ftTxt); /*THIS IS THE KEY LINE THAT I ADDED WHICH CHANGED THE FONT SIZE AS I TYPED IN MY TABLE'S CELL*/
return comp;
}
@Override
public Object getCellEditorValue() {
// TODO Auto-generated method stub
return ((JTextField)comp).getText();
}
}
...and here's what I did when I actually altered my table using the new class I created. My table had five columns, so I repeated the lines five times.
Code:
TableColumn col1 = TABLENAME.getColumnModel().getColumn(0);
TableColumn col2 = TABLENAME.getColumnModel().getColumn(1);
TableColumn col3 = TABLENAME.getColumnModel().getColumn(2);
TableColumn col4 = TABLENAME.getColumnModel().getColumn(3);
TableColumn col5 = TABLENAME.getColumnModel().getColumn(4);
col1.setCellEditor(new myTableCellEditor());
col2.setCellEditor(new myTableCellEditor());
col3.setCellEditor(new myTableCellEditor());
col4.setCellEditor(new myTableCellEditor());
col5.setCellEditor(new myTableCellEditor());
-= the best is yet to come =-
-
January 7th, 2010, 03:45 PM
#4
Re: What to Override?
If you just subclass DefaultCellEditor and override getTableCellEditorComponent to call the superclass method, then set the font on the Component returned, you don't have to worry about setting the text or overriding getCellEditorValue - the superclass handles that. That's what it's for. You just pass in a JTextField to the constructor and it will use that:
Code:
TableCellEditor tce = new DefaultCellEditor(new JTextField()) {
Font ftTxt = new Font("Dialog", Font.PLAIN, 8);
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row,
int column)
{
Component comp = super.getTableCellEditorComponent(table, value, isSelected, row, column);
comp.setFont(ftTxt);
return comp;
}
};
Simples...
When you find yourself repeating lines of code multiple times like that, consider using an array or ArrayList, and looping. Try to remove duplicate code wherever you can.
Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it in the same way twice...
C. Alexander
Last edited by dlorde; January 7th, 2010 at 03:47 PM.
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.
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
|