R2D2 R2D2 - 2 months ago 8
Java Question

Updating JTextArea each time new calculation is preformed.

I am building my first graphical user interface and it is a fairly simple calculator application. The problem I am running into is when I try to preform another calculation it keeps adding a new JTextArea for the output. I tried

remove()
,
revalidate()
,
repaint()
, but it still continues to add new JTextArea boxes. What I am doing wrong?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static java.lang.Thread.currentThread;
import java.util.concurrent.TimeUnit;

public class MovieCalculatorJFrame extends JFrame
{
private static final double PERCENT_TOTAL = 0.20;
private double grossChildTicketSold;
private double grossAdultTicketSold;
private double netChildTicketSold;
private double netAdultTicketSold;
private double grossRevenue;
private double netRevenue;
private MovieTextBoxPanel textBoxPanel;
private JPanel outPutPanel;
private JTextArea outPut;
private JButton calcButton;
private JPanel calcButtonPanel;
private final int WINDOW_WIDTH = 800;
private final int WINDOW_HEIGHT = 1500;

//Create constructor
public MovieCalculatorJFrame()
{
//Display title
setTitle("Profits Calculator");

//Set window size
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

//Create GridLayout
// 3 row with 1 columns
setLayout(new GridLayout(3,1));

//Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create custom Panels
textBoxPanel = new MovieTextBoxPanel();

//Add the components to the content pane
add(textBoxPanel);

//Build calculate button panel
buildCalculateButton();


//Allows time for splash to appear.
try
{
TimeUnit.MILLISECONDS.sleep(2000);
}
catch(Exception e)
{
System.out.println("Exception caught");
}

// Pack the contents of the window and display it
pack();
setVisible(true);
}

private void buildCalculateButton()
{
//Create a JPanel for the button
calcButtonPanel = new JPanel();

//Create Button and add it to the JPanel
calcButton = new JButton("Calculate");
calcButtonPanel.add(calcButton);

//add JPanel to content pane
add(calcButtonPanel);

//Register action listener
calcButton.addActionListener(new CalcButtonListener());
}

//inner class with action listener.
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//Check fields for errors
try{
grossChildTicketSold = (textBoxPanel.getChildCost() * textBoxPanel.getChildTotal());
grossAdultTicketSold = (textBoxPanel.getAdultCost() * textBoxPanel.getAdultTotal());
netChildTicketSold = (PERCENT_TOTAL * grossChildTicketSold);
netAdultTicketSold = (PERCENT_TOTAL * grossAdultTicketSold);
grossRevenue = (grossAdultTicketSold + grossChildTicketSold);
netRevenue = (PERCENT_TOTAL * grossRevenue);

}
catch(Exception a)
{
System.out.println("Fields are not completely filled out or letters may have been enter");
}


//Create new JPanel and JText and outputs result to JText.
JPanel newPanel = new JPanel();
if(outPut !=null)
{
newPanel.remove(outPut);
newPanel.revalidate();
newPanel.repaint();
}

outPut = new JTextArea("Gross revenue for adult tickets sold: " + grossAdultTicketSold + "\n");
outPut.append("Net revenue for adult tickets sold: " + netAdultTicketSold + "\n");
outPut.append("Gross revenue for child tickets sold: " + grossChildTicketSold + "\n");
outPut.append("Net revenue for child tickets sold: " + netChildTicketSold + "\n");
outPut.append("Gross revenue: " + grossRevenue + "\n");
outPut.append("Net revenue: " + netRevenue + "\n");

//Build output JPanel and add it to the content pane

newPanel.add(outPut);
add(newPanel);
setVisible(true);


}
}
public static void main(String[] args)
{
new MovieCalculatorJFrame();
}
}


This is the custom JPanel I used in case you want everything.

