aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable number renderer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable number renderer" Watch "JTable number renderer" New topic
Author

JTable number renderer

Rick Taylor
Greenhorn

Joined: Oct 10, 2006
Posts: 12
I am writing a cash register program that requires users to enter transaction data into a JTable. The DefaultTableModel seems to be the best option for displaying an empty table, except that I wanted to use a number renderer to render certain columns (quantity, price, etc). So I subclassed DefaultTableModel and added a getColumnClass method. The problem is, the columns that return Number.class are not editable. Am I missing something? I thought the JTable class included a default renderer for numbers ...

Thanks in advance. My code is below:

import javax.swing.table.*;

public class ItemsModel extends DefaultTableModel {

public ItemsModel() {
super();
}

public ItemsModel (Object[] columnNames, int rowCount) {
super(columnNames, rowCount);
}

public Class getColumnClass(int columnIndex) {
switch (columnIndex) {
case(2):
case(3):
case(4):
case(8):
return Number.class;
default:
return Object.class;
}
}

}


Rick Taylor
prashant gour
Ranch Hand

Joined: Feb 07, 2006
Posts: 45
override the following method too.


/**
* Returns <code>true</code> if the specified cell is editable, and
* <code>false</code> if it is not. This implementation returns
* <code>false</code> for all arguments.
*
* @param rowIndex rowIndex the row index of the cell.
* @param columnIndex columnIndex the column index of the cell.
* @return boolean true if cell is editable otherwise false
*/
@Override
public boolean isCellEditable(int rowIndex, int columnIndex)
{
return true;
}
Rick Taylor
Greenhorn

Joined: Oct 10, 2006
Posts: 12
Thank you for the response. I tried overriding the isCellEditable method, but that had no effect. Then I tried revising the getColumnClass method to return Integer.class or Float.class instead of Number.class and got some unexpected results. Both classes make the column cells editable, but for each of the number columns, either the display alignment or data type is off. This is what happens when I enter the number 7 in each column (see comments):

public Class getColumnClass(int columnIndex) {
switch (columnIndex) {
case(0):
return Object.class; // left-aligned 7 in Column 0
case(1):
return Object.class; // left-aligned 7 in Column 1
case(2):
return Float.class; // left-aligned 7.0 in Column 2
case(3):
return Float.class; // right-aligned 7 in Column 3
case(4):
return Float.class; // right-aligned 7 in Column 4
case(5):
return Object.class; // left-aligned 7 in Column 5
case(6):
return Object.class; // left-aligned 7 in Column 5
case(7):
return Object.class; // left-aligned 7 in Column 5
case(8):
return Float.class; // right-aligned 7 in Column 8
default:
return Object.class;
}
}

My questions are: why does the number 7 display as a float, but left-aligned, at columnIndex 2? And why do the floats at columnIndex 3, 4, and 8 display as integers? I have tried writing this method using an if/else statement and got the same results. Also, why does Number.class render the cells uneditable, but Integer.class and Float.class don't?

Thank you for your insights.
[ October 18, 2006: Message edited by: Rick Taylor ]
Rick Taylor
Greenhorn

Joined: Oct 10, 2006
Posts: 12
For what it's worth ...

I have corrected the problem of my JTable cells containing the wrong data types by instantiating a DefaultTableCellRenderer for each of the columns that will contain numbers. But floats, doubles, and integers are still left-aligned after the data is entered into the cell (although the cursor appears on the right-hand side of the cell prior to editing), and any cell whose class is Number.class is still not editable.

As an experiment, I tried creating a JTable and populating it with initial values. Again, I overrode the getColumnClass method to return Number.class, Float.class, and Integer.class for certain columns. In each case, the values are right-aligned. Also, the float and integer values can be edited, but cells of class Number are still not editable.

Does anybody know why I can't edit Numbers? Is this normal for JTables, or could it be something in my code? Thanks for your help.
alex d�az
Greenhorn

Joined: Oct 22, 2006
Posts: 6
Hello...

I am using netbeans 5 for create fast swing interfaces.

I am creating a JTable for display items, quantity, prices, etc.

I have tried differents ways to align right cells the price columns without be able to get good effort, this continue left align.

I am not sure if there is any condition in netbeans to make able to change the align by column.

If you have idea about this, please, help me.
Naga Malleshwararao
Greenhorn

Joined: Oct 22, 2006
Posts: 4
Hai try this method definition for getColumnClass.
public Class getColumnClass(int column)
{
Vector v = (Vector)dataVector.elementAt(0);
return v.elementAt(column).getClass();
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable number renderer