Here is what is going on with my program,
- Run the program, first Jframe pops up (Text Editor).
- I click the calculator button and a Jframe pops up (Unit Converter) with 1 button and 1 text field.
- I type in a number into the first text field ( Fahrenheit to Celsius), click the Fahrenheit to Celsius button but the conversion does not append to the text area in Text Editor.
- I close the "Unit Converter" window, click the calculator button again and the text I entered the first time in the "Fahrenheit to Celsius" field is still there. This second time I click the calculator button, the conversion happens and it gets appended to the text area.
I want the data to be appended to the "Text Editor" textArea once I press the "Fahrenheit to Celsius" button.
I have four different classes (this is the way my professor wanted our project set up) :/ Here is my "Main" Class
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Program p = new Program();
}
}
Next, here is my "Program" class
import javax.swing.JFrame;
public class Program {
public Program(){
TextEditor textEditor = new TextEditor();
textEditor.setVisible(true);
textEditor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Calculator calculator = new Calculator();
calculator.setVisible(false);
calculator.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
}
}
And here is my "TextEditor class" I omitted some of the buttons as those are not what causing any issues.
public class TextEditor extends JFrame implements ActionListener{
JTextArea textArea;
JButton calculatorButton;
Calculator c = new Calculator();
public TextEditor(){
super("TextMe");
this.setLayout(new BorderLayout());
loadMenuBar();
loadToolBar();
loadTextArea();
this.pack();
}
private void loadTextArea() {
// TODO Auto-generated method stub
textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(800,600));
this.add(BorderLayout.CENTER, textArea);
}
private void loadToolBar() {
// TODO Auto-generated method stub
JToolBar toolBar;
// CALCULATOR BUTTON
calculatorButton = new JButton(new ImageIcon(this.getClass().getResource("/images/calculator.png")));
calculatorButton.addActionListener(this);
// TOOL BAR
toolBar = new JToolBar(JToolBar.HORIZONTAL);
toolBar.add(calculatorButton);
this.add(BorderLayout.PAGE_START, toolBar);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == calculatorButton){
loadCalculator ();
}
}
private void loadCalculator() {
// TODO Auto-generated method stub
{
c.setVisible(true);
c.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
this.setLayout(new FlowLayout());
}
if(!c.fToC.getText().isEmpty()){
textArea.setText(String.valueOf(c.fahrenheit));
}
}
}
Lastly, here is my "Calculator" class. Again, sorry that there is so much code here.
public class Calculator extends JFrame implements ActionListener{
JButton fToCButton;
JTextField fToC;
double fahrenheit;
public Calculator(){
super("Unit Converter");
this.setLayout(new FlowLayout());
fToC = new JTextField(5);
fToCButton = new JButton("Ferenheit To Celcius");
fToCButton.addActionListener(this);
add(fToCButton, BorderLayout.WEST);
add(fToC);
this.pack();
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == fToCButton){
degreeConversion();
}
}
public void degreeConversion() {
// TODO Auto-generated method stub
double conversion = Double.parseDouble(fToC.getText());
fahrenheit = (((conversion -32) * 5) / 9);
}
}