Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-10-2008, 03:15 AM
Member
 
Join Date: Apr 2008
Posts: 34
kewlgeye is on a distinguished road
How to place a value into a Label on another Jframe
I am trying to set a label to display a stringed message from another Jframe in the project. This string message has a variable concatenated into it of an integer value if that makes any difference. How do I do this? Your help is appreciated, thank you.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-10-2008, 03:39 AM
Senior Member
 
Join Date: Jun 2008
Posts: 451
Fubarable is on a distinguished road
The key here is object to object communication. This can be as simple as passing a reference of one object to the other and calling getter and / or setter methods, or can be more complex such as use of the Observer pattern (such as Swing uses) or its many variants.

As in most programming problems, the devil is in the details. So what are you details? You might consider posting some code that illustrates your problem and what you are trying to do.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-10-2008, 05:04 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,543
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Dealing with object reference can be the solution in easy way, I think. But yes, it's better to provide more details on it.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-10-2008, 05:15 AM
Member
 
Join Date: Apr 2008
Posts: 34
kewlgeye is on a distinguished road
How to place a value into a label on another JFrame
I don't have any code for the label. Its almost like I described it in the original post.

I have a jframe with a calculate button when pushed calculates the number entered places that new number in a sentence and opens up a new JFrame window and shows that result in a jLabel and thats it.

My code for the calculate button works fine, and stores what needs to be stored into an integer and displays the sentence into a string.

I just need to show that string in a jlabel in a new window.

the name of the label is jLabel1
and the string name is resulta
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-10-2008, 05:28 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,543
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Use an object reference to the second JFrame. On the button click of the first frame call the text setting method in the second frame.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-10-2008, 05:52 AM
Member
 
Join Date: Apr 2008
Posts: 34
kewlgeye is on a distinguished road
Eranga

You have helped me out before and i really appreciate it.

I trust that your response is very clear for you, but for me, I am afraid I don't quite understand that. Could you please explain that?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-10-2008, 06:17 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,543
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Ok, you have two classes(basically two classes where you have implemented two JFrames), right?

Can you create an object of one in another?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-10-2008, 06:44 AM
Member
 
Join Date: Apr 2008
Posts: 34
kewlgeye is on a distinguished road
How to place a value into a label on another JFrame
Can you create an object of one in another?


Ok, the only way I know how to do this, is to go to my results page and go for example

public class results extends sunday

sunday is where the string is located that I want to place in the results form jLabel1

I tried reading up but I am not finding anything that seems to help me.

I found a section setLabelfor(); section

i tried doing this in netbeans and found the section in properties but I keep getting an error saying that it cannot find symbol.

if their is some special kind of code that I am suppose to put in the setlabelfor() section in order to access the string from the sunday form I don't know what it is, and so far unsuccesful in finding the information for this problem

still no luck
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 06-10-2008, 07:22 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,543
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Do you know how to work with object of classes?

Use setText() add text into a label.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 06-10-2008, 07:26 AM
Member
 
Join Date: Apr 2008
Posts: 34
kewlgeye is on a distinguished road
I tried the setText section like this

jLabel1.setText(aUnder);

and this way,

jLabel1.setText("", + aUnder);

and I keep getting the error cannot find symbol

I think its because its in another jFrame

I don't know what to do.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 06-10-2008, 07:27 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,543
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
So try what I say.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 06-10-2008, 03:28 PM
Senior Member
 
Join Date: Jun 2008
Posts: 451
Fubarable is on a distinguished road
Swing can be pretty daunting to say the least. You may wish to put Swing on hold for a little bit and brush up on the basics of Java by going through a decent text book.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 06-10-2008, 04:30 PM
Member
 
Join Date: Apr 2008
Posts: 34
kewlgeye is on a distinguished road
I also tried in the setLabel, and setText sections

setLabelfor(sunday.aUnder);

and label.setText(sunday.aUnder);


This is what I thought was referencing something. I couldn't find a page that anywhere that explained how to reference a variable from another JFrame, and I don't know how to read around things that well if it was somehow hidden in a document.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 06-10-2008, 04:32 PM
Senior Member
 
Join Date: Jun 2008
Posts: 451
Fubarable is on a distinguished road
Your problem has nothing to do with JFrame or Swing and everything to do with understanding basic Java. JFrames are objects just like any other. And it is often the case that one object holds variables of other objects.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 06-10-2008, 04:51 PM
Senior Member
 
