tags:

views:

184

answers:

7

I am currently working on some code with which I am having some trouble.

I have two buttons on a GUI. If one button is pressed it assigns a value to a string a value to reflect this.

The other button is the same except that a different value is assigned to the string.

This String is created at the beginning before the constructor in the following manner:

public string s = "String"; // public so I can call it in another class

The problem comes when I want to find out which button was pressed in another class. I want to see what s is so I have to create an instance of that class GUI:

gui = new GUI();

This then resets the value of s to "String" again and this ruins my comparison.

Is there any way I can get around this?

I've tried lots of ideas but nothing seems to work.

+3  A: 

When you initially create the GUI (i.e. GUI gui = new GUI();), save that reference for when you want to access the member s.

When you create a new GUI object, that new object was never manipulated by the user, so its s value is just "String". You need to hold the reference to the original object.

Welbog
A: 

in the comments below this post, myself and 2 other individuals have determined that you should learn the java langauge, research the notion of object oriented programming (including object scope), and then learn how to implement the proper event handling code - there are several ways in which you can accomplish what you want, i would suggest a combination of the code i provided along with the reference suggestion supplied by Welbog up above (this of course, will require modification of my code, but not much actually if your main class is somewhere else)

basically, google a java tutorial (or buy a book/"acquire" a book), and if you know some basic concepts skip ahead to the object-oriented part.

if i follow what you want, here's some skeleton code:

// imports

public class GUI extends JFrame implements ActionListener {
    public String s = "String";

    public GUI() {
     // initializer code for buttons
     btn1.addActionListener(this);
     btn2.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
     if(s == "???")
      s = "this";
     else
      s = "that";
    }

    // other methods

}
zaczap
What part of his question made you think he needed to know about event handlers?
Geoffrey Chetwood
the whole clicking a button thing?
zaczap
Agreed. He asked about finding which button was pressed.
Mark
@zaczap: And you somehow missed where he doesn't understand how objects work?
Geoffrey Chetwood
well how will any of this possibly help him then?
zaczap
@zaczap: There are people correctly trying to explain how objects work. That should help him. Telling him how to write forms of event handlers certainly will not.
Geoffrey Chetwood
either way, our boy is gonna need to learn how to do both to accomplish what he wants with any sense of mastery over the program. to say either of the top up-voted answers will help him anymore than mine seems rather ridiculous, since we've established he doesn't understand the basic concept
zaczap
@zaczap: Which is why my answer is very generic and forces him to learn instead of just handing him the gun he needs to shoot himself in the foot.
Welbog
@Welbog: i'm not hating on your answer =/ i just felt rather defensive, being attacked for trying to help someone. i'd argue a not fair tone was used against me =(
zaczap
Call the FBI! Someone was mean on the Internet! We cannot let this stand!
Pesto
@zaczap: I'd argue you have to read the question more thoroughly before answering.
Welbog
@zaczap: In what sense do you feel 'attacked'?
Geoffrey Chetwood
@pesto: hilarity ensues =D
zaczap
I am offended by your constant, senseless use of smilies.
Geoffrey Chetwood
(>'_')> <('_'<)
Welbog
@Welblog: hilarity ensues (subtracted smiley face, apparently bad)
zaczap
Welblog? AD HOMINEM AD HOMINEM!!!
Geoffrey Chetwood
@zaczap: What am I? A Google frontend? He can look up what he needs to know to do his damned job on his own time. I'm not going to help someone who can't help himself.
Welbog
@Welbog: complete truth, i removed the other comment already, amazed by the dancing kirby
zaczap
Try using the shift key when you begin a sentence as well.
Geoffrey Chetwood
In fact, use the shift key where appropriate in general.
Geoffrey Chetwood
Can someone please explain what " (>'_')> <('_'<) " is? All I'm seeing is a bunch of random symbols...
Michael Myers
Sorry mom. What about your "AD HOMINEM" rant?
zaczap
@mmyers: Dancing Kirby
zaczap
What you understand me as trying to do is correct. But my problem is it does what it is meant to, but then when the other class creates an instance of it, It resets String s to null again! So when I check to see what 's' is in the other class, it appears as null.
The Special One
@The Special One: That's precisely how objects instances are supposed to behave. Please take the time to get a better understanding of objects and classes. The Internet is full of tutorials to help you.
Welbog
+2  A: 

It is probably better practice to pass the state of your first form to the second form or have the first form inform the second form that a particular event has occurred in real time. Otherwise you would need to pass a reference of the first form to the second form to do the comparison you are trying IE:

public class SecondForm {
    private FirstForm _firstForm = null;
    public SecondForm(FirstForm firstForm){
        _firstForm = firstForm;
    }

    ...

    if(_firstForm.s == "comparison"){
        ...
    }
}

SecondForm secondForm = new SecondForm(this);
Quintin Robinson
A: 

create an extra value that holds the button(name). Change the value when you click on the appropiate button and read it out from a different class. Basicly the same idea as s

PoweRoy
A: 

It's very difficult to decipher your question. But in general, if you want to know which button it was that caused an event, add a different ActionListener to each one (or its model). Keep the code as clean and simple as possible.

Tom Hawtin - tackline
A: 

I would use a listener for observing the state of the 'string' variable.

public class Gui
{
    public static final String STRING_PROPERTY = "string";
    private String string = "String";
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    private void setString(String string)
    {
     String oldValue = this.string;
     this.string = string;
     propertyChangeSupport.firePropertyChange(STRING_PROPERTY, oldValue, this.string);
    }

    public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
    {
     propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
    }
}

This snippet just demonstrates how to add propertyChangeListener support to your class. The class that is interested in knowing the value of 'string' would implement PropertyChangeListener and be added to through the 'addPropertyChangeListener' method.

willcodejavaforfood
Which one of you noobs are to down vote me? :)
willcodejavaforfood
A: 

You could use an ActionListener and an action command on each button as shown in this code

public class GUI  extends JFrame implements ActionListener
{
  public static final String BUTTON_1_PRESSED = "BUTTON_1_PRESSED";
  public static final String BUTTON_2_PRESSED = "BUTTON_2_PRESSED";

  public String s = "String";

  public GUI()
  {
    // create buttons
    button1.addActionListener(this);
    button1.setActionCommand(BUTTON_1_PRESSED);
    button2.addActionListener(this);
    button2.setActionCommand(BUTTON_2_PRESSED);
  }

  public void actionPerformed(ActionEvent e) 
  {
    if(e.getActionCommand().equals(BUTTON_1_PRESSED))
    {
      s = "this";
    }
    else if(e.getActionCommand().equals(BUTTON_2_PRESSED))
    {
      s = "that";
    }
  }
}
Mark