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
849 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

760 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

23 Experts available now in Live!

Get 1:1 Help Now