I have a problem with a project i'm working on, and I was wondering if it was possible to do what i'm thinking. Basically I have a simple mathematical GUI. I want it to take 2 inputs multiply them and output the answer. I am doing this all through the GUI. I have the GUIs set up, but everything I've tried has not been able to transfer the inputs from one GUI to the other (only want the values). Another thing i'm trying to do is have 2 GUI setups, one with 2 inputs and 2 buttons (I have that one) and another with 3 JTextFields (2 of which are invisible that hold the input values of the first GUI). I am doing this in eclipse. Posting Code below. Also I am new to Java programming, so if my code looks awful, that's why.

Basically I want 2 inputs from one GUI to transfer to another GUI that multiplies them together. Also if there is an easier way that I didn't realize that makes it all in one GUI and is super simple, that would also be helpful.

I also do not know how to make my code robust enough to only allow the user to input numbers. Again, new to Java. Thank you for your time for those who sit down to read all of this!

This is my Main code

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;

import java.awt.event.ActionListener;
import java.io.*;

class Proejct extends button{

private static final long serialVersionUID = 1L;
public static void main(String[] args) throws IOException {
    SecondClass brandon = new SecondClass();
    brandon.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    brandon.setSize(500, 500);
    brandon.setVisible(true);
    JButton button = new JButton("Close", new CloseAction());
    brandon.add(button);
    button.setBounds(85, 48, 241, 21);
    button.setToolTipText("Click me!");
    button.addActionListener(new CloseListener());
    JButton button1 = new JButton("Calculate");
    brandon.add(button1);
    button1.setToolTipText("This will calculate your variables!");
    button1.addActionListener(new Action());
}
}

(imports)

(my close class)

class Action extends JFrame implements ActionListener {

public void actionPerformed(ActionEvent e) {
SecondClass second = new SecondClass();
second.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
second.setSize(500, 500);
second.setVisible(true);
JButton button = new JButton("Close", new CloseAction());
second.add(button);
button.setBounds(85, 48, 241, 21);
button.setToolTipText("Click me!");
button.addActionListener(new CloseListener());
}
}

