0

There are two players who will input their name in the JTextFields. What I want to do is that the data I entered from the Welcome frame in Enter.java will be transferred to the JLabels in ActualGame.java.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class Enter extends JFrame implements ActionListener {

    private String one = "";
    private String two = "";
    private JTextField txtOne = new JTextField();
    private JTextField txtTwo = new JTextField();

    public Enter() {
        this.setLayout(new FlowLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Welcome");
        setSize(200, 130);
        setVisible(true);
        setResizable(false);
        setLocationRelativeTo(null);

        add(txtOne);
        add(txtTwo);

        enter.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e) {
        Main main = new Main();
        this.setVisible(false);
        one = txtOne.getText();
        two = txtTwo.getText();
    }
}

Main is the main class that holds the JFrame of ActualGame() and also the main class of Enter().

import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class Main extends JFrame {

    public Main() {
        add(new ActualGame());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Main");
        setSize(400, 557);
        setVisible(true);
        setResizable(false);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        Enter enter=new Enter();
    }
}

the ActualGame:

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

public class ActualGame extends JPanel{

    private JLabel lblOne = new JLabel(one);//how i wish it would be that easy
    private JLabel lblTwo = new JLabel(two);

    public ActualGame() {

        setLayout(new FlowLayout());
        add(lblOne);
        add(lblTwo);

    }
}

What should I do to be able to use the String variable one and two from Enter.java to ActualGame.java? I'm new and noob in programming especially java swing. Open to criticisms and suggestions. Thank you.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
zxc
  • 9
  • 4

2 Answers2

3

Suggestions:

  • Passing information from one object to another is no different with Swing as with other Java programs. You can call methods or constructors and pass information in via parameters.
  • A key difference though is when to pass information. With event driven programs, this is often triggered by an event, a listener, and so use of the observer design pattern is comment.
  • For your purposes, the first window could be a modal dialog such as a JOptionPane or a modal JDialog which will make it easier to figure out when to pass information. When using a modal dialog, all code flow in the calling program is paused while the dialog is visible, and then resumes once the dialog is no longer visible. It's easy then to have the calling program query the dialog once this occurs, because you'll know precisely where in your code this will occur.
  • You'll want to avoid excessive showing of different windows in your application as it can quickly get annoying to the user. A few dialogs here and there are OK, especially if you need the information to be given in a modal fashion, but in general it's better to swap GUI "views" when needed, and a CardLayout is good for this.
  • But having said this, separate views are often created by separate classes, so the problem of passing information back and forth remains a problem with similar solutions as described above.

Specifically, give your Enter class a getText method that will allow other objects to query it for the state of its JTextField:

public String getTxtOneText() {
  return txtOne.getText();
}

Also, change your ActualGame class so that it can accept String information when needed:

class ActualGame extends JPanel {

   private JLabel lblOne = new JLabel();

   public ActualGame(String text) {
      lblOne.setText(text);
      setLayout(new FlowLayout());
      add(lblOne);

   }

   public void setLblOneText(String text) {
      lblOne.setText(text);
   }
}

e.g.,

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

public class Foo {
   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            ActualGame actualGame = new ActualGame("");
            Main main = new Main(actualGame);
            main.pack();
            Enter enter = new Enter(main);
            enter.setVisible(true);

            actualGame.setLblOneText(enter.getTxtOneText());
            main.pack();
            main.setLocationRelativeTo(null);
            main.setVisible(true);
         }
      });
   }
}

class Enter extends JDialog implements ActionListener {

   private String one = "";
   private JTextField txtOne = new JTextField(10);
   private JButton enter = new JButton("Enter");

   public Enter(JFrame frame) {
      super(frame, "Welcome", true);
      this.setLayout(new FlowLayout());

      enter.addActionListener(this);
      txtOne.addActionListener(this);

      add(txtOne);
      add(enter);
      pack();
      setLocationRelativeTo(null);

      // this has to be done last
      // setVisible(true);
   }

