Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

I have a JTable that has a column of class Double.  How do I format it so there is always at least 1 number after the decimal place?

Posted on 2009-07-02
8
872 Views
Last Modified: 2013-12-29
I have a JTable that has a column of class Double.  How do I format it so there is always at least 1 number after the decimal place for the numbers in the column of Doubles.  I have a Table Model that extends AbstractTableModel for the JTable.

For example if I have
200
200.3
300.33

in a column in my JTable they will display as

200.0
200.3
300.33
to the user?

Thanks
0
Comment
Question by:gdkinney_2
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24767856
You would need to use a custom TableCellRenderer
0
 
LVL 92

Expert Comment

by:objects
ID: 24768994
0
 

Author Comment

by:gdkinney_2
ID: 24874116
Objects

I used the code that your link provides and it seems to work.  Before I used the CellRenderer my numbers in my cells were right justified but now they are left justified can I make them right justified again?

Thanks
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 150 total points
ID: 24874450
Set the renderer component text alignment to Component.RIGHT_ALIGNMENT
http://java.sun.com/javase/6/docs/api/javax/swing/JLabel.html#setHorizontalAlignment(int)

0
 
LVL 92

Accepted Solution

by:
objects earned 350 total points
ID: 24875180
set the alignment of the renderer

DecimalFormatRenderer renderer = new DecimalFormatRenderer();
renderer.setHorizontalAlignment(Component.RIGHT_ALIGNMENT);

table.getColumnModel().getColumn(1).setCellRenderer(renderer);

Or you could do it (new renderer constructor)
0
 

Author Comment

by:gdkinney_2
ID: 24878766
Both experts gave valuable advice that helped solve my problem.  Thank You!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24878969
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 24883511
(-:
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
javap bin 2 40
tomcat not starting 6 68
servlet web applications   metadata-complete="true" or false 3 109
SequenceInputStream example 3 19
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question