que1326
que1326

Reputation: 2325

access vars from another JFrame Gui

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

Upvotes: 3

Views: 2582

Answers (5)

David Kroukamp
David Kroukamp

Reputation: 36423

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

Upvotes: 3

Mohayemin
Mohayemin

Reputation: 3870

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

Upvotes: 3

toto2
toto2

Reputation: 5326

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.

Upvotes: 2

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

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

}

Upvotes: 3

Lorenz
Lorenz

Reputation: 1303

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.

Upvotes: 3

Related Questions