3

Ok guys, I've made some changes in my code as told by you. I have 3 classes:

The second class (and first GUI): I have 4 JButtons - Simulare, CazParticular, Start and HandSelection, some JLabels and 3 JTextFields; When I press the HandSelection button another frame it's created with different content.

The 3rd class (and second GUI): I have 2 JButtons - Ok and Cancel and other stuff. When I press the Ok button I want to the access to the JTextField(QuesHandText) from the first Gui and use the method setText(). I can't figure this out, I'm thinking on this like 4-5 days and still can't get the answer. Please help me!

Screenshot

What code should I write in if statement to be able to modify the text in the JTextField from 2nd class (first GUI) ?

First class:

import javax.swing.JFrame;
public class Main {

    public static void main(String[] args){

        //other stuff
        GuiMain gui = new GuiMain();

        gui.frame1.setLocation(150,150);
        gui.frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        gui.frame1.setSize(400,250);
        gui.frame1.setResizable(false);
        gui.frame1.setVisible(true);

        //other stuff
    }
}

Second class:

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import javax.swing.*;

public class GuiMain {

    public static GuiMain instance;
public static GuiMain getInstance(){
    if(GuiMain.instance == null){GuiMain.instance = new GuiMain();}
    return GuiMain.instance;
}
    public JFrame frame1 = new JFrame();

    public JTextField QuesHandText, FlopTurnRiverText, RezultatText; 
    public JButton Simulare, CazParticular, Start, HandSelection;
    public int w1,h1;
    public JLabel someText;
    static int u=0;
    public int j=0;

    public GuiMain(){

        frame1.setTitle("HoldemTool");
        frame1.setLayout(null);
        QuesHandText = new JTextField(4);

        Simulare = new JButton("Simulare");
        CazParticular = new JButton("Caz particular");
        Start = new JButton("Start");
        HandSelection = new JButton(new ImageIcon(getClass().getResource("GuiPic.png")));
        Handler handler1 = new Handler();

        CazParticular.addActionListener(handler1);
        Simulare.addActionListener(handler1);

        HandSelection.addActionListener(handler1);
        Start.addActionListener(handler1);

        QuesHandText.setEditable(false);
        FlopTurnRiverText.setEditable(false);
        RezultatText.setEditable(false);

        frame1.add(Welcome1);
        frame1.add(Welcome2);
        frame1.add(QuesHand);
        frame1.add(FlopTurnRiver);
        frame1.add(Rezultat);
        frame1.add(QuesHandText);
        frame1.add(FlopTurnRiverText);
        frame1.add(RezultatText);
        frame1.add(Simulare);
        frame1.add(CazParticular);
        frame1.add(Start);

    }

    public JTextField getQuesHandText(){
    return QuesHandText;
}
    public class Handler implements ActionListener{
        public void actionPerformed(ActionEvent e){

            if(e.getSource()==Simulare)
            {

            }
            if(e.getSource()==CazParticular){
                QuesHandText.setEditable(true);
                FlopTurnRiverText.setEditable(true);

                QuesHandText.setText("");
                FlopTurnRiverText.setText("");
                RezultatText.setText("");

                frame1.setSize(470, 250);
                Start.setBounds(3*FlopTurnRiverText.getX(), QuesHand.getY(), 65, h1);
                HandSelection.setBounds(3*FlopTurnRiverText.getX(), FlopTurnRiverText.getY(), 65, h1);

                frame1.add(HandSelection);
                frame1.add(Start);
            }
            if(e.getSource()==Start){
                QuesHandText.setText("Text");
            }
            if(e.getSource()==HandSelection){
                GuiSelection gui2 = new GuiSelection();  
                gui2.frame2.setVisible(true);
            }
        }

    }}

3rd class

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import javax.swing.*;

public class GuiSelection extends GuiMain {

    JFrame frame2 = new JFrame();
    GuiMain guiMain;
    public JButton Ok,Cancel;

    //other stuff

    public GuiSelection(){
        guiMain = new GuiMain();
        frame2.setTitle("Hand selection");
        frame2.setSize(1135,535);
        frame2.setLayout(null);
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.setVisible(true);
        frame2.setResizable(false);

        //other stuff

        Handler2 handler2 = new Handler2();

        Ok.addActionListener(handler2);
        Cancel.addActionListener(handler2);

        frame2.add(Ok); frame2.add(Cancel); 

    }

    public class Handler2 implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==Cancel){
                frame2.hide();
            }
            if(e.getSource()==Ok)
            {
            GuiMain.getInstance().getQuesHandText().setText("From Ok");

                //When I prees this button "Ok" I want to get access to the JTextField(QuesHandText) in the         GuiMain class, and .setText();
                //somothing like QuesHandtText.setText("someText");
            }
        }
    }
}
que1326
  • 2,227
  • 4
  • 41
  • 58
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 3) As mentioned, prefer composition to inheritance. – Andrew Thompson Jul 15 '12 at 14:49
  • Hei, can you explain why `GuiSelection` extends `GuiMain`? I can also see there is an object of `GuiMain` in `GuiSelection` – Mohayemin Jul 15 '12 at 15:53
  • I thought I would help me in the future, but it doesen`t ... that`s not the problem. – que1326 Jul 15 '12 at 18:25

