0

asked a question early but didn't layout my code very well, and the whole question got a bit muddled, then as I changed my code but still have the same issue, the question still remains, however I decided to re-ask the question with my code laid out much neater so you can see the important bits.

Here's the link to the initial question:

Cannot get variable from Child back to Parent in JAVA (Options Window)

Basically I have a variety of classes. Three of which are:

Menu()
HUD()
Options()

My main class is Menu() and from Menu(), HUD() is opened via a button. Menu is not causing me any issues, however, HUD() and Options() are.

Options() is opened up from inside HUD() and in there are 5 buttons groups, containing 14 options between them. When the user selects each option for the button group, he then clicks Apply, and the values (string values) of the Radio Button Selected for each group, should be assigned to given variables on HUD(). However, when clicking on Apply, it clears all the variables instead, or that's what it appears to be doing.

Apply Button in the Option() class:

private void cmdApplyActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:

    hud.setTime(btnTxtTime);
    hud.setTemp(btnTxtTemp);
    hud.setSurface(btnTxtSurface);
    hud.setWeather(btnTxtWeather);
    hud.setRadiation(btnTxtRadiation);
    dispose();
}  

This is a section of the Option() Class.

public class Options extends javax.swing.JFrame {

    public String btnTxtTime;
    public String btnTxtTemp;
    public String btnTxtSurface;
    public String btnTxtWeather;
    public String btnTxtRadiation;
    public static boolean ApplyClicked;

/**
 * Creates new form Profile
 */

    private HUD hud;

    public Options(HUD hud) {
        initComponents();
        this.hud = hud;
    }

This is a method in Option() class:

public String getTime() {

    if ("Day".equals(grpTimeOfDay.getSelection())) {
        btnTxtTime = "Day";
        return this.btnTxtTime;
    }

    if ("Night".equals(grpTimeOfDay.getSelection())) {
        btnTxtTime = "Night";
        return this.btnTxtTime;
    }
    return null;
}

This is how Options() is openned from within HUD():

private void cmdOptionsActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:

    Options o = new Options(hud);
    this.getLocation(p);
    o.setLocation((int) p.getX() + 100, (int) p.getY() + 100);
    o.setVisible(true);
}        

This is the start of my HUD() Class:

public abstract class HUD extends javax.swing.JFrame implements Runnable {

    private Options o;
    private HUD hud;


    public HUD(Options o) {

        initComponents();
        this.o = o;

and this is the method from HUD() which gets the value of the JButtons from Options():

public void setTime(String strTime) {

    strTime = o.getTime();
    txtTime.setText(strTime);
}
Community
  • 1
  • 1

1 Answers1

1

Let's take a look at just one method:

public void setTime(String strTime) {
    strTime = o.getTime();
    txtTime.setText(strTime);
}

You call this from the Option class

hud.setTime(btnTxtTime);

But setTime is ignoring the value you pass in and immediately overwriting it with o.getTime().

It's hard to tell from what you've posted, but you may be confused about how method calls and arguments work.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • but don't I need to call getTime from Options() to get the value that setTime then sets in HUD()? – Adam Ray Bicknell Feb 23 '13 at 00:31
  • You are passing `btnTxtTime` from `Options#cmdApplyActionPerformed` when you call `setTime()`. Without seeing a lot more of your code it's hard to tell what you are doing. – Jim Garrison Feb 23 '13 at 00:33
  • I have uploaded my Login() ... which is actually my main class not Menu() ... Menu(), HUD() and Options(). Users logins in on Login(), Menu() opens, HUD is opened from Menu(), Options is opened from HUD() ... http://www.sendspace.com/filegroup/ra%2BXT2lJunDn7XNVaRCBQQt7nlSWU06R – Adam Ray Bicknell Feb 23 '13 at 00:47
  • Any help you can give me Jim is massively appreciated :) – Adam Ray Bicknell Feb 23 '13 at 01:01
  • Sorry, but this isn't the way SO works. I suggest you take your code and extract a very simple example of what is confusing -- two classes, two methods, showing what you want to have happen, without all the complications of Swing, etc. Delete this question and post the simplest possible example with no extraneous data as a separate question. – Jim Garrison Feb 23 '13 at 01:22