*
The moose likes Swing / AWT / SWT and the fly likes Passing Variable Beetween GUI's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Passing Variable Beetween GUI Watch "Passing Variable Beetween GUI New topic
Author

Passing Variable Beetween GUI's

mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24
hi everyone.
ok im a student currently using Sun One Studio 4. My question is..
i have 2 Jframe windows, one called 'main' and one called 'details'
on the 'main' Jframe i have a textbox and a button - for where you input ur name and click the button to open the second GUI window.
on the 'details' Jframe i have simply one textbox for the value of my name which i input on the first window to be loaded into.
how do i go about passing the value of a variable so it can be used in every Jframe. i have the name veriable held from the textbox on the first window, just cant seem to use it on the second:
declaring name as:
static String name;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
name = "" + jTextField1.getText ();
----------------------------------------------------------------
thanks alot, anyfeedback on how i can use this variable throught 2 Jframes or more would great.
toothpick
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Create a constructor in your second JFrame...

And then in your first JFrame when you create the second JFrame...

Let me know if that doesn't make sense. There are other ways to do this, but this is a good easy fast way.


GenRocket - Experts at Building Test Data
mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24
don't quite understand.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by mo robertson:
don't quite understand.

Exactly what don't you understand?
mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24
i just dont get it.
do i still declare name as public string?
ill get code
[ April 23, 2004: Message edited by: mo robertson ]
mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24

-------------------------------------------------------------------

------------------------------------------
maggie woo
Ranch Hand

Joined: Dec 18, 2003
Posts: 35
you're not calling the 2nd frame properly...
don't do

do
mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24
few errors when i tryed that code. unsure why it doing it.
NameInput.java [63:1] name has private access in java.awt.Component
Stored storedFrame = new Stored(name);
^
Stored.java [57:1] cannot resolve symbol
symbol : variable yourname
location: class Stored
jTextField1.setText(yourname);
^
2 errors
Errors compiling class NameInput.
maggie woo
Ranch Hand

Joined: Dec 18, 2003
Posts: 35
Originally posted by mo robertson:
few errors when i tryed that code. unsure why it doing it.
NameInput.java [63:1] name has private access in java.awt.Component
Stored storedFrame = new Stored(name);
^
Stored.java [57:1] cannot resolve symbol
symbol : variable yourname
location: class Stored
jTextField1.setText(yourname);
^
2 errors
Errors compiling class NameInput.

Ok, I didn't examine your code that closely. There are some typos.
First:
Stored storedFrame = new Stored(yourname);
Second:
either:
jTextField1.setText(arg1);
or:
public Stored(yourname){
jTextField1.setText(yourname);
}
mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24
Stored.java [54:1] <identifier> expected
public Stored(yourname)
^
Stored.java [61:1] ')' expected
private javax.swing.JTextField jTextField1;
^
2 errors
Errors compiling Stored(yourname).
maggie woo
Ranch Hand

Joined: Dec 18, 2003
Posts: 35
*sigh*
"public Stored(String yourname) ..."
[please don't remove the curly braces just because i didn't include it here]
If you have compiler errors, you must already have some strategy for figuring out the problem. If nothing else, return to the version that WAS compiling, and add changes one at a time.
[ April 24, 2004: Message edited by: maggie woo ]
mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24
u basically called me a dumbass, you have me the code. if i knew what i was doing i wouldnt have posted in the first place. thats why i posted for help. i posted expecting some awnser and used the awnser i got. to then find out you didnt add in something. im sorry i didnt look at the code, i expected the code u supplyed to work without me go looking on the forum for another way to get the code working.
i mean ok ur good at java and things, but i dont have a clue. i think you should just relax a little and think that others maybe aint quite as good as yourself.
thank you anyhow.
toothpick
maggie woo
Ranch Hand