5 Answers5

3

Add to your first GUI the method public JTextField getQuesHandText() and a static method public static JFrame getInstance() which returns the instance of the first GUI. Now you can call SecondClass.getInstance().getQuesHandText() from anywhere to get the JTextField instance. Note that with this method you can only have a single instance of SecondClass at any time.

Your getInstance() method will look as follows:

public class SecondClass extends JFrame {

    private static SecondClass instance;

    public static SecondClass getInstance() {
        if(SecondClass.instance == null)
            SecondClass.instance = new SecondClass();

        return SecondClass.instance
    }

}

Note that you shouldn't create an instance of SecondClass manually.

Lorenz
  • 1,263
  • 9
  • 20
  • and the code for getQuesHandText(){ return QuesHandText;} it`s ok ?, I edited too in the main code – que1326 Jul 15 '12 at 19:55
  • take a look at 3rd class, last lines (GuiMain.getInstance().getQuesHandText().setText("From Ok");) – que1326 Jul 15 '12 at 20:02
  • Yes, this is correct. Alternatively, you could replace the function getQuesHandText() with a method which returns the content of your JTextField and a method which sets the content of your JTextField if you want to restrict access to your actual JTextField. – Lorenz Jul 15 '12 at 22:01
  • But use GuiMain.getInstance() instead of new GuiMain() – Lorenz Jul 15 '12 at 22:05
  • I did everything you told me, and still doesen`t work: GuiMain.getInstance().getQuesHandText().setText("From Ok"); – que1326 Jul 16 '12 at 07:16
  • Make sure that the only point where you use the constructor of GuiMain is from inside the newInstance() method (So change the line `GuiMain gui = new GuiMain();` to `GuiMain = GuiMain.getInstance();`). Try to make the constructor of GuiMain private: `private GuiMain() { ... }` instead of `public GuiMain() { ... }`. This ensures that you cannot create an instance of GuiMain from outside the class. – Lorenz Jul 16 '12 at 12:51
3

Use the instance of the initiated Class to get access to its public variables. So you should do something like:

GuiMain main=new GuiMain();
...
main.QuesHandtText.setText("someText");

Alternatively if your JTextField is private (though its not) use an instance method that has public access to change its contents - this is the preferred method:

Class A:

class A {
private JTextField tf;
public void setFieldText(String text) {
tf.setText(text);
}
}

Class B:

class B {
A a = new A();
a.setText("hello");
}
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
3

Use Composition,

1. Create the instance of the class which contains the JFrame, whose JTextField you need to access.

2. Then on that instance call the setter or getter method of the JTextField.

Edited:

Make Sure you have implemented Singleton principle on the Main class, else you will get a new instance , which you dont want...... In Second class.

public class GuiMain{

Main m = new Main();

m.getText(); m.setText();

// Other stuffs

}

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • I think he is having trouble on how to access that instance from the other JFrame, which you do not discuss. – toto2 Jul 15 '12 at 14:52
3

Perhaps you do not really need two Windows. What you need is a Dialog which can be achieved by the class JOptionPane.

Here is a demo code.

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

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

public class YourGui implements ActionListener {
    private JFrame frame;
    private JTextField text;
    private JButton takeInput;

    public YourGui() {
        frame = new JFrame();
        frame.setLayout(new GridLayout(2, 1));

        text = new JTextField();
        takeInput = new JButton("Take Input!");

        frame.add(text);
        frame.add(takeInput);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 100);

        takeInput.addActionListener(this);
    }

    public void show() {
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int selection =
            JOptionPane.showConfirmDialog(frame, "Select Hand", "Select",
                JOptionPane.OK_CANCEL_OPTION);

        if (selection == JOptionPane.OK_OPTION) {
            text.setText("You selected ok");
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                YourGui gui = new YourGui();
                gui.show();
            }
        });
    }
}
Mohayemin
  • 3,841
  • 4
  • 25
  • 54
  • +1 the idea is good, though, you need to change the components of the `JOptionPane` to show the `Jlabel`s for images. Here is one example for [Adding a JPanel to JOptionPane](http://stackoverflow.com/a/10309277/1057230) – nIcE cOw Jul 15 '12 at 16:40
2

In the handler of GuiMain, pass itself (the main JFrame) as an argument to the constructor of GuiSelection:

GuiSelection gui2 = new GuiSelection(this);

And then change the constructor of GuiSelection from

public GuiSelection(){
    guiMain = new GuiMain();
    ...

to

public GuiSelection(GuiMain guiMain){
    this.guiMain = guiMain;
    ...

Also, it seems quite odd that GuiSelection is a subclass of GuiMain. Probably both of these should be direct subclasses of JFrame.

Also, you should wrap everything in the main method within SwingUtilities.invokeLater since everything related to Swing should run on the event-dispatch thread.

Also, you should never use public member variables: it is very un-Java.

toto2
  • 5,306
  • 21
  • 24