Share Data Between JFrame instances

Ok, so I'm a jeep at this stuff but......I have a main JFrame GUI but I need to have another screen come up when a data value is entered in a TextField. I need to be able to input a value in a TextField on this second screen so that the first knows about it's value. I've tried several things but get that pesky 'non-static.....static context' message. Any suggestions?

Thanks

[394 byte] By [petemeda] at [2007-11-26 13:02:47]
# 1

Why don't you post a little bit of your code. Make sure to surround the code in code tags. See http://forum.java.sun.com/help.jspa?sec=formatting for help on formatting.

From what you say, I would guess you are trying to do a whole lot in a static main() method instead of doing it in a non-static member somewhere.

Instead of...

public class MyClass

{

public static void main(String args[])

{

// lots of stuff

}

}

... try something like ...

public class MyClass

{

public MyClass

{

// lots of stuff

}

public static void main(String args[])

{

new MyClass();

}

}

codebooka at 2007-7-7 17:06:18 > top of Java-index,Desktop,Core GUI APIs...
# 2

What I've got is something like this:

A JFrame class:

public class MyClass extends javax.swing.JFrame{

......stuff

public static void main....

new MyClass

Then I have another class set up the same as MyClass.

In the body of the first JFrame (not in the main but in an actionlistener) I create an instance of the second MyClass2. In an actionlistener in MyClass2 I try to access a component (like a textfield) in MyClass but get the message about non-static.....in static context. What gives?

Thanks

petemeda at 2007-7-7 17:06:18 > top of Java-index,Desktop,Core GUI APIs...
# 3

It might be a good idea to research more about object orientation before continuing. I could be wrong, but it seems that you are blurring the boundary between a class and an instance of a class.

To fix the problem at hand: MyClass2 needs to have access to the actual instance of MyClass, not just the class itself. There might be several instances of MyClass in your system, and MyClass2 doesn't know which one you want. The approach you're using only works for static methods/properties, since static methods are the same for ALL instances of the class.

Compile and try out this code, it demonstrates how two JFrames can communicate:

import javax.swing.JFrame;

class MyClass extends JFrame{

//a main method to get things started

public static void main(String args[]){

//create an instance of MyClass and show it

MyClass mc = new MyClass();

mc.setVisible(true);

//call MyClass's event - this is analagous to an action event

mc.yourEvent();

}

public MyClass(){

//set up the size and title of MyClass

this.setSize(200,200);

this.setTitle("MyClass instance.");

}

public void yourEvent(){

//create an instance of MyClass2 and show it

MyClass2 mc2 = new MyClass2(this);

mc2.setVisible(true);

mc2.myMethod();

}

public void doSomething(){

//code in MyClass that is called by MyClass2

System.out.println("This code got called by MyClass2.");

}

}

class MyClass2 extends JFrame{

//private variable for keeping a reference to MyClass

private MyClass mc;

//create a new constructor that keeps track of MyClass

public MyClass2(MyClass mc){

//store the reference to MyClass

this.mc = mc;

//set up the size and title of MyClass2

this.setSize(200,200);

this.setLocation(20,20);

this.setTitle("MyClass2 instance.");

}

//other methods can access myclass as follows:

public void myMethod(){

mc.doSomething();

}

}

BillCa at 2007-7-7 17:06:18 > top of Java-index,Desktop,Core GUI APIs...
# 4

I'm totally confused. I looked at the example here but I must be missing something.

My problem is similar. I have a menu program TestMenu.java that calls another class in a second program TestProcessRecs.java. Thisform appears (JFrame with JScrollpane and JTable). I put in my search criteria and hit my process button. I receive, from my MySQL database, the information I am looking for in the table.

What I am trying to do is to make a selection from the table, hit the Modify Header Jbutton. The JButton's code should open a form (JFrame ) in TestHeadUpdt.java that allows me to display and update several fields in the SQl record I will have retrieved.

My problem is that I can't pass the value I captured in TestProcessRecs.java from the table to TestHeadUpdt.java in order to retrieve the right record. How can I share this value between the two JFrames (the JFrames being in separate programs.java)?

vazcroa at 2007-7-7 17:06:18 > top of Java-index,Desktop,Core GUI APIs...