Join Date: Jun 2008
Posts: 451
Fubarable is on a distinguished road
Here's a simple example:
Code:
import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; /** * This guy holds a JTextField and a JButton. When the button is pressed, any text in the text field gets * shoved into the JLabel on Frame1 */ public class Frame2 extends JFrame { private Frame1 frame1; // here's the variable that holds reference to the Frame1 object private JTextField field = new JTextField(12); public Frame2(String title, Frame1 frame) { super(title); this.frame1 = frame; // here I pass the Frame1 reference to the frame1 variable setLayout(new FlowLayout()); add(field); JButton button = new JButton("Button"); add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String text = field.getText(); frame1.setLabelText(text); // when the button is pushed, field's text gets shoved into Frame1's label } }); } }
Code:
import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * this guy holds a JLabel, "label" whose text is set by the Frame2 JFrame * @author Pete */ public class Frame1 extends JFrame { private JLabel label = new JLabel(); // our label public Frame1(String title) { super(title); JPanel contentPane = (JPanel)getContentPane(); contentPane.setPreferredSize(new Dimension(600, 400)); contentPane.setLayout(new FlowLayout()); contentPane.add(new JLabel("Frame 2 text: ")); contentPane.add(label); // add the label to the JFrame. } /** * creates the second JFrame, Frame2 */ public void showSecondFrame() { // here is the key: I'm passing "this" to the Frame2 object via its constructor Frame2 frame2 = new Frame2("Frame 2", this); frame2.pack(); frame2.setLocationRelativeTo(null); frame2.setVisible(true); } /** * public method that allows Frame2 the ability to change Frame1's label text * @param text */ public void setLabelText(String text) { label.setText(text); } private static void createAndShowUI() { Frame1 frame1 = new Frame1("Frame 1"); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.pack(); frame1.setLocationRelativeTo(null); frame1.setVisible(true); frame1.showSecondFrame(); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
Note that this is not how I'd implement this as I would avoid subclassing a JFrame, and I'd try to loosen the "coupling" of the two classes if possible.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 06-10-2008, 06:21 PM
Member
 
Join Date: Apr 2008
Posts: 34
kewlgeye is on a distinguished road
Thank you, but I don't understand anything you did, or how to use what you have given me.

I'm using netbeans and I don't even know where to put what you have shown me at. I tried going to the sunday page where the label is at and creating an object, I think that is what i did like this.

String underA = new sunday().aUnder;


aUnder holds the string, and UnderA is what I tried to reference from the setlabelfor section of the results page like this

jLabel1.setLabelfor(sunday.underA);


that didn't work, I got some static, non-static referencing error message. on the results page.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 06-10-2008, 07:07 PM
Senior Member
 
Join Date: Jun 2008
Posts: 451
Fubarable is on a distinguished road
Quote:
Originally Posted by kewlgeye View Post
Thank you, but I don't understand anything you did, or how to use what you have given me.
and this is one of the main reasons I strongly recommend newbies to Swing not use code-generation software. It hinders rather than helps your process of learning Swing. My main suggestions are these:

0) Drop your current project, then start to
1) Study Java basics via a decent textbook or the Sun tutorials, and
2) Study Swing itself via the Sun Swing tutorials. This will teach you to generate your own Swing code, but more importantly to understand what this code is doing. Then
3) When you have a good grasp of Swing, then use code-generating software such as NetBeans when you feel it will help you.

Otherwise you will continue to run into frustration after frustration.

Good luck.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 06-10-2008, 07:38 PM
Member
 
Join Date: Apr 2008
Posts: 34
kewlgeye is on a distinguished road
Well, I can't say that I haven't done this already because I have. I did great in my Java College Courses, and am doing this on my own because I felt I didn't get a good grasp on the language so I began my own projects, practicing with different ideas that came along.

I have a few java books that I reference in order to help me with my projects, but I have always had a difficult time reading and comprehending, and often I am used to receiving negative remarks or behaviors from some, and I expect this.

My project consists of about 10 frames with alot of different things, and I have one more coding part to do, and that was this posts original intent. I can't abandon the project though. I will read, and post, and read until i figure it out, which has always proven to be a bit more difficult than it has been for most other in my life. Thanks for the advice and your help.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 06-10-2008, 07:57 PM
Senior Member
 
Join Date: Jun 2008
Posts: 451
Fubarable is on a distinguished road
I'm not meaning to insult you, but I do mean to try to convince you that your process needs fixing, that's all. Please look at my code again. Ignore any layout info and such. The key is that I pass a reference of my Frame1 object into Frame2 via Frame2's constructor. Frame2 needs to be able to call one of Frame1's methods, setLabelText(...), and by calling this method tells Frame1 to show the text that is sent in the method's parameter. It's as simple as that. if any of this doesn't make sense, please let me know.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 06-10-2008, 09:10 PM
Member
 
Join Date: Apr 2008
Posts: 34
kewlgeye is on a distinguished road
I'm trying to study your post, and having a difficult time.

I uploaded an image of a part of my code from the page that displays the label "the results page" I didn' do anything to the code in the sunday page where all the code is located in the event section of the calculation button.

On top you can see I put the sunday res = new sunday() etc.... code their

maybe that doesn't do anything, I don't now. I thought that I was storing the string value from the sunday page into the string rslt, and then I went into the properties window of the jlabel1 and went to the set text and setlabelfor sections and tryed to place the string rslt their, and once or twice I didn't receive any error but still no result.


What I don't understand is this.

// here is the key: I'm passing "this" to the Frame2 object via its constructor
Frame2 frame2 = new Frame2("Frame 2", this);

this.frame1 = frame; // here I pass the Frame1 reference to the frame1 variable


where or how does something like this go in my results page or sunday page. Do I have to erase classes? I mean my result page looks just like the picture and thats it with the exception of the initComponents() which is just the labels positioning.


And don't worry, I am just frustrated that I can't understand this stuff after reading over and over, so many times, and having no one to explain it when I need things. I really need interaction.
Attached Images
File Type: jpg Page with label.jpg (63.9 KB, 4 views)

Last edited by kewlgeye : 06-10-2008 at 09:15 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On