que1326
que1326

Reputation: 2325

How to get acces to elements from another GUi?

First I have a GUI (gui1), when I press a button, a different GUI (gui2) is created. My question is: How I can get access to elements from gui2, using methods from gui1?

Example: When I press a button from gui1, I want to QuesHandText.setText(myVector[0]); QuesHandText is a JTextField from gui1 and myVector[0] a var from gui2. The result error message: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

When I press Ok from Gui2 , I want to setText for the JTextField on Gui1 http://img72.imageshack.us/img72/2822/36185233.png

  //imports
  public class Gui extends JFrame{

  public JButton Simulate, Particular, Start, HandSelection;
  public JTextField QuesHandText, FlopTurnRiverText, RezultatText; 
  public Gui g;

  public Gui()
    {
      QuesHandText = new JTextField(4);
      //instruct
      ClassParticular handler1 = new ClassParticular();
      Particular.addActionListener(handler1);
    }

  public Gui(String t)
    {

      //instruct
      myVector[0]="some_string";
      myVector[1]="some_string2";
    }

  public class ClassParticular implements ActionListener{

    public void actionPerformed(ActionEvent event){


        //instruc

        HandSelection hs = new HandSelection();
        HandSelection.addActionListener(hs);

        StartClass hndlr = new StartClass();
        Start.addActionListener(hndlr);
        add(HandSelection);
        add(Start);

    }
}

   public class HandSelection implements ActionListener{
    public void actionPerformed(ActionEvent e){
        g = new Gui("Hand selection");
        g.setVisible(true);
        g.setSize(1135,535);
        g.setDefaultCloseOperation(HIDE_ON_CLOSE);
        g.setResizable(false);


    }
}

   public class StartClass implements ActionListener{
    public void actionPerformed(ActionEvent event){

        QuesHandText.setText(myVector[0]); // THE PROBLEM IS HERE, I KNOW IT !!

    }
}

}

Upvotes: 0

Views: 184

Answers (2)

Mohayemin
Mohayemin

Reputation: 3870

You have two constructors of Gui.

public Gui()

And

public Gui(String t)

You have initialized QuesHandText in the first one, but not in the second one.

If you use the second one to initialize the Gui you are supposed to get a NullPointerException.

I think you should do this in constructors:

[Edited as suggested by Kleopetra]

public Gui(){
   this("");
}

public Gui(String t){
   //instruct (I am not sure what it means)

   quesHandText = new JTextField(4);
   classParticular handler1 = new ClassParticular();
   particular.addActionListener(handler1);

   myVector = new String[2]; // or some other size you need.
   myVector[0]="some_string";
   myVector[1]="some_string2";
}

Upvotes: 3

mKorbel
mKorbel

Reputation: 109815

1.your problem is

public class Gui extends Jframe{

that should be

public class Gui extends JFrame{

2.another problems are

public JButton Simulate, Particular, Start, HandSelection;
public JTextField QuesHandText, FlopTurnRiverText, RezultatText; 
public Gui g;
  • remove JButton and JTextField because they are JComponents and API names

  • or declare JButton and JTextField correctly

.

public JButton myButton, ...
public JTextField myTextField, ...

3.don't extends JFrame create that as local variable

4.don't re_create a new GUI from ActionPerformed use CardLayout

Upvotes: 3

Related Questions