The Artima Developer Community
Sponsored Link

Java Answers Forum
setLocation()

8 replies on 1 page. Most recent reply: May 3, 2003 1:18 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
Thang Nguyen

Posts: 18
Nickname: thang
Registered: Apr, 2002

setLocation() Posted: May 2, 2003 12:46 PM
Reply to this message Reply
Advertisement
I am trying to set location for the lable but it does not work! Why, please!

Thanks

import java.applet.*;
import java.awt.*;
public class Applet1 extends Applet  
{   
   //Create a label   
   Label lblTitle = new Label("Hello");   
   //Create font   
   Font fntHeader = new Font("Arial", Font.BOLD, 24);   
   public void init() {     
      lblTitle.setText("Bonjour");      
      lblTitle.setFont(fntHeader);      
      lblTitle.setLocation(10, 10);      //<--- :(
      add(lblTitle);       
   }  
}


Erik Price

Posts: 39
Nickname: erikprice
Registered: Mar, 2003

Re: setLocation() Posted: May 2, 2003 12:58 PM
Reply to this message Reply
Try different combinations of the arguments to the setLocation method, such as
label.setLocation(-10, -10);
label.setLocation(-10, 10);
label.setLocation(10, -10);
label.setLocation(10, 10);

See what happens. If this is of no help, perhaps you can do a better job of explaining (1) your desired outcome, and (2) what happens with the code as shown.

Thang Nguyen

Posts: 18
Nickname: thang
Registered: Apr, 2002

Re: setLocation() Posted: May 2, 2003 1:43 PM
Reply to this message Reply
Sorry for my vague question.

I tried the combination as suggested but it does the same thing - do nothing! The label always appears at the center of the applet window.

I tried this as well: setLayout( null ); at the beginning of init() then everthing disappears.

Thanks. (sorry for my bad English - But I tried my best :) )

Thang Nguyen

Posts: 18
Nickname: thang
Registered: Apr, 2002

Re: setLocation() Posted: May 2, 2003 2:12 PM
Reply to this message Reply
Here is my code again:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
 
public class Applet1 extends Applet implements ActionListener
{   
   //Create a label   
   Label lblTitle = new Label();
   Label lblResultFound = new Label();
   //Create font   
   Font fntHeader = new Font("Arial", Font.BOLD, 16);
    //Create textfield
   TextField txtQuery = new TextField(20); 
    //Create button  
   Button btnFind = new Button("Find");
 
   public void init() { 
   //   setLayout(null);
      lblTitle.setText("Glossary of: ");      
      lblTitle.setFont(fntHeader);
      lblTitle.setLocation(2, 2); //it always appears at the default place (center of applet window)
              
      txtQuery.requestFocus();
 
      add(lblTitle);     
      add(txtQuery);
      add(btnFind);
      add(lblResultFound);
 
      btnFind.addActionListener(this);
   }
   
   public void actionPerformed(ActionEvent e){
     lblResultFound.setText("Result found: "); 
     lblResultFound.setLocation(2, 20); // it moves to where expected but the text is truncated :(
   }
}
 

Erik Price

Posts: 39
Nickname: erikprice
Registered: Mar, 2003

Re: setLocation() Posted: May 2, 2003 2:14 PM
Reply to this message Reply
> Sorry for my vague question.
>
> I tried the combination as suggested but it does the same
> thing - do nothing! The label always appears at the center
> of the applet window.

I read the JavaDoc for and it looks like the setLocation method sets the location of the Component relative to the component's parent. I think this means that your Label must be within another component, but in your example there is no parent Component (Applet is not a component). So perhaps this is why it doesn't move anywhere?

Erik Price

Posts: 39
Nickname: erikprice
Registered: Mar, 2003

Re: setLocation() Posted: May 2, 2003 2:16 PM
Reply to this message Reply
> Here is my code again:
>
...
 
> public void init() {
> //   setLayout(null);
> lblTitle.setText("Glossary of: ");
> lblTitle.setFont(fntHeader);
> lblTitle.setLocation(2, 2); //it always appears at
> rs at the default place (center of applet window)
> 
> txtQuery.requestFocus();
> 
> add(lblTitle);
> add(txtQuery);
> add(btnFind);
> add(lblResultFound);
> 


Try moving the order around:

this.add(lblTitle);
lblTitle.setLocation(2, 2);


Not sure if that will do anything, but give it a try.

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: setLocation() Posted: May 2, 2003 7:14 PM
Reply to this message Reply
The reason for not being able to position the components is that applets by default use the FlowLayout manager (i think) for laying out the components. In your init method, set the layout to null :

setLayout(null);

your positioning will work then.

Thang Nguyen

Posts: 18
Nickname: thang
Registered: Apr, 2002

Re: setLocation() Posted: May 2, 2003 8:33 PM
Reply to this message Reply
I did try so Singh M, but nothing appears if uncomment the setLayout(null) line.

I tried to change the order around but it did not work either. It is wierd that if I used the actionPerformed() to change the location then it works but it truncates the text in the label!

Thanks

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: setLocation() Posted: May 3, 2003 1:18 PM
Reply to this message Reply
change your actionPerformed method
   public void actionPerformed(ActionEvent e){
 
     lblResultFound.setText("Result found: "); 
 
       //lblResultFound.setLocation(2, 20); // it moves to where expected but the text is truncated :(
     validate();
   }


validate will cause the applet layout to do it again. Since the length of your text label has grown, invoking the layout manager again will allow it all to be seen.

The line lblResultFound.setLocation(2, 20); is useless in a flow layout. The layout manager will just move it again and resize everything to its preferred size, unless you set the preferred size to your own value.

I think all you really wanted was to see the label when needed.

Also check the width and height in the html code for the applet and make sure it is big enough.

<html>
<body>
<applet code="Applet1.class" width="800" height="200"></applet>
</body>
</html>


and use shift enter when you reload the html web page to make sure it loads the most current Applet1.class file and not one in the web browser cache.

Flat View: This topic has 8 replies on 1 page
Topic: Read from file Previous Topic   Next Topic Topic: >> operator and 0xff

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use