Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JTable display problem 1

Status
Not open for further replies.

confusedtekguy

IS-IT--Management
Jun 6, 2003
47
US
Hi all I'm having a problem displaying an array of double into a JTable. The double array ammortize[][] contains 360 rows of 4 columns of mortgage calculations, and I'm looking to display it in JTable, by using a decimal formatter to format the data and save the formated strings in an Object array of Strings so I can display it in a JTable.

Code:
       int	payments_per_year,
		interest_payed = 0,
		principle_applied = 1,
		cum_principle = 2,
		new_balance = 3;

        NumberFormat numberFormatter;
	String temp;
	numberFormatter = new
        DecimalFormat("'$'#,###,###.##");

	String[] columnNames = {
	   "Payment Number",
	   "Interest Paid",
	   "Principle Paid",
	   "Total Principle Paid",
	   "New Balance",
           };

		          
	Object [][] data = new String[360][5];
	      for(int i=0;i<=10;i++){
                temp = Integer.toString(i);
		data[i][0]= temp;
		temp = numberFormatter.format(amortization[i][interest_payed]);
		data[i][1] = temp;
		temp = numberFormatter.format(amortization[i][principle_applied]);
		data[i][2] = temp;
		temp = numberFormatter.format(amortization[i][cum_principle]);
		data[i][3] = temp;
		temp = numberFormatter.format(amortization[i][new_balance]);
		data[i][4] = temp;
		}

       	model = new DefaultTableModel(data, columnNames);
	table = new JTable(model);
	table.setPreferredScrollableViewportSize(new Dimension(1100, 700));
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);

My biggest problem is that function where I set the Object array to the formated string does [&#91;]b]not[&#91;]/b] work inside the for loop. It does however work if I take out the for loop and only initilalize the first row and then the table prints first row of numbers. I thought about using vectors, but I'm not sure how to declare and use a 2D vector. Any help would be very much appretiated, I'm new to java and I'm sure there is an easy way of doing this, I just can't figure out what it is.
 
a) what's amortization? I guess a 2-dim array of doubles/floats.
b) In the colnames, after the last element is a comma too much.
c) I made a test-app which worked - with small modifications (size)
d) consider replacing the ints (... new_balance = 3;) with:
Code:
public final static int NEW_BALANCE = 3;
e) for precise calculation of money, consider using BigDecimal.

Code:
import java.awt.*;
import javax.swing.*;
import java.text.*;
import javax.swing.table.*;
import java.util.Random;

public class JTableTest  extends JFrame
{
	public JProgressBar progressBar;
	private DefaultTableModel model;
	private JTable table;
	private static final int ROWS = 364;
	private static final int COLS = 5;
		
	public JTableTest ()
	{
		setTitle ("JTableTest");
		
		setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		
		addTable ();
		setSize (640, 480);

		setVisible (true);
		System.out.println ("solved...");
	}

	public static void main(String[] args) 
	{
		new JTableTest ();
	}
    
	public void fill (float [][] f)
	{
		Random r = new Random ();
		
		for (int i=0; i<ROWS; ++i)
		{
			for (int j=0; j<COLS; ++j)
			{
				f[i][j] = r.nextFloat ();
			} 
		}
	}
	
	public void addTable ()
	{
		int payments_per_year,
		interest_payed = 0,
		principle_applied = 1,
		cum_principle = 2,
		new_balance = 3;
	
		NumberFormat numberFormatter;
		String temp;
		numberFormatter = new DecimalFormat("'$'#,###,###.##");
		
		String[] columnNames = {
			"Payment Number",
			"Interest Paid",
			"Principle Paid",
			"Total Principle Paid",
			"New Balance"
		};
	
		float amortization [][] = new float [ROWS][COLS];
		fill (amortization);
		
		Object [][] data = new String[ROWS][COLS];
		
		for (int i=0; i < ROWS; ++i)
		{
			temp = Integer.toString(i);
			data[i][0]= temp;
			temp = numberFormatter.format (amortization[i][interest_payed]);
			data[i][1] = temp;
			temp = numberFormatter.format (amortization[i][principle_applied]);
			data[i][2] = temp;
			temp = numberFormatter.format (amortization[i][cum_principle]);
			data[i][3] = temp;
			temp = numberFormatter.format (amortization[i][new_balance]);
			data[i][4] = temp;
		}

		model = new DefaultTableModel (data, columnNames);
		table = new JTable(model);
		table.setPreferredScrollableViewportSize (new Dimension (600, 430));
		JScrollPane scrollPane = new JScrollPane (table);
		add(scrollPane);		
	}
}

dont't visit my homepage:
 
Thanks stefanwagner! The problem was in the way I was declaring and filling the data array. Once I set ROWS and COLS to static final, it worked. I don't really need to use decimal for extra precision, since this is for refrence only.

But the problem that I'm having now is that the number of ROWS will change as the term of the loan changes, and as soon as I dynamically change the rows it stops working again. I'm thinking of doing away with this idea and using a scroll pane and the paint method to draw the table and display the array.
 
For dynamically fixing the row count you have to get rid of the 'static' modifier:
Code:
public class JTableTest extends JFrame
{
	//...
	private final int ROWS;
	private static final int COLS = 5;
		
	public JTableTest (int rows)
	{
		setTitle ("JTableTest");
		ROWS = rows;
note that in difference to c/c++, you may declare a final variable, and initialice it later (but only once of course).

You now may call this programm with a dynamic number of rows. If you're asked for dynamic changing, a next step must be involved.
Code:
public static void main(String[] args) 
{
	try 
	{
		int rows = Integer.parseInt (args [0]);
		new JTableTest (rows);
	} 
	catch (NumberFormatException nfe) 
	{
		System.exit (1);
	}
}
and for reducing redundancy, you could do this:
Code:
for(int i=0; i < ROWS; ++i)
{
	temp = Integer.toString(i);
	data[i][0]= temp;
	for (int j = interest_payed; j < COLS; ++j)
	{
		temp = numberFormatter.format(amortization[i][j]);
		data[i][j] = temp;
	}
}

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top