participate


Swing - Jtable Double cloumn rendering problem
<<   Back to Forum  |   Give us Feedback
This topic has 1 reply on 1 page.
jagdishrao
Posts:39
Registered: 10/25/04
Jtable Double cloumn rendering problem   
Nov 5, 2004 5:27 AM

 
hi friends

as i am new to swings and Jtable pls help me if u can.


in the code copied below when i start putting value into the Double column
a ( .0 ) is appended at the end which is undesirable it should be like that only when the value is
something like 22.99 and also i want to limit the double value's fractional part to be limited to 2 digits only.
if this can be done in my Table model itself so that it will work everywhere i use this model

it will be really great !!!!!

if anyone can send a sample code for achieving this it would be of great help


import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.text.*;
 
public class TableProcessing extends JFrame implements TableModelListener
{
    JTable table;
 
    public TableProcessing()
    {
        String[] columnNames = {"Item", "Quantity", "Price", "Cost"};
        Object[][] data =
        {
            {"Bread", new Integer(1), new Double(1.11), new Double(1.11)},
            {"Milk", new Integer(1), new Double(2.22), new Double(2.22)},
            {"Tea", new Integer(1), new Double(3.33), new Double(3.33)},
            {"Cofee", new Integer(1), new Double(4.44), new Double(4.44)}
        };
 
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        model.addTableModelListener( this );
//
        table = new JTable( model )
        {
            //  Returning the Class of each column will allow different
            //  renderers to be used based on Class
            public Class getColumnClass(int column)
            {
                return getValueAt(0, column).getClass();
            }
 
            //  The Cost is not editable
            public boolean isCellEditable(int row, int column)
            {
                if (column == 3)
                    return false;
                else
                    return true;
            }
        };
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
//
        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
    }
 
    /*
     *  The cost is recalculated whenever the quantity or price is changed
     */
    public void tableChanged(TableModelEvent e)
    {
        if (e.getType() == TableModelEvent.UPDATE)
        {
            int row = e.getFirstRow();
            int column = e.getColumn();
 
            if (column == 1 || column == 2)
            {
                int    quantity = ((Integer)table.getValueAt(row, 1)).intValue();
                double price = ((Double)table.getValueAt(row, 2)).doubleValue();
                Double value = new Double(quantity * price);
                table.setValueAt(value, row, 3);
            }
        }
    }
 
    public static void main(String[] args)
    {
        TableProcessing frame = new TableProcessing();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

thank in advance
jags
 
thomas.behr
Posts:2,353
Registered: 9/6/00
Re: Jtable Double cloumn rendering problem   
Nov 5, 2004 5:40 AM (reply 1 of 1)  (In reply to original post )

 
Use a custom renderer that uses an apropriate instance of [url=http://java.sun.com/j2se/1.4.2/docs/api/java/text/NumberFormat.html]NumberFormat[/url] to produce the desired string representation of your doubles.
[url=http://java.sun.com/docs/books/tutorial/uiswing/components/table.html]Read this for more information on custom renderers.[/url]
 
This topic has 1 reply on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 29
  • Guests : 132

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums