removing JTable

I posted this problem earlier but didn't get a solution that worked. Also I was told to post this in the swing forum so here is my problem.

I have a mortgage caculator that calculates a monthly mortgage payment and displays the mortgage's amortization table(when/if the user presses the table button). I also have a clear button that clears all the fields and removes the JPanel that my JTable is on.

My problem is this. When I click the Clear button, The JPanel is removed with the following code(it does what i want it to do):

getContentPane().remove(amortTablePanel);

getContentPane().repaint();

My problem is that when i calculateanother mortgage and click the Amortization Table button, both the firstand the second tables show up.

How can I completely remove the table when i click the Clear button?

Thanks in advance.

Message was edited by:

oc084

[934 byte] By [oc084a] at [2007-11-27 2:12:02]
# 1
Well, I'd say your problem lies not with the removal code but with the code that adds the tables ...
thomas.behra at 2007-7-12 2:05:55 > top of Java-index,Desktop,Core GUI APIs...
# 2

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 2:05:55 > top of Java-index,Desktop,Core GUI APIs...
# 3

import javax.swing.*;

import javax.swing.text.*;

import java.awt.*;

import java.awt.event.*;

import java.math.*;

import java.text.NumberFormat;

public class Mortgage extends JFrame implements ActionListener

{

JPanel amountPanel = new JPanel();

JPanel ratePanel = new JPanel();

JPanel termPanel = new JPanel();

JPanel paymentPanel = new JPanel();

JPanel buttonPanel = new JPanel();

JPanel buttonAmortPanel = new JPanel();

JPanel amortTablePanel = new JPanel();

JLabel lAmount = new JLabel("Enter Amount:");

JLabel lRate = new JLabel("Enter Rate:");

JLabel lTerm = new JLabel("Enter Term:");

JLabel lPayment = new JLabel("Monthly Payment Is:");

JLabel lPaymentAmt = new JLabel("");

JLabel lEmpty = new JLabel("");

JTextField tAmount = new JTextField(10);

JTextField tRate = new JTextField(8);

JTextField tTerm = new JTextField(8);

JButton bCalc = new JButton("Calculate");

JButton bClear = new JButton("Clear");

JButton bExit = new JButton("Exit");

JButton bAmort = new JButton("Amortization Table");

JScrollPane scrollPane = new JScrollPane();

//New instance of NumberFormat

NumberFormat currency = NumberFormat.getCurrencyInstance();

//Used to calculate

double dAmount;

double dRate;

int iTerm;

double dPayment;

void buildGUI()

{

getContentPane().setLayout(new FlowLayout());

lPaymentAmt.setBorder(BorderFactory.createLineBorder(Color.black));

lPaymentAmt.setPreferredSize(new Dimension(200, 30));

lPaymentAmt.setFont(new Font("Arial", 20, 20));

lEmpty.setPreferredSize(new Dimension(60, 30));

bCalc.setPreferredSize(new Dimension(200, 30));

bAmort.setPreferredSize(new Dimension(290, 30));

amountPanel.add(lAmount);

amountPanel.add(tAmount);

ratePanel.add(lRate);

ratePanel.add(tRate);

termPanel.add(lTerm);

termPanel.add(tTerm);

paymentPanel.add(lPayment);

paymentPanel.add(lPaymentAmt);

paymentPanel.add(lEmpty);

bCalc.addActionListener(this);

bClear.addActionListener(this);

bExit.addActionListener(this);

bAmort.addActionListener(this);

buttonPanel.add(bClear);

buttonPanel.add(bCalc);

buttonPanel.add(bExit);

buttonAmortPanel.add(bAmort);

getContentPane().add(amountPanel);

getContentPane().add(ratePanel);

getContentPane().add(termPanel);

getContentPane().add(paymentPanel);

getContentPane().add(buttonPanel);

getContentPane().add(buttonAmortPanel);

setTitle("Mortgage Calculator");

setSize(600, 220);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

} //end buildGUI

//Method to calculate monthly payment

void getPayment(double dAmount, double dRate, int iTerm)

{

dPayment = (dAmount * (dRate / 1200)) / (1-Math.pow(1 / (1+dRate / 1200)

, (iTerm * 12)));

lPaymentAmt.setText(currency.format(dPayment));

}

void getAmortTable()

{

//recalculate payment before creating amortization table

if (lPaymentAmt.getText() != "")

{

//convert user-entered amounts

dAmount = Double.valueOf(tAmount.getText());

dRate = Double.valueOf(tRate.getText());

iTerm = Integer.parseInt(tTerm.getText());

//variable to calculate amortTable

int iCounter;

int iPayment = iTerm * 12;

double dMonthlyRate = dRate / 1200;

double dTotalInterest = 0;

double dInterestPaid = 0;

double dPrincipalPaid = 0;

double dBalance = 0;

setSize(600, 600);

//arrays for amortTable

String[] colNames =

{

"Month", "Payment", "Principal Paid", "Interest Paid", "Balance"

};

String[][] data = new String[iPayment][5];

//calculate amortization table and fill array with results

for (iCounter = 0; iCounter < iPayment; iCounter++)

{

//Calculate

dInterestPaid = dAmount * dMonthlyRate;

dPrincipalPaid = dPayment - dInterestPaid;

dBalance = dAmount - dPrincipalPaid;

dAmount = dBalance;

dTotalInterest += dInterestPaid;

for (int colCounter = 0; colCounter < 5; colCounter++)

{

int dataCounter = 0;

data[iCounter][dataCounter] = Integer.toString(iCounter + 1)

;

data[iCounter][dataCounter + 1] = ("" + currency.format

(dPayment));

data[iCounter][dataCounter + 2] = ("" + currency.format

(dInterestPaid));

data[iCounter][dataCounter + 3] = ("" + currency.format

(dPrincipalPaid));

data[iCounter][dataCounter + 4] = ("" + currency.format

(dBalance));

} //end for

} //end for

//create JTable and fill with arrays

JTable amortTable = new JTable(data, colNames);

JScrollPane scrollPane = new JScrollPane(amortTable);

//add JTable to scrollpane

scrollPane.setPreferredSize(new Dimension(500, 370));

//set size of scrollpane

amortTablePanel.add(scrollPane);

getContentPane().add(amortTablePanel);

amortTablePanel.repaint();

} //end if

else

{

try

{

getPayment(Double.valueOf(tAmount.getText()), Double.valueOf

(tRate.getText()), Integer.parseInt(tTerm.getText()));

getAmortTable();

}

catch (Exception error)

{

JOptionPane.showMessageDialog(null,

"Invalid Input. Please Try Again.", "Input Error",

JOptionPane.ERROR_MESSAGE);

}

} //end else

} //end getAmortTable

void setClearGUI()

{

getContentPane().remove(amortTablePanel);

getContentPane().repaint();

tAmount.setText("");

tRate.setText("");

tTerm.setText("");

lPaymentAmt.setText("");

tAmount.requestFocus();

//setSize(600, 220);

}

public void actionPerformed(ActionEvent onClick)

{

Object src = onClick.getSource();

if (src == bCalc)

{

try

{

getPayment(Double.valueOf(tAmount.getText()), Double.valueOf

(tRate.getText()), Integer.parseInt(tTerm.getText()));

}

catch (Exception error)

{

JOptionPane.showMessageDialog(null,

"Invalid Input. Please Try Again.", "Input Error",

JOptionPane.ERROR_MESSAGE);

}

}

if (src == bClear)

{

setClearGUI();

} //end if bClear

if (src == bAmort)

{

getAmortTable();

} //end if bAmort

if (src == bExit)

{

System.exit(1);

} //end if bExit

} //end actionPerformed

public static void main(String[] args)

{

new Mortgage().buildGUI();

}

} //end Mortgage

