LeythG
LeythG

Reputation: 191

How to use the same data between two GUI's in Java?

Hey guys I am currently working on a homework assignment. I have two GUI's that I would like to access the same data which will be handled by my other class accountmanager. I believe I need to make one main class that will call both GUI's and create an accountmanager object.

Once I construct the object, how would I send the information to both GUI classes?

Thanks ahead for your help, I hope I was detailed enough. I don't really know how to explain it without posting all of the code.

edit


Ok I appreciate your replies. I am stuck at this..

    Account Accnt = new Account();
GUI1 gui1 = new GUI1();
GUI2 gui2 = new GUI2();

I want to decide what data gets stored for Account Object by using the GUI. For example, GUI1 opens accounts with a name that the user inputs. Account assigns an ID number for each account that is opened. GUI2 can then access information stored for each account by choosing the ID number.

Upvotes: 1

Views: 2417

Answers (6)

user1071777
user1071777

Reputation: 1307

Create the object, pass it (reference) into both GUIs (either through constructor or a method).

Example:

main(){
    Account accnt = new Account();
    // both GUI objects receive the same accnt object
    GUI1 gui1 = new GUI1(accnt);
    GUI2 gui2 = new GUI2(accnt);
    // your code
}

public class GUI1{
    private Account accnt;

    public GUI1(Account accnt){
        this.accnt = accnt;
        // your code
    }
    // your code
}

public class GUI2{
    private Account accnt;

    public GUI2(Account accnt){
        this.accnt = accnt;
        // your code
    }
    // your code
}

Upvotes: 2

Deco
Deco

Reputation: 3321

As others have said, there's a number of ways to go about this but the way you choose to do it needs to reflect your ability (i.e. your teacher/lecturer/etc. will know if you suddenly implement an Observer Pattern and can't explain what it is). In this particular situation I would go with the Singleton approach, as there isn't a database or anything majorly complex.

A singleton is a class where only one instance of it is created and provides a global point of access to the object.

Here's an example of a singleton class and how you might use one:

public final class ExampleSingleton {
    private static ExampleSingleton instance = null;

    private ExampleSingleton() { /*Do not allow instantiation*/ }

    public static ExampleSingleton getInstance() {
        if (instance == null) {
            instance = new ExampleSingleton();
        }

        return instance;
    }

    public void sayHello() {
        System.out.println("Hello!");
    }
}

The above class is using a private constructor which means that you cannot explicity construct an ExampleSingleton. This means that to get a singleton object, you would do something like:

 ExampleSingleton myObject = ExampleSingleton.getInstance();

Then to use the object you would reference myObject and any methods therein (in this case sayHello().

What this means for you, is that your code can end up looking something like this:

public class MyGui1 {
    private ExampleSingleton myData = null;

    public MyGui1() {
        myData = ExampleSingleton.getInstance();
    }

    public updateData(. . .) {
        myData.updateData(. . .);
    }

    public retrieveData(. . . ) {
        myData.retrieveData(. . .);
    }
}

public class MyGui2 {
    private ExampleSingleton myData = null;

    public MyGui2() {
        myData = ExampleSingleton.getInstance();
    }

    . . . 

    //Do you see where I'm going with this?
    }
}

public class Main {
    public static void Main(String args[]) {
        ExampleSingleton myData = ExampleSingleton.getInstance();
        myData.setInitialData(. . .);

        MyGui1 = new MyGui1();
        MyGui2 = new MyGui2();

        //Do more stuff here
    }
}

Because both GUI objects are using the same singleton, the data will always be available to both.

Upvotes: 1

Jaa-c
Jaa-c

Reputation: 5137

You might want to study some design patterns first. You can use observer pattern or you can use MVC design, which you'll have to learn sooner or later anyway, if you wanna do some more advanced programming...

Just google "java mvc example" and give it a try.

Upvotes: 1

Paul Croarkin
Paul Croarkin

Reputation: 14675

Use the Observer Pattern.

Have the AccountManager class implement java.util.Observable.

Have the GUI classes implement java.util.Observer.

Call addObserver() on AccountManager for each of the GUI classes.

Call notifyObservers when a change occurs in AccountManager.

Upvotes: 1

atedja
atedja

Reputation: 1516

You can do it in two ways:

  1. Create a universally accessible data (i.e. singletons) that the two GUIs can access
  2. Pass the data to the GUI

    Data theData = new Data();
    GUIObject object1 = new GUIObject();
    GUIObject object2 = new GUIObject();
    object1.setData(data);
    object2.setData(data);

Upvotes: 0

Dennis
Dennis

Reputation: 4017

Your question is a bit vague; there are many ways to go about this, here is one example.

public class MyData()
{
   ...my data
}

public class ViewOne extends JFrame
{
   MyData myData = new MyData;
}

public class ViewTwo extends JFrame
{
   MyData myData = new MyData;
}

Upvotes: 0

Related Questions