Results 1 to 6 of 6
  1. #1
    bdn1404 is offline Member
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Default Edit JPanel Text During Runtime...from another class

    I am very new to Java and am having trouble changing text at runtime in a JLabel. My main class is set up to handle the computation and displays the UI frame when necessary. I am reading in data real-time so i need to be able to constantly change the label in my frame as the data changes.

    I have been testing to see if I could make the runtime changes by putting the code directly into the UIFrame's main. I created the label using the Netbeans design editor and then used this code:

    Java Code:
    JLabel jLabel1 = null;
    jLabel1.setText("text");
    which gave me no errors, but had no effect on the label. How do I link my code-created instance with the Netbeans-created label?

    Then, if I can get that to work, how do I access that same label from my (seperate) Main class?

    Thanks
    Last edited by bdn1404; 07-24-2007 at 11:30 PM. Reason: Typo

  2. #2
    elizabeth is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    Write this
    Java Code:
    JLabel jLabel1= new JLabel("text");
    instead of

    Java Code:
    JLabel jLabel1 = null;
    jLabel1.setText("text");

  3. #3
    bdn1404 is offline Member
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Default

    I have tried that as well. It seems that my frame is not changing anything at runtime. I have tried repainting it, but nothing will change unless I get an event involved.

    When I used a button click event as a test, I was able to use the same jPanel1 that I created in Netbeans, because of the nonstatic context. However, when I try to do the same in a main, I have to create an instance which isn't changing.

  4. #4
    mary is offline Member
    Join Date
    Jul 2007
    Posts
    51
    Rep Power
    0

    Default

    please send me the complete code to test it in my machine

  5. #5
    rhobincu is offline Member
    Join Date
    Aug 2007
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by bdn1404 View Post

    Java Code:
    JLabel jLabel1 = null;
    jLabel1.setText("text");
    Check the console. This will most surely generate a NullPointerException.

  6. #6
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    Maybe im wrong, but this is what i get in mind
    Quote Originally Posted by bdn1404
    need to be able to constantly change the label in my frame as the data changes.
    Why don't u use a function with a String variable parameter to replace "text" in that class ur working on so that it that can be called from ur main class, maybe like this
    Java Code:
    JLabel jLabel1 = new JLabel(initialValue);
    setLabel(String input)
    {
    	jLabel1= new JLabel(input);
    }
    And yes, u better post the code

Similar Threads

  1. How To Edit/Add JSP Pages in NetBeans IDE
    By JavaForums in forum NetBeans
    Replies: 2
    Last Post: 02-17-2009, 12:14 PM
  2. Example of RunTime class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-04-2008, 10:32 AM
  3. Referencing JPanel Class
    By uncopywritable in forum New To Java
    Replies: 2
    Last Post: 08-12-2007, 02:31 PM
  4. how to edit lines.
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 05:41 AM
  5. How To:Use a JSlider to adjust Text size in a JPanel
    By louiebagz in forum AWT / Swing
    Replies: 2
    Last Post: 07-01-2007, 08:37 AM

Posting Permissions

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