oc084a at 2007-7-12 2:05:55 > top of Java-index,Desktop,Core GUI APIs...
# 4

That is not a SSCCE, that is your entire application.

Your question is about removing a component and add a component to the content pane. So create a demo that does just that when you have a problem. It should be about 10 lines of code. I'm not going to read "your application" the next time to figure out what you are doing. Its up to you to isolate the problem before posting.

The problem with your application is that you keep adding scrollpanes to the amortization panel. The amoritization panel uses a FlowLayout so the just keep displaying side by side. There is no need to use a panel to simple hold the scroll pane. Add the scrollpane directly to the content pane. And add you table to the scrollpane when you create the GUI. Then when you change the amortization schedule you simple create a new DefaultTableModel and use the table.setModel() method. You don't need to create a new table every time. Then you should be able to use scrollpane.setVisible(...) to hide/showl the table as required.

camickra at 2007-7-12 2:05:55 > top of Java-index,Desktop,Core GUI APIs...
# 5

dAmount = Double.valueOf(tAmount.getText());

dRate = Double.valueOf(tRate.getText());

iTerm = Integer.parseInt(tTerm.getText());

And use the correct method. You are using "double" primitives, not Double objects, so use the "parseDouble" method. Don't rely on the compiler to do autoboxing for you.

camickra at 2007-7-12 2:05:55 > top of Java-index,Desktop,Core GUI APIs...
# 6

Right, amortTablePanel still has all these new tables you keep creating (BTW you'll run out of memory pretty quick creating new arrays and tables all the time), so each time it gets added back onto the contentPane it has the old tables. As said above, create one table on one scroll pane ahead of time and manipulate the TableModel.

For more info on using tables, please be sure to read the tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

pthorsona at 2007-7-12 2:05:55 > top of Java-index,Desktop,Core GUI APIs...
# 7

Really sorry about the long program. My problem was taht I was validating after i removed the scrollpane. after i added teh following methods

getContentPane().remove(scrollPane);

getContentPane().validate();

getContentPane().repaint();

It fixed the problem. Thanks everyone for all your help.

oc084a at 2007-7-12 2:05:55 > top of Java-index,Desktop,Core GUI APIs...