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
897 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
[X]
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
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

724 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