Joined: Dec 18, 2003
Posts: 35
well, I should have pointed out the problem instead of giving you the answer.
The problem is, you instantiated a Stored object (storedFrame), but you did not call that object's show() method, you called it on a new Stored object (one that never got a peek at the value in the "yourname" variable).
That was the problem. I am not a good teacher, or I would have pointed that out to you and let you figure out the solution yourself. As it was, my giving you the code didn't help at all. Eventually, it's possible that it will get you over one tiny little hurdle, but it hardly advances your knowledge of Java.
If you are a student, why are you using Sun One instead of Notepad or JEdit? Nevermind. I'm an oldie..
mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24
and it still doesnt work.
mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24
we have been told to use this program. thus i dont know if its good or bad, and know very little java myself. i found this forum and thot it would help me, as i could ask experienced java programmers questions of why and how to do things. it seems to have worked so far.
meInput.java [64:1] ')' expected
Stored storedFrame = new Stored(String yourname);
^
1 error
Errors compiling jButton1ActionPerformed.
mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24
ive added brackets left right and centre. cant see where to put it
maggie woo
Ranch Hand

Joined: Dec 18, 2003
Posts: 35
yes, the first correction was in NameInput. here is where you called the .show() method on the wrong thing. instead of calling it on the Stored object you already instantiated (passing in the 'yourname' variable), you called it on a new Stored object that didn't have the 'yourname' variable when it was called.

the second was in the constructor in Stored which orignally accepted an 'arg1' variable, but inside the constructor, you specified a 'yourname' variable that the compiler was complaining about. i was just telling you to make them the same. whatever you pass in, that's what you set to the JTextField:

these are not corrections to what you've done already--don't know what you've done at this point. if you go back to the original code you posted and make these fixes, maybe some of it will work. if there's still stuff not working, sorry for wasting your time..
- maggie
[ April 23, 2004: Message edited by: maggie woo ]
mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24
**** Name Input ****

**** Stored ****

ok it runs, but doesnt work, it produces the following errors when i put my name in and hit the button.
java.lang.NullPointerException
at Stored.<init>(Stored.java:56)
at NameInput.jButton1ActionPerformed(NameInput.java:49)
at NameInput.access$100(NameInput.java:7)
at NameInput$2.actionPerformed(NameInput.java:34)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.Component.processMouseEvent(Component.java:5100)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by mo robertson:
u basically called me a dumbass, you have me the code. if i knew what i was doing i wouldnt have posted in the first place. thats why i posted for help. i posted expecting some awnser and used the awnser i got. to then find out you didnt add in something. im sorry i didnt look at the code, i expected the code u supplyed to work without me go looking on the forum for another way to get the code working.
i mean ok ur good at java and things, but i dont have a clue. i think you should just relax a little and think that others maybe aint quite as good as yourself.
thank you anyhow.
toothpick

maggie wasn't calling you names. I think you need to relax a little. I know this is frustrating for you. But maggie is trying to help you. We don't provide solutions around here. We try and help you learn what you are doing. But it appears to me as if you are trying to fly before you can walk.
With a program like you are working on it would seem that you should have prior knowledge of some basic Java syntax and programming rules. I think that was the assumption maggie made when beginning to help you. I know it was the assumption I made. It is also frustrating to us when we provide help for someone and they just aren't "getting it". But maybe that is our fault for our assumptions.
Instead of just providing us with your erros and code, it would help if you explained what precise parts of the code you aren't understanding. Then maybe we won't have to make so many asssumptions about what you do and do not know.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

The problem with your code now is that you have two constructors. All you need is the 1 constructor...

Then it should work. You were getting a null pointer exception because you were calling 1 constructor which didn't instantiate any of your GUI code (initComponents()).
[ April 24, 2004: Message edited by: Gregg Bolinger ]
mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24
woooooooooooooooooosh, ty alot.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by mo robertson:
woooooooooooooooooosh, ty alot.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Passing Variable Beetween GUI's
 
Similar Threads
JSP pages-passing variables
saving the details in when i click next and previous buttons
Radio buttons(URGENT)
Passing values between windows/frames
refer name in HTML for &lt;input type="image"&lt; failed