-1

This is my Main class

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
            Program p = new Program();
        }

}

Here is my Program class

public class Program {

    public Program(){
        //System.out.println("Let's begin...");

        TextEditor textEditor = new TextEditor();
        textEditor.setVisible(true);
        textEditor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }
}

Here is my TextEditor class. In this class is where I have a JFrame that has a text field in which I would like the results of the fahrenheit to celcus conversion found in my Calculator class to be placed.

public class TextEditor extends JFrame implements ActionListener{
JTextArea textArea;

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);
}

    @Override
    public void actionPerformed(ActionEvent e) {
}   loadCalculator()
private void loadCalculator() {
    // TODO Auto-generated method stub
    Calculator c = new Calculator();
    Calculator calculator = new Calculator();
    calculator.setVisible(true);
    calculator.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

    textArea.setText(String.valueOf(c.fahrenheit));

}

Here is my Calculator class which extends JFrame and implements ActionListener. In here is where a JFrame comes up with a JButton and a JTextField next to it. When the user enters a number into the text field, it does a calculation and converts Fahrenheit to Celsius. Once that is done, I would like the result to show up on the JTextArea from the TextEditor Class.

public class Calculator extends JFrame implements ActionListener{
JButton fToCButton;
JTextField fToC;
double fahrenheit;
TextEditor a = new TextEditor();

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();
            }
        }

        private void degreeConversion() {
            // TODO Auto-generated method stub
            double conversion = Double.parseDouble(fToC.getText());
            fahrenheit = (((conversion -32) * 5) / 9);
            a.textArea.setText(String.valueOf(fahrenheit));
            System.out.println(fahrenheit);
        }


 }
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Nov 23 '14 at 05:40
  • Why dont u try to pass the `string` as a parameter. `String s = field.getText(); pass(s);` Where pass is method of another class. – Vighanesh Gursale Nov 23 '14 at 05:41
  • Thank You @AndrewThompson, I did reduce my original code to just the areas that I am having issues with, which is what I posted. This is probably about 1/4 of my original code for my full program. – Carlos Campos Nov 23 '14 at 05:42
  • I am still very new to programming, but i thought passing strings as paramaters was more for c# or c++. – Carlos Campos Nov 23 '14 at 06:14
  • An MCVE needs to be a single source file. To do that (and allow it to compile) 1) Change `public class TextEditor` to `class TextEditor` 2) Paste it into the end of the other source 3) Add the imports needed to compile them & 4) Provide a `main(String[])` to run it. – Andrew Thompson Nov 23 '14 at 06:23
  • I know it seems like many classes but our professor asked us to do our project this way. Seems like it's just a personal preference. – Carlos Campos Nov 23 '14 at 21:58

1 Answers1

0

As Andrew Thompson already indicated, make sure your code at least compiles and has a main method. The TextEditor class you provided is missing the loadMenuBar & loadToolBar methods, the actionPerformed & loadCalculator methods are sort of merged together, and a main method is missing in both classes. Please formulate your question as best as you can to get good answers.

I think the reason that the result of the conversion does not show up is that the TextEditor and Calculator objects are not connected to each other. In the Calculator.degreeConversion method you try to pass the result to a TextEditor object, but this object is created by the calculator itself (see the field declaration) and this text editor is not visible. This could be solved by passing a reference of the text editor to the calculator constructor.

In code:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;


/**
 * TextEditor class.
 */
public class TextEditorVersion2 extends JFrame implements ActionListener {
    JTextArea textArea;

    public static void main(String[] args) {
        final TextEditorVersion2 textEditor = new TextEditorVersion2();
        textEditor.setVisible(true);
        final CalculatorVersion2 calculatorVersion2 = new CalculatorVersion2(textEditor);
        calculatorVersion2.setVisible(true);
    }

    public TextEditorVersion2(){
        super("TextMe");
        this.setLayout(new BorderLayout());
        // Freek: these methods are missing.
        //loadMenuBar();
        //loadToolBar();
        // Freek: these methods are missing.
        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);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    }

    private void loadCalculator() {
        // TODO Auto-generated method stub
        CalculatorVersion2 c = new CalculatorVersion2(this);
        CalculatorVersion2 calculatorVersion2 = new CalculatorVersion2(this);
        calculatorVersion2.setVisible(true);
        calculatorVersion2.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        textArea.setText(String.valueOf(c.fahrenheit));
    }
}


/**
 * Calculator class.
 */
class CalculatorVersion2 extends JFrame implements ActionListener {
    JButton fToCButton;
    JTextField fToC;
    double fahrenheit;
    TextEditorVersion2 a;

    public CalculatorVersion2(final TextEditorVersion2 textEditor) {
        super("Unit Converter");
        this.a = textEditor;
        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();
        }
    }

    private void degreeConversion() {
        // TODO Auto-generated method stub
        double conversion = Double.parseDouble(fToC.getText());
        fahrenheit = (((conversion -32) * 5) / 9);
        a.textArea.setText(String.valueOf(fahrenheit));
        System.out.println(fahrenheit);
    }
}
Community
  • 1
  • 1
Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28