Forums Register Login

Passing Variable Beetween GUI's

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
don't quite understand.
+Pie Number of slices to send: Send
 

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


Exactly what don't you understand?
+Pie Number of slices to send: Send
i just dont get it.
do i still declare name as public string?
ill get code
[ April 23, 2004: Message edited by: mo robertson ]
+Pie Number of slices to send: Send

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

------------------------------------------
+Pie Number of slices to send: Send
you're not calling the 2nd frame properly...
don't do

do
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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);
}
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
*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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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..
+Pie Number of slices to send: Send
and it still doesnt work.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
ive added brackets left right and centre. cant see where to put it
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
**** 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)

+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
woooooooooooooooooosh, ty alot.
+Pie Number of slices to send: Send
 

Originally posted by mo robertson:
woooooooooooooooooosh, ty alot.


This is my favorite tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 4904 times.
Similar Threads
Passing values between windows/frames
Radio buttons(URGENT)
refer name in HTML for &lt;input type="image"&lt; failed
saving the details in when i click next and previous buttons
JSP pages-passing variables
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:36:34.