Note that there are some explanatory texts on larger screens.

plurals
  1. POaccess vars from another JFrame Gui
    text
    copied!<p>Ok guys, I've made some changes in my code as told by you. I have 3 classes:</p> <p>The second class (and first GUI): I have 4 <code>JButton</code>s - Simulare, CazParticular, Start and HandSelection, some <code>JLabel</code>s and 3 <code>JTextField</code>s; When I press the HandSelection button another frame it's created with different content.</p> <p>The 3rd class (and second GUI): I have 2 <code>JButton</code>s - Ok and Cancel and other stuff. When I press the Ok button I want to the access to the <code>JTextField(QuesHandText)</code> from the first Gui and use the method <code>setText()</code>. 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!</p> <p><img src="https://i.stack.imgur.com/S9JGS.jpg" alt="Screenshot"></p> <p>What code should I write in if statement to be able to modify the text in the <code>JTextField</code> from 2nd class (first GUI) ?</p> <h2>First class:</h2> <pre><code>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 } } </code></pre> <h2>Second class:</h2> <pre><code>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); } } }} </code></pre> <h2>3rd class</h2> <pre><code>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"); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload