• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Display of double in GUI different from non-GUI.

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
row.add(itemObjects[x].getitemName());
row.add(itemObjects[x].getproductNumber());
row.add(itemObjects[x].getitemCount());
row.add(itemObjects[x].getcostEach());
row.add(itemObjects[x].getquantityInBundle());
row.add(itemObjects[x].getrestockingFee());
row.add(itemObjects[x].getitemTotalValue());




rowData.add(row);
}
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));
p.add(btn);
p.add(lbl);
JFrame f = new JFrame();
f.getContentPane().add(sp);
f.getContentPane().add(p,BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
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));
}
});
}
}
[/code]


Again, thanks for any advice you can offer... Its not critical, but I would like to submit a more professional looking product.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
This is my favorite tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic