• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

JTable number renderer

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}

}
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai try this method definition for getColumnClass.
public Class getColumnClass(int column)
{
Vector v = (Vector)dataVector.elementAt(0);
return v.elementAt(column).getClass();
}
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic