Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

setLocation problem

Thread view: 
Scott Vetter - 26 Nov 2003 16:03 GMT
   I'm a newbie to Java and am trying to figure out a small problem
with the setLocation statement in that nomatter what settings I use, it
text always appears at the same location.

The code is such:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class test extends JApplet
{
   JLabel text = new JLabel("This is the text");

   public void init()
   {
     Container con = getContentPane();
     con.setBackground(Color.yellow);
     con.setLayout(new FlowLayout());
     text.setLocation(100,250);
     con.add(text);
   }
}

   Any help would be appreciated...

TIA,

Scott
Thomas Weidenfeller - 26 Nov 2003 16:32 GMT
>     I'm a newbie to Java and am trying to figure out a small problem
> with the setLocation statement in that nomatter what settings I use, it
> text always appears at the same location.

Don't mess with setLocation at all. Fortunately, you use a layout
manager, and the layout manager decides about the location, that is his
job. Forget about the setLocation thing.  Instead, you time is best
spent by learning the layout managers. You could e.g. start here:

http://java.sun.com/docs/books/tutorial/uiswing/mini/layout.html

and here:

http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html

/Thomas
Scott Vetter - 26 Nov 2003 18:40 GMT
Thomas:

   Thank you for the response.  Agreed there are probably better
methods to do the things that need to be done, but for right now I have
to start using the simple stuff to understand.

Scott

>>    I'm a newbie to Java and am trying to figure out a small problem
>>with the setLocation statement in that nomatter what settings I use, it
[quoted text clipped - 12 lines]
>
> /Thomas
Andrew Thompson - 26 Nov 2003 20:31 GMT
> Thomas:
>
>     Thank you for the response.  Agreed there are probably better
> methods to do the things that need to be done, but for right now I have
> to start using the simple stuff to understand.

What you are doing is kludgy, rather than simple.

In order to understand where you are going wrong,
you _need_ to understand how java lays out GUI's.

The links Thomas provided point you in the
right direction.

[ Coming from someone who went from using
the 'hack' to which you referred for scrolling text,
to developing a _ScrollLayout_ for the purpose. ]

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
Xavier Tarrago - 27 Nov 2003 08:27 GMT
As it has been said, you should use a layout. But you can use absolute
positionning too:
see http://java.sun.com/docs/books/tutorial/uiswing/layout/none.html
Basically, you have to set a null layout (i-e no layout)

con.setLayout( null);

> Thomas:
>
[quoted text clipped - 20 lines]
> >
> > /Thomas
ak - 27 Nov 2003 08:24 GMT
Ok if you want to set location, first set layout manager to 'null'!
con.setLayout(null);
text.setLocation(100, 250);

>     I'm a newbie to Java and am trying to figure out a small problem
> with the setLocation statement in that nomatter what settings I use, it
[quoted text clipped - 25 lines]
>
> Scott
Scott Vetter - 28 Nov 2003 04:34 GMT
> Ok if you want to set location, first set layout manager to 'null'!
> con.setLayout(null);
> text.setLocation(100, 250);

   First I want to thank all that answered.  A bit more complex than
what I wanted but the ideas provided some pointers.

   The reason for my desire to use the setLocation method() was it is
what it covered(?) in class and wanted to keep within the class bounds
which covered (very slightly) the afore mentioned method.

   I did try the con.setLayout(null); but it didn't change a thing.
Now mind you I'm not try to cheat in class, but just trying to get the
darn thing to work...  Very frustrating.

Thanks,

Scott
Andrew Thompson - 28 Nov 2003 05:39 GMT
...
> > Ok if you want to set location, first set layout manager to 'null'!
> > con.setLayout(null);
> > text.setLocation(100, 250);
> >
>     I did try the con.setLayout(null); but it didn't change a thing.

ak's advice should have worked.  Post your entire
code (as it is with current changes) and we may be
able to spot where you are going wrong.

[ I have said my piece about layout manager's,
so unless I forget that I did that, you can be
spared further mention of it.. ;-) ]

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
Scott Vetter - 02 Dec 2003 14:55 GMT
   The file is attached.

Thanks...

Scott

> ...
>
[quoted text clipped - 17 lines]
> * http://www.1point1C.org/ 1.1C - Superluminal!
> * http://www.AThompson.info/andrew/ personal site
ak - 04 Dec 2003 00:24 GMT
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class sltest extends JApplet
                   implements ActionListener
{
 JLabel hdg = new JLabel("HEADING");
 JLabel txt = new JLabel("Label");
 JButton pb = new JButton("press");

 public void init()
 {
   Container con = getContentPane();
   con.setLayout(null);
   con.add(hdg);
   hdg.setBounds(10, 10, 100, 30);
   con.add(txt);
   txt.setBounds(20,20, 100, 30);
   con.add(pb);
   pb.setBounds(30, 30, 100, 30);
   pb.addActionListener(this);
 }
public void actionPerformed(ActionEvent e)
{
  Object source = e.getSource();
  if (source == pb)
     System.out.println("button pressed");
}
}

--

____________

http://reader.imagero.com the best java image reader.

>     The file is attached.
>
[quoted text clipped - 23 lines]
> > * http://www.1point1C.org/ 1.1C - Superluminal!
> > * http://www.AThompson.info/andrew/ personal site

----------------------------------------------------------------------------
----

> import javax.swing.*;
> import java.awt.*;
[quoted text clipped - 26 lines]
>  }
> }
Andrew Thompson - 04 Dec 2003 00:42 GMT
> import javax.swing.*;
<snip solution>

Good work ak.

Could I ask you though, to perhaps make more
use of the 'delete key', so you are not reposting
entire previous conversations?

That would be most excellent.   :-)

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.