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
856 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java  and programming certification ? 4 86
eclipse compiler vs Installed JREs option 3 77
servlet  URL Rewriting 1 28
nextBoolean(double p) for Random class 3 35
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now