Results 1 to 4 of 4
  1. #1
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    95
    Rep Power
    0

    Question Updating a the text in a jlabel?

    Hi everyone, its me again. Before I ask my question I will confess that I have looked everywhere for a very simple solution to my problem, I don't think what I am trying to do is all that complex. What I am trying to do is change the text in a jlabel in the java netbeans JFrame form but it doesn't update the text. (I am changing the directions shown for a compass) For example this is what I am trying to do:

    Java Code:
    public Compass() {
            initComponents();
            this.setLocationRelativeTo(null);
            atFork=true  //this normally won't be true depending on certain conditions, but I am trying to test strings
        }
    
    private void initComponents() {  
     jLabel2 = new javax.swing.JLabel();
    jLabel2.setText("jLabel2");
    if(atFork){
    jLabel2.setText("East");
    }
    //I repeat the process as shown in the last four lines, I want each jLabel to display a certain direction
    So the JLabel is blank when I try to run this. Any ideas, I know the solution should be simple. I have taken a similar direction in working with Strings in a JTextArea and I can change the texts depending on whether certain booleans are true are not. Why can't I do the same with a JLabel?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: Updating a the text in a jlabel?

    Srop using the NetBeans visual designer until you're comfortable with coding a GUI manually. Trust me, it's easier; also, whenever you run into a problem, you have readable code that you can post and ask for help.

    The code snippet you posted doesn't in any way reveal the cause of your problem, ans I suspect that the entire (generated) code will be highly unreadable, thus reducing your chance of getting any useful help or guidance.

    Also, you need to be aware that there's a section for AWT/Swing questions. I'm moving this there from New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    95
    Rep Power
    0

    Default Re: Updating a the text in a jlabel?

    ok, thats fine. I actually did try to write this in the normal way but I run into a problem with the picture that I am trying to use and found it easier to use the swing editor,(well except for updating jlabels!). But yeah your right this code doesn't show what the problem is, that is the problem. This code runs fine if I only have just one setText. If I have more than one for the same jlabel, then the text is empty or blank

  4. #4
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    95
    Rep Power
    0

    Default Re: Updating a the text in a jlabel?

    nevermind I firgured it out, I have to use setText in my constructor for the conditional and have the default setText in initcomp.

Similar Threads

  1. Replies: 9
    Last Post: 07-23-2013, 01:09 PM
  2. JLabel Align Text
    By OllyHal in forum AWT / Swing
    Replies: 1
    Last Post: 02-18-2012, 02:03 AM
  3. Updating a text file
    By coollucky in forum New To Java
    Replies: 3
    Last Post: 11-29-2011, 05:56 PM
  4. Updating a JLabel from another class.
    By Fumomo in forum AWT / Swing
    Replies: 6
    Last Post: 05-04-2011, 04:31 PM
  5. Swing JLabel Updating (Threading)
    By Skezza in forum AWT / Swing
    Replies: 3
    Last Post: 07-27-2010, 03:43 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
  •