Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Question 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Dealing with object reference can be the solution in easy way, I think. But yes, it's better to provide more details on it.

  4. #4
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Default 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

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  6. #6
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Default

    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?

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, you have two classes(basically two classes where you have implemented two JFrames), right?

    Can you create an object of one in another?

  8. #8
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Default 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

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  10. #10
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Default

    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.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  13. #13
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Default

    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.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Here's a simple example:
    Java 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
          }
        });
      }
    }
    Java 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.

  16. #16
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Default

    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.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  18. #18
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Default

    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.

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  20. #20
    kewlgeye is offline Member
    Join Date
    Apr 2008
    Posts
    34
    Rep Power
    0

    Default

    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 Thumbnails Attached Thumbnails How to place a value into a Label on another Jframe-page-label.jpg  
    Last edited by kewlgeye; 06-10-2008 at 09:15 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. How can i copy a folder from one place to another..
    By rajeshgubba in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 03:21 AM
  2. How to place the GUI components in correct order
    By impact in forum New To Java
    Replies: 2
    Last Post: 05-04-2008, 07:41 AM
  3. How to place panel into frame
    By vivek_9912 in forum AWT / Swing
    Replies: 2
    Last Post: 11-20-2007, 12:21 AM
  4. how to place a divider of the splitpane?
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:41 PM
  5. how to place an image in an applet
    By paty in forum Java Applets
    Replies: 2
    Last Post: 08-04-2007, 06:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •