?
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
Medium Priority
?
918 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 600 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 1400 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 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