(This is the part of my code that is causing me the most problems, I don't know how to trasnfer the inputs from the first GUI to the second. I have tried a lot of things, but nothing has worked)

NEW CLASS

import java.awt.FlowLayout;

import javax.swing.*;
import javax.swing.text.JTextComponent;

public class SecondClass extends JFrame {

public interface num1 {
    JTextField num1 = new JTextField(10);
    static String getText() {
        // TODO Auto-generated method stub
        return null;
    }
}
public interface num2 {
    JTextField num2 = new JTextField(10);
}
public JTextField closed;
public JTextField NS1;
public JTextField NS2;
public static dubleClass dubleClass;
public static Object num2;
public static Object num1;
public SecondClass() {  
super("Newton's First Law");
setLayout(new FlowLayout());
JTextField num1 = new JTextField(10);
num1.setToolTipText("Type your Mass here!");    
add(num1);
JTextField num2 = new JTextField(10);
num2.setToolTipText("Type your Acceleration here!");
add(num2);
/* public static final getnum1 {

    }*/
}
    public class dubleClass extends SecondClass {
    public dubleClass() {   
        closed = new JTextField(("This is your answer!"));
        closed.setEditable(false);
        closed.setVisible(true);
        add(closed);
// NS1.setText(((JTextComponent) num1).getText());
// dubleClass.NS1.setText(SecondClass.dubleClass.num1);
NS1.setVisible(false);
add(NS1);
NS2.setText(((JTextComponent) num2).getText());
// dubleClass.NS2.setText(this.num2);
NS2.setVisible(false);
add(NS2);
// double dub1 = Double.parseDouble(num1.getText());
// double dub2 = Double.parseDouble(num2.getText());
System.out.println(NS2);
System.out.println(NS1);
System.out.println(num2);
System.out.println(num1);
System.out.println(closed);

    }
    private void dubleClass(FlowLayout flowLayout) {
        // TODO Auto-generated method stub
    }
    private dubleClass(FlowLayout flowLayout) {
    }
    }
}

Another failed attempt, but the code is still used.

NEW CLASS

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JLabel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class dubbleClass extends JFrame{
private static final long serialVersionUID = 1L;

public JTextField item1;   //2 text fields
public JTextField item2;
// make text fields so user can input values for mass and acceleration
public dubbleClass() {
super("Newton's First Law");
setLayout(new FlowLayout());
// layout of gui, as in title
//add(button)
JTextField num1 = new JTextField();
num1.setToolTipText("Type your Mass here!");    
add(num1);
JTextField num2 = new JTextField();
num2.setToolTipText("Type your Acceleration here!");
add(num2);
// double dub1 = Double.parseDouble(num1.getText());
// double dub2 = Double.parseDouble(num2.getText());
// double dub3 = dub2 * dub1;
// String item3 = Double.toString(dub3);

// example texts for where to input what
// change my JTextField to a double
@SuppressWarnings("unused")
class button1 extends JFrame{
        /**
     * 
     */
    private static final long serialVersionUID = 1L;
        public button1(){
            double dub1 = Double.parseDouble(num1.getText());
            double dub2 = Double.parseDouble(num2.getText());
            double dub3 = dub2 * dub1; 
            String item3 = Double.toString(dub3);
            new JTextField(item3);
// double num1 = Double.parseDouble(item1.getText());
// double num2 = Double.parseDouble(item2.getText());
// double num3 = Double.parseDouble(item3.getText());
// num3 = num1*num2;
System.out.println(item3);
// String Force = Double.toString(num3);
// System.out.println(Force);
}
// multiplies mass and acceleration to get force

}
class Action implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        double dub1 = Double.parseDouble(num1.getText());
        double dub2 = Double.parseDouble(num2.getText());
        double dub3 = dub2 * dub1; 
        System.out.println(dub3);
        String s = Double.toString(dub3);
        new JTextField(s);
    }

Basically i'm using my Proeject class to make a gui with the 2 text fields and 2 buttons. The first Gui called is SecondClass. When the "Calculate" button is presssed, I want it to make a new gui with dubbleClass and have 3 JTextFields where 2 are the inputs and invisible, and the other one is the two numbers multiplied together then outputted as a string saying something like "Your force is %d" or something like that. Again, thank you for your time.

Recommended Answers

All 9 Replies

The "standard" answer is to have a "model" class that contains the input data and performs the calculation. You create an instance of the and pass it as a parameter to the constructor of all your GUI classes. Now ny GUI class can use the its input fields to set the data values, or query the data and the calculated result for display.

When creating a "model" class, how would i go about getting the inputs to transfer there? I have tried having a class to do exactly what you said, to calculate from the input data, but I can't do it properly because my input values always come back as invalid because they didn't transfer over from the other class. Is there a function used to do this, or am I just overthinking something really simple?

Example of this is code lines 181-189, is that not what you're talking about, and is there any way that I can call that Class from my main Class?

Your model class can have public methods like, for example:

void setArg1(double newValue)
double getArg1()
void setArg2(double newValue)
double getArg2()
double getResult()

so in one GUI you can do stuff like:

model.setArg1(Double.parseDouble(num1.getText()));

and in the other GUI stuff like

num1DIsplay.setText("" + model.getArg1())
resultDIplay.setText("" + model.getResult());

becuase both the GUIs share the same model instance. all those values are shared via the model.

so i've made a "Model" Class, and you say i can use the "model.setArg1(Double.parseDouble(num1.getText()));" function, but I have tried this and the other things said in the comment above, and all I'm getting so far is stack overflow error "Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError" (The error is on the return for the getResult)

public class Model {

    public void setAcceleration(double newAcceleration) {

    }
    public double getAcceleration() {

        return getAcceleration();

    }
    public void setMass(double NewMass) {
    }
    public double getMass() {

        return getMass();

    }
    public static double getResult(){

        return getResult();

    }
}

This is the Model Class i made, if you could tell me what's wrong and how I could fix it, that'd be great! Also thanks for your time JamesCherrill, this is really frustrating and i appreciate your help!

   public static double getResult(){
        return getResult();
    }

As soon as you call this method you will get a stack overflow.
getResult calls getResult in an infinite recursive loop.
I guess it should be something like

   public static double getResult(){
        return <some calculation involving mass and acceleration?>;
    }

my time is cheap... you're welcome.

You should create private variables then your setters and getters to set and access those the values of those private variables respectively.
To clean it up what James suggested, create another method that do the calculations. Here is a basic example.

public class Model{
    private double acceleration;
    private double mass;
    private double force;

    /* Setters */
    public void setMass(double m){
        mass = m;
    }
    public void setAcceleratiob(double a){
        acceleration = a;
    }

    /* Getters */
    public double getMass(){
        return mass;
    }
    public double getAcceleration(){
        return acceleration;
    }
    public double getForce(){
        return force;
    }

    /* Methods */
    public void calculateForce(){
        return mass * acceleration;
    }
}

Then you can call that in the 2 GUI you are implementing.. Gooodluck.

Sorry for not responding, but after weeks of not being able to handle all of java, I decided to switch over to Python, and I must say. The language is SO much easier, and I completed the project I had so much trouble on in Java in about an hour or so in Python. So thank you for all of your time used to help me, but for now I will not be attempting to fix this project.

Yes, Java is way too heavy for this simple application, and something like Python is far more appropriate.

But the real question is: where are you going next?
People use Java to build huge and complex systems for which Python would not be so suitable. If you were planning to stick to relatively simple apps then ignore Java for now. If, on the other hand, this was just a learning exercise at the start of a complete learning process then you should stick with it.
Similarly, with just 2 data items and 2 simple windows, a single code module is OK. But learning to do it that way is a dead-end. For a real app with (eg) data about a customer and their order history, with a dozen GUI forms, you have to have some decent structure/architecture. Once again - where do you want to go with your learning?

Either way, I have marked this thread "solved"for you.
Good luck
J

Well, currently I am just learning some basic programming ie. making a gui to do a simple task like make another gui, or multiply two numbers together, and eventually I will put these things together to make an even more complex gui. I am making this for an internship I am currently in. Personally, I do not plan on learning Java in great detail, but if need be, I do now have a book on Java that I could learn from, but you can bet that if I need any help in the future this is going to be the only site I come to. I love the community here and appreciate any and all help from everyone!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.