0

I am currently working on a problem in Java Swing where I will need to have multiple jFrames (equipment lists, add equipment, remove equipment etc.). I need to be able to access certain variables across all of my jFrames.

Any ideas would be great! Thanks

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    Your frames could ... share a common object, holding those variables ... – Andy Thomas Mar 31 '16 at 16:28
  • 3
    1. Separate your model code (the code that holds the logic and data) from your view code (the GUI code), and this all can get neatly simplified. 2. You really don't want to use multiple JFrames and neither do the user's of your program. Please read [Multiple JFrames: Good or Bad](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad) for more on this. 3. If you need more specific help, please ask a much more specific question, one that shows pertinent code, preferably a valid [mcve] (please check out the link). – Hovercraft Full Of Eels Mar 31 '16 at 16:30
  • Note also that your question is one related to Swing, a GUI library, and not NetBeans, an IDE. – Hovercraft Full Of Eels Mar 31 '16 at 16:31
  • Please note that this site is not a code writing service. If you [edit] your question to describe what you have tried so far and where you are stuck, then we can try to help with specific problems. You should also read [ask]. – Toby Speight Mar 31 '16 at 17:21

3 Answers3

1

You can use a common data object to store values that need to be available in all your classes.

First, create a new class to hold your data:

public class DataObject{
    public String value1 = "";
    public String value2 = "";
    public int int1 = 0;
}

Second, define your JFrame classes something like this:

public class Frame1 extends JFrame{

    private DataObject data;

    public Frame1(DataObject data){
        this.data = data;
    }


    public void doStuff(){
        // ...do some stuff
        data.value1 = "Some new value";
    }       
}



public class Frame2 extends JFrame{

    private DataObject data;

    public Frame2(DataObject data){
        this.data = data;
    }


    public void whatever(){
        if( data.value1.equals("Some new value") )
            // ...do some stuff
    }       
}

And last, create new instances of your JFrame classes, giving them the same instance of DataObject in their constructors, like this:

DataObject myDataObject = new DataObject();
Frame1 frame1 = new Frame1(myDataObject);
Frame2 frame2 = new Frame2(myDataObject);
Magnus
  • 17,157
  • 19
  • 104
  • 189
0

Simplest way: Make your Frames to hold reference to equipment container.

Intermediate way: Supervisor holds reference to your Frames, and feeds them with data, and informs if they should be visible/moved/changed.

Hardest way: Model that contains equipment container, holds references to your presenters, that hold references to your Frames. Model updates presenters that control the Frames. Frames send information to presenters, and they inform the model about your actions.

PayWong
  • 9
  • 2
  • 2
    I think you're making it sound way to complicated. My guess is that the OP won't understand one word of the _Intermediate way_ and _Hardest way_, or else he wouldn't have asked the question in the first place. – Magnus Mar 31 '16 at 16:59
-1

Let's say you have a class called frame1 with a variable called equipment and this variable's value is "hello".

public class frame1{
public static String equipment = "hello";
}

Now let's name you're second frame frame2.

public class frame2{
}

So let's say that in frame2 you want to output the value of equipment from frame1.

public class frame2{
System.out.println(frame1.equipment);
}

You would just print out yourClass.yourVariable

If you have a class in a different package you would print out:

yourPackage.yourClass.yourVariable
Kröw
  • 504
  • 2
  • 13
  • 31
  • 1
    This suggests the usage of global state, which is always a bad idea. See http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil – Stefan Dollase Mar 31 '16 at 16:50
  • @StefanDollase Ok, this isn't ALWAYS a bad idea, or it wouldn't have been implemented in the language, but I can see how it isn't the best solution for this problem. – Kröw Oct 31 '16 at 21:59