0

I've been having some difficulty with this. Basically I want a user to enter a name in one Java class and have that data displayed in another. I try using gets but I keep getting 'null'. Also how would you do this if I wanted to get an int from my class (exampl) to display in my second class. Would it basically be the same?

Here's an example to show what I mean

1st class

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Exampl extends JFrame implements ActionListener {

    JLabel intro = new JLabel("Welcome What is your name?");
    JTextField answer = new JTextField(10);
    JButton button = new JButton("Enter");
    final int WIDTH = 330;
    final int HEIGHT = 330;
    JLabel greeting = new JLabel("");
    JButton next =new JButton("Next");
     String name = answer.getText();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public Exampl() {
        super("Welcome");
        setSize(WIDTH, HEIGHT);
        setLayout(new FlowLayout());
        add(intro);
        add(answer);
        add(button);
        add(greeting);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.addActionListener(this);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        if (source == button) {
             String greet = "Hello there" + name;
             greeting.setText(greet);
              add(next);
       next.addActionListener(this);

           if(source==next)
            dispose();
            new Hello();
        }

    }


      public static void main(String[] args) {
        Exampl frame= new Exampl();
        frame.setVisible(true);

    }
}

2nd class

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Hello extends JFrame implements ActionListener {
    String name;
    JLabel hi = new JLabel("Nice to see you "+name);
    JButton button = new JButton("Enter");
    final int WIDTH = 330;
    final int HEIGHT = 330;
    JLabel greeting = new JLabel("");
    JButton next =new JButton("Next");

    public Hello() {
        super("Welcome");
        setSize(WIDTH, HEIGHT);
        setLayout(new FlowLayout());
        add(hi);
        add(button);
        add(greeting);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.addActionListener(this);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        if (source == button) {
             String greet = "example " + name;
             greeting.setText(greet);
              add(next);
        }
    }

    public static void main(String[] args) {
        Exampl frame= new Exampl();
        frame.setVisible(true);           
    }        
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Winderd
  • 145
  • 6
  • Yeah. I just added the second class to show what I mean. I want whatever value is inputted to go to the next JPanel. For example if someone writes down 'Fred' in the first class I want that value to be able to be displayed on the second class – Winderd Dec 12 '16 at 19:07
  • Possible duplicate of [Passing values between JFrames](http://stackoverflow.com/questions/7017063/passing-values-between-jframes) – Frakcool Dec 12 '16 at 19:20
  • Thank you, that's very helpful in that it simplifies things but I want to know how to move a value to another class (such as int). I'm planning on doing a project where I add the inputted name and a random int into a file so I can store it. Or would it make more sense to add that code into the same class? – Winderd Dec 12 '16 at 19:32
  • Read the edit on my answer below – Frakcool Dec 12 '16 at 19:37

2 Answers2

2

My recommendations for you:

  1. Don't extend JFrame, instead extend JPanel. See: Extends Frame vs creating it inside the program and Why shouldn't you extend JFrame and other components
  2. Don't use multiple JFrames. See The use of multiple JFrames, good / bad practice? (BAD) instead you might want to try using Card Layout or JDialogs

What do you mean by extend? do you mean "public class Hello extends Exampl"? I'm new to java so I don't know much.

  1. From that comment on another answer, you might want to learn the basics first in console applications before going into a GUI application which adds more complexity to your programs and thus your learning.

I'm planning on doing a project where I add the inputted name and a random int into a file so I can store it. Or would it make more sense to add that code into the same class?

  1. From that comment, you can create a public method which returns the value in a JTextField in the format you need, then call that method from the other class, for example:

    public String getUserName() {
        return userNameField.getText();
    }
    
    public int getDigit() {
        try {
            return Integer.parseInt(digitField.getText());
        } catch (NumberFormatException nfe) {
            nfe.printStackTrace();
        }
        return -1; //In case it's not a number, or return 0, or whatever you need
    }
    
    //------In another class------//
    
    int digit = object1.getDigit(); //Where object1 is an instance of the other class where those methods are defined
    
Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
0

Make a getter in first class and extends second one class then use super.getter();

  • What do you mean by extend? do you mean "public class Hello extends Exampl"? I'm new to java so I don't know much. – Winderd Dec 12 '16 at 19:18