This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Display of double in GUI different from non-GUI. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Display of double in GUI different from non-GUI." Watch "Display of double in GUI different from non-GUI." New topic

Display of double in GUI different from non-GUI.

Debra Simeroth
Ranch Hand

Joined: Jun 11, 2009
Posts: 33
Hi there!

I have a little project for school that is due today. It meets all of the "requirements" but I am not completely happy with the output. (This is an online course and my Java resources are limited to my textbook, the internet and this forum.) In my non-GUI program the doubles appeared at 2 digits, now when I do the math, my dollars and cents come out with any variety of decimal places. I have done some reading on Big Decimal, but am confused as to how to apply it here. I am used to the primitive data types and can't seem to find clarity on how to use it. One of my classmates suggested using int for everything and dividing by 100... well it works kind of... and leaves me with no decimal places at all! Below is my code, since I have read too many posts that say they can't help without it.... Thanks in advance!

class InventoryTest

public void buildGUI()
Item itemObjects[] = new Item[ 6 ];
itemObjects[ 0 ] = new Item( "Air Care Filter", 01624, 5, 12.50, 1 );
itemObjects[ 1 ] = new Item( "Clean Effects 24\"", 10024, 12, 734.56, 1 );
itemObjects[ 2 ] = new Item( "Clean Effects 21\"", 10021, 24, 726.15, 1 );
itemObjects[ 3 ] = new Item( "Clean Effects 17.5\"", 10017, 3, 697.89, 1 );
itemObjects[ 4 ] = new Item( "Thermostat TCONT", 802, 18, 98.57, 1 );
itemObjects[ 5 ] = new Item( "Paper Filter Package", 11224, 6, 2.35, 6 );
Vector colNames = new Vector(Arrays.asList(new String[]{"Item Name","Product Number","Item Count","Cost Each","Bundled Quantity","Restocking Fee","Inventory Value"}));
Vector rowData = new Vector();
for(int x = 0, y = itemObjects.length; x < y; x++)
Vector row = new Vector();

DefaultTableModel dtm = new DefaultTableModel(rowData,colNames);
final JTable table = new JTable(dtm);
JScrollPane sp = new JScrollPane(table);
sp.setPreferredSize(new Dimension(800,600));

JButton btn = new JButton("Calculate Total Value");
final JLabel lbl = new JLabel("TotalValue = $");
JPanel p = new JPanel(new GridLayout(1,2));
JFrame f = new JFrame();
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
double total = 0.0;
for(int x = 0, y = table.getRowCount(); x < y; x++)
total += new Double(""+table.getValueAt(x,6)).doubleValue();
lbl.setText("TotalValue = $" + new java.text.DecimalFormat("0.00").format(total));

Again, thanks for any advice you can offer... Its not critical, but I would like to submit a more professional looking product.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
add this class

change the commented out JTable line to this block

also, this will break if you drag non 'number' columns to column 5 or 6, so needs more code (perhaps not critical for an assignment)
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2414
Table Format Renderers shows you how to add a renderer to a TableColumn so you don't need to override the table. It also shows how to create a currency renderer.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20193

And there's another way, using setDefaultRenderer:
This also uses super classes - if it can't find a renderer for Double.class, it will try Number.class, and even Object.class if necessary.

This approach can be used if all columns with the same class should be rendered the same.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Display of double in GUI different from non-GUI.
jQuery in Action, 3rd edition