import javax.swing.*;
import java.awt.*;
public class MovieTextBoxPanel extends JPanel
{
private JLabel childTicketCostText;
private JTextField childTicketCostTextBox;
private JLabel AdultTicketCostText;
private JTextField AdultTicketCostTextBox;
private JLabel childTicketSoldText;
private JTextField childTicketSoldTextBox;
private JLabel AdultTicketSoldText;
private JTextField AdultTicketSoldTextBox;

private double childTicketCost;
private double childTicketTotal;
private double adultTicketCost;
private double adultTicketTotal;
private String input;

//Create constructor
public MovieTextBoxPanel()
{
//Create GridLayout
// 2 row with 2 columns
setLayout(new GridLayout(2,2));

//Create child price text message
childTicketCostText = new JLabel("Price per child's ticket");
//Create child price text box
childTicketCostTextBox = new JTextField(5);
//Create adult price text message
AdultTicketCostText = new JLabel("Price per adult's ticket");
//Create adult price text box
AdultTicketCostTextBox = new JTextField(5);
//Create child tickets sold text message
childTicketSoldText = new JLabel("Number of children's tickets sold");
//Create child tickets sold text box
childTicketSoldTextBox = new JTextField(5);
//Create adult tickets sold text message
AdultTicketSoldText = new JLabel("Number of adult's tickets sold");
//Create adult tickets sold text box
AdultTicketSoldTextBox = new JTextField(5);

//Create border
setBorder(BorderFactory.createLineBorder(Color.black));

//Add text boxes to MovieTheaterProfits Jpanel
add(childTicketCostText);
add(childTicketCostTextBox);
add(AdultTicketCostText);
add(AdultTicketCostTextBox);
add(childTicketSoldText);
add(childTicketSoldTextBox);
add(AdultTicketSoldText);
add(AdultTicketSoldTextBox);


}

public double getChildCost()
{
input = childTicketCostTextBox.getText();
childTicketCost = Double.parseDouble(input);
return childTicketCost;
}
public double getAdultCost()
{
input = AdultTicketCostTextBox.getText();
adultTicketCost = Double.parseDouble(input);
return adultTicketCost;
}
public double getChildTotal()
{
input = childTicketSoldTextBox.getText();
childTicketTotal = Double.parseDouble(input);
return childTicketTotal;
}
public double getAdultTotal()
{
input = AdultTicketSoldTextBox.getText();
adultTicketTotal = Double.parseDouble(input);
return adultTicketTotal;
}

}

Answer

the error is that whenever you click on the button create a new instance of JPanel(newPanel)

to fix the problem try to declare your JPanel class level is advisable

 private JPanel newPanel; // create variable of class level
 private JTextArea outPut;
 //Create constructor 
  public MovieCalculatorJFrame()   //constructor 
  {
    setTitle("Profits Calculator");
    newPanel = new JPanel(); //instancia
    outPut = new JTextArea();
    ....
   // end to constructor
   newPanel.add(outPut);
   add(newPanel);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setVisible(true); 
   pack();
  }

it is not necessary to remove the TextArea your Panel would suffice to clean your content and then do the jTextArea.append("New Text");

your CalcButtonListener could look like

 private class CalcButtonListener implements ActionListener 
 {
    public void actionPerformed(ActionEvent e)
    {
           try{
                  grossChildTicketSold = (textBoxPanel.getChildCost() *   textBoxPanel.getChildTotal()); 
                  grossAdultTicketSold = (textBoxPanel.getAdultCost() *   textBoxPanel.getAdultTotal());
                  netChildTicketSold = (PERCENT_TOTAL * grossChildTicketSold);
                  netAdultTicketSold = (PERCENT_TOTAL * grossAdultTicketSold);
                  grossRevenue = (grossAdultTicketSold + grossChildTicketSold);
                  netRevenue = (PERCENT_TOTAL * grossRevenue);
              }
              catch(Exception a)
              {
              System.out.println("Fields are not completely filled out or letters may have   been enter");
          }
           outPut.setText(""); //clear text to JTextArea
           outPut.append("Gross revenue for adult tickets sold: " +      grossAdultTicketSold + "\n");
           outPut.append("Net revenue for adult tickets sold: " + netAdultTicketSold + "\n");
           outPut.append("Gross revenue for child tickets sold: " + grossChildTicketSold   + "\n");
           outPut.append("Net revenue for child tickets sold: " + netChildTicketSold +    "\n");
           outPut.append("Gross revenue: " + grossRevenue + "\n");
           outPut.append("Net revenue: " + netRevenue + "\n");
    }  
 }