   public String getTxtOneText() {
      return txtOne.getText();
   }

   public void actionPerformed(ActionEvent e) {
      setVisible(false);
   }
}

class Main extends JFrame {
   ActualGame actualGame;

   public Main(ActualGame actualGame) {
      super("Main");
      this.actualGame = actualGame;
      add(actualGame);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

}

class ActualGame extends JPanel {

   private JLabel lblOne = new JLabel();

   public ActualGame(String text) {
      lblOne.setText(text);
      setLayout(new FlowLayout());
      add(lblOne);

   }

   public void setLblOneText(String text) {
      lblOne.setText(text);
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thanks for the quick reply. does the getText() method throws the value to the setText() which throws to the String text? thank you for the suggestions. i will keep that in mind. – zxc Dec 08 '13 at 14:45
  • @JosfYorobe: see edits to answer. Consider editing your question to give more code, more information so we can understand just what you're trying to do. – Hovercraft Full Of Eels Dec 08 '13 at 14:46
  • @JosfYorobe: again please check out my answer and code. Please ask if anything is unclear. – Hovercraft Full Of Eels Dec 08 '13 at 15:31
  • @JosfYorobe: Also store the names in `java.util.Preferences` for future reference. HFOE: I can see your house from [here](http://ekisto.sq.ro):-) – trashgod Dec 08 '13 at 18:50
  • @trashgod: what is that graph? – Hovercraft Full Of Eels Dec 08 '13 at 21:55
  • 1
    Enter your SO id, `522444`, to see your building; I'm next door. – trashgod Dec 08 '13 at 22:34
  • @trashgod: yep, I found you and me, but I have to wonder about what algorithm places us in the island that we inhabit. – Hovercraft Full Of Eels Dec 08 '13 at 22:53
  • @HovercraftFullOfEels: shared tags, I think; GMTA. – trashgod Dec 08 '13 at 23:06
  • @HovercraftFullOfEels, after studying your solution, i now solved my problem. now i have another problem, let's say in my MAIN frame, i have a RESET button. the idea is that, once the RESET is clicked, the ENTER dialog box will appear once again, ask for name and the MAIN frame updates. what i did was use the RUN method from the PSVM. but the previous frame doesnt close instead opens a new one. i just dont want to hide the previous one. i want to close it completely but System.exit(0) closes the program completely. any thoughts? – zxc Dec 13 '13 at 14:09
  • @zxc: that sounds like a new problem perhaps, and if so, then you'll want to ask a new question together with your latest code, preferably an [sscce](http://sscce.org), a program that compiles and runs for us without modification, without need for outside resources such as images or databases, and that is short, without any code not related to your problem or allowing your code to run (as per my example in my answer above). – Hovercraft Full Of Eels Dec 13 '13 at 16:51
0

Try to make ActualGam as a underclass of Enter

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class Enter extends JFrame implements ActionListener {

private String one = "";
private JTextField txtOne = new JTextField();

public Enter() {
    this.setLayout(new FlowLayout());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setTitle("Welcome");
    setSize(200, 130);
    setVisible(true);
    setResizable(false);
    setLocationRelativeTo(null);

    add(txtOne);

    enter.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {
    Main main = new Main();
    this.setVisible(false);
    one = txtOne.getText();

}
class ActualGame extends JPanel{

   private JLabel lblOne = new JLabel(one);

   public ActualGame() {
    setLayout(new FlowLayout());
    Enter.this.add(lblOne);
   }
}
}
Gcinbax
  • 187
  • 3
  • 12
  • thanks for the suggestion but i want to know the answer to my question. your suggestion would be really effective – zxc Dec 08 '13 at 15:03
  • 1
    @JosfYorobe: but not a good suggestion as it is asking you to "un-refactor" your code. I urge you to ignore Gcinbax's answer. – Hovercraft Full Of Eels Dec 08 '13 at 15:03