Results 1 to 6 of 6
  1. #1
    nonabhai is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default problem with JLabel.setText();

    I have two classes:
    Class1, Class2
    Class1 has a Jbutton and class2 has a Jlabel
    //////////////////////////////////////////////////////////////////////////
    What am tryng to do is when I press Jbutton of class1, a method of class2 should be called to change the text of Jlabel.
    My Code is:
    code1:
    ////////Class1 Code////////

    Java Code:
    public class Class1 extends javax.swing.JFrame implements actionListner {
        public Class1() {
            this.setLayout(new FlowLayout());        
            this.add(new Class2());
            this.add(b1);
            b1.addActionListner();  
        }
     JButton b1 = new Jbutton(); 
     Class2 clas2 = new Class2();
    
    public void actionPerformed(ActionEvent e) {
           clas2.set();
        }
    ///////Class2 Code///////

    Java Code:
    public class Class2 extends javax.swing.JFrame implements actionListner {
        public Class2() {
            this.setLayout(new FlowLayout());        
            this.add(L1);  
        }
     JLabel L1 = new JLabel("Original Text");
    
    public void set(){
        java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    jLabel1.setText("Text Changed");
                    }
                }
            });
        }
    The problem is that its not uppdating the text on JLabel...
    (system.out.println(L1.getText()) shows updated text)

    How can I fix it please help!!!

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    please post a compilable code - your code makes no sense :)
    Last edited by eRaaaa; 10-08-2010 at 07:03 AM.

  3. #3
    nonabhai is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default

    Java Code:
    ///////Class1 Code///////
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class Class1 extends javax.swing.JFrame implements actionListner {
        public Class1() {
            this.setLayout(new FlowLayout());        
            this.add(clas2);
            this.add(b1);
            b1.addActionListner();  
        }
    JButton b1 = new Jbutton();
    Class2 clas2 = new Class2();
    public void actionPerformed(ActionEvent e) {
           clas2.set();
        }
    
    public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
    }
    /////Class2 Code///////
    import javax.swing.*
    
    public class Class2 extends JPanel implements actionListner {
        public Class2() {
            this.setLayout(new FlowLayout());        
            this.add(L1);  
        }
     JLabel L1 = new JLabel("Original Text");
    
    public void set(){
        SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    jLabel1.setText("Text Changed");
                    }
                }
            });
        }
    Thanks eRaaaaa for reply :)... is it ok now?

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Quote Originally Posted by nonabhai View Post
    is it ok now?

    not really :)

    - what is "actionListner" ? (ActionListener?)
    - JButton b1 = new Jbutton(); ( = new JButton() !!!)
    - b1.addActionListner(); (addActionListener(this) ?? )
    - JLabel L1 and jLabel1.setText ....
    - Class2 does not implement ActionListener :)
    - what is NewJFrame ??

    do you mean:
    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    
    public class Class1 extends javax.swing.JFrame implements ActionListener {
    	public Class1() {
    		this.setLayout(new FlowLayout());
    		this.add(clas2);
    		this.add(b1);
    		b1.addActionListener(this);
    	}
    
    	JButton b1 = new JButton();
    	Class2 clas2 = new Class2();
    
    	public void actionPerformed(ActionEvent e) {
    		clas2.set();
    	}
    
    	public static void main(String args[]) {
    		java.awt.EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				new Class1().setVisible(true);
    			}
    		});
    	}
    }
    Java Code:
    import java.awt.EventQueue;
    import java.awt.FlowLayout;
    
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class Class2 extends JPanel {
    	public Class2() {
    		this.setLayout(new FlowLayout());
    		this.add(L1);
    	}
    
    	JLabel L1 = new JLabel("Original Text");
    
    	public void set() {
    		EventQueue.invokeLater(new Runnable() {
    			@Override
    			public void run() {
    				L1.setText("Text Changed");
    			}
    		});
    	}
    }
    :confused: :eek:

  5. #5
    nonabhai is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default

    Sooooorry! eRaaaa... yes you are right,

    I was trying to summarize the problem, but in hurry I made mistakes in editing (I was getting late to college) ... But you predicted the right code :) ..
    Sorry again
    Last edited by nonabhai; 10-08-2010 at 06:46 PM.

  6. #6
    nonabhai is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default

    Thank you eRaaaa the problem is solved now!!! In fact I realized my stupidity :D
    Cheers! :) :) :)

Similar Threads

  1. If statement and JLabel.setText() problem
    By peterhabe in forum New To Java
    Replies: 5
    Last Post: 07-25-2010, 10:11 AM
  2. setText() problem
    By Jozo in forum Java Applets
    Replies: 4
    Last Post: 04-27-2010, 05:29 AM
  3. Jlabel update problem
    By fantasyme in forum AWT / Swing
    Replies: 3
    Last Post: 04-14-2010, 05:10 AM
  4. setText() problem
    By jls7168 in forum New To Java
    Replies: 2
    Last Post: 02-20-2009, 10:34 PM
  5. JLabel + GUI problem
    By tonyelaltaico in forum Java Applets
    Replies: 5
    Last Post: 02-03-2009, 01:46 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
  •