Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Arrow adding label to the anel of other class

    hai...
    plz help...i wanna add a label of one class to the panel of other class... this label will be relocated always using thread.sleep to show its movement... so im keeping that in separate file..but the panel needs no change... how to accomplish this?... anybdy plz help..


    thanxs..

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    in the class of the label:
    Java Code:
     private JLabel jlabel1;
     public JLabel myLabel = jlabel1;
    in the other class:
    Java Code:
     mainClass.myLabel.setText("hello");
    Last edited by ozzyman; 03-20-2011 at 02:02 AM.

  3. #3
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    thank u...

  4. #4
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    /* hai, this is a class file where the paint method will draw the boundary and the label is also included in a particular location using the launch function.....
    below is the function which is calling a thread where that thread must change the location of the label in a sequence and show its movement using sleep....
    i dnt no wats the problem with dis....der is no movement of that label... y is it so?..
    plz help..
    */

    public void launch() {

    Icon icon = new ImageIcon("D://img.gif");
    label=new JLabel();
    label.setIcon(icon);
    panel.add(label);
    label.setBounds(180, 40, 15, 41);
    move s=new move();

    }


    // move class

    import javax.swing.*;
    public class move implements Runnable {
    Thread th;
    public move()
    {

    th.start();

    }



    public void run()
    {



    try
    {
    field f=new field();
    f.player1.setBounds(280, 40, 15, 41);
    th.sleep(1000);
    f.player1.setBounds(290, 30, 15, 41);
    } catch (Exception e) {
    System.out.println(e);
    }
    }


    thank u...

  5. #5
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

    Default

    im sorry....iv changed that label name....

    below is my code.. sorry for the inconvenience.....plz check dis code and help me...


    Java Code:
    /* hai, this is a class file where the paint method will draw the boundary and the label is also included in a particular location using the launch function.....
    below is the function which is calling a thread where that thread must change the location of the label in a sequence and show its movement using sleep....
    i dnt no wats the problem with dis....der is no movement of that label... y is it so?..
    plz help..
    */
    
    //boundary class...
    public JLabel label;
     public void launch() {
    		
            	 Icon icon = new ImageIcon("D://img.gif");
            label=new JLabel();
            label.setIcon(icon);
            panel.add(label);
            label.setBounds(180, 40, 15, 41);      
           move s=new move();
    
    }

    Java Code:
    // move class
    
    import javax.swing.*;
        public class move implements Runnable {
    Thread th;
        public move()
        {
           
           th.start();
            
        }
            
        
    
        public void run() 
        {
             
                try
                {
                  boundary b=new boundary();
                   b.label.setBounds(280, 40, 15, 41);
                    th.sleep(1000);
                   b.label.setBounds(290, 30, 15, 41); 
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        
    ....

    thanx
    Last edited by Fubarable; 03-23-2011 at 04:55 AM. Reason: Moderator Edit: code tags added

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

    Default

    You don't really want outside classes to fiddle directly with the fields of another class such as in your code where outside classes directly fiddle with the JLabels' bounds properties. What if later you represent your player in a different way and the setBounds makes no sense? Rather, I suggest that you create a separate Player class, one that knows its position, that has methods to get the position:

    Java Code:
    public Point getPosition() {
      // your code here
    }

    and a method to set the position

    Java Code:
    public void moveToPosition(Point p) {
      // your code here
    }
    and then inside of the Player class you change the position of your JLabel with its image, but you hide this detail from outside classes.

  7. #7
    Join Date
    Feb 2011
    Posts
    14
    Rep Power
    0

Similar Threads

  1. adding a actionListener but not using inner class
    By hariza in forum AWT / Swing
    Replies: 2
    Last Post: 10-08-2010, 08:24 AM
  2. Adding 2 Implements to the class
    By jboy in forum New To Java
    Replies: 2
    Last Post: 10-23-2009, 06:19 AM
  3. can we change label from action class?
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 04-28-2009, 03:12 PM
  4. Problems adding an external class
    By jan2321 in forum Eclipse
    Replies: 2
    Last Post: 11-06-2008, 11:34 AM
  5. Adding a new class to a jar file
    By Raul Menendez in forum New To Java
    Replies: 2
    Last Post: 08-21-2008, 05:52 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
  •