Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Positioning elements. Is it possible without layouts?

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    21
    My Mood
    Sleepy
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Positioning elements. Is it possible without layouts?

    Hi all,

    I'm still trying to understand the logic of Java, and it feels like I'm getting stuck more and more

    Right now I wanna create a JFrame and add just one button to it. If I use FlowLayout the button is centered by default, but I want it to be located at x:10 and y:10.

    package graphical;
     
    import javax.swing.*;
    import java.awt.*;
     
    public class GUI extends JFrame
    {
    	private JButton button;
     
    	public GUI() 
    	{
    		super("JAVA");
    		setLayout(new FlowLayout());
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setSize(640, 480);
    		this.setVisible(true);
     
    		button = new JButton("some button");
    		button.setLocation(10, 10); // ???
    		this.add(button);
     
    	}
    }

    I've googled out several ways to create custom layouts but they all seem to be too large.
    As I'm mostly an AS3 coder, I'd normally do this by just setting the button's x and y values like so:
    button.x = 10;
    button.y = 10;

    And this approach would work fine in ActionScript 3, but doesn't seem to work in Java.

    Is there a simple way to do the same in Java?


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Positioning elements. Is it possible without layouts?

    Quote Originally Posted by goodguy View Post
    Hi all,

    I'm still trying to understand the logic of Java, and it feels like I'm getting stuck more and more

    Right now I wanna create a JFrame and add just one button to it. If I use FlowLayout the button is centered by default, but I want it to be located at x:10 and y:10.

    package graphical;
     
    import javax.swing.*;
    import java.awt.*;
     
    public class GUI extends JFrame
    {
    	private JButton button;
     
    	public GUI() 
    	{
    		super("JAVA");
    		setLayout(new FlowLayout());
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setSize(640, 480);
    		this.setVisible(true);
     
    		button = new JButton("some button");
    		button.setLocation(10, 10); // ???
    		this.add(button);
     
    	}
    }

    I've googled out several ways to create custom layouts but they all seem to be too large.
    As I'm mostly an AS3 coder, I'd normally do this by just setting the button's x and y values like so:
    button.x = 10;
    button.y = 10;

    And this approach would work fine in ActionScript 3, but doesn't seem to work in Java.

    Is there a simple way to do the same in Java?
    If it's the same class, you don't have to this.methodName(), just call it like methodName().

    Layouts are a royal pain!

    I've fiddled with them many times.

    For JButton, there's also a setBounds() method that should setLocation(). I've no clue why it doesn't just move it to that location with the setLocation().
    Last edited by javapenguin; January 21st, 2011 at 01:55 PM.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Positioning elements. Is it possible without layouts?

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. The Following User Says Thank You to KevinWorkman For This Useful Post:

    javapenguin (January 21st, 2011)

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Positioning elements. Is it possible without layouts?

    Yes, but how come setLocation(x,y) never worked quite how I wanted it to for me?

    What's setLocation() do?

    Do you have to use Insets, whatever those are, like in the example, or can you just plug in numbers?

    I can find what in theory works, but it never got to work yet.

  6. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Positioning elements. Is it possible without layouts?

    Quote Originally Posted by javapenguin View Post
    If it's the same class, you don't have to this.methodName(), just call it like methodName().
    But using this.methodName() isn't hurting anything, and is often used to make the code more readable.

    Quote Originally Posted by javapenguin View Post
    Layouts are a royal pain!
    Not if you have a basic understanding of how they work, which a quick read-through of the tutorials will give you. They make otherwise complicated things, like resizing, repositioning, changing look and feel, etc very trivial. Steering an amateur programmer away from using layouts is not helpful.

    Quote Originally Posted by javapenguin View Post
    The JComponent class has a setAllignmentX() and setAllignmentY() methods that set allignment for that JComponent.
    Okay. And how do you think that will help the OP? What are you actually suggesting?

    Quote Originally Posted by javapenguin View Post
    For JButton, there's also a setBounds() method that should setLocation(). I've no clue why it doesn't just move it to that location with the setLocation().
    Because that's not how it works. If you have no clue, then why bother adding your input? Throw together a simple program that uses the method in question. Play around with it an test your assumptions before simply spewing out assumptions.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Positioning elements. Is it possible without layouts?

    Quote Originally Posted by javapenguin View Post
    Yes, but how come setLocation(x,y) never worked quite how I wanted it to for me?
    Then your assumptions are wrong. Write a basic program that plays with it.

    Quote Originally Posted by javapenguin View Post
    What's setLocation() do?
    What does the API say it does? What do the tutorials say?

    Quote Originally Posted by javapenguin View Post
    Do you have to use Insets, whatever those are, like in the example, or can you just plug in numbers?
    How hard would it have been to look up what insets are before posting this? How hard would it have been to write a simple program that tested them?

    Quote Originally Posted by javapenguin View Post
    I can find what in theory works, but it never got to work yet.
    That's because you don't test your assumptions. Write some simple test programs that play with these things. Stop hijacking this thread.

    I'm irritated. Can you tell?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Positioning elements. Is it possible without layouts?

    I tried to fix this wronging awhile ago.

    I have attached a .txt file of an Object I made that lets you position objects based on Coordinate, centered, offsetting, pointers to other elements, arrays (1D and 2D) of components, ect.

    I also attached a test program that I don't think uses all the methods (it is an old test program). Tell me if you need help using it, I'll be happy to help. It has made GUI building about a thousand times easier for me. You probably cannot use this with a GUI builder, I imagine things will blow up.

    Save them as .java files and play with the layout manager. It is very simple once you get the hang of it.
    Attached Files Attached Files
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Transferring elements between arraylists
    By KipTheFury in forum Collections and Generics
    Replies: 6
    Last Post: August 23rd, 2010, 02:13 PM
  2. Help with Arrays - Counting elements
    By ShakeyJakey in forum Collections and Generics
    Replies: 7
    Last Post: August 8th, 2010, 04:09 PM
  3. The positioning and alignment of the text on the paper to be printed
    By java_fledgeling in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 8th, 2010, 08:54 PM
  4. Need help understanding GUI screen layouts and labels
    By Bill_H in forum AWT / Java Swing
    Replies: 3
    Last Post: December 2nd, 2009, 11:50 PM
  5. Content positioning on the screen
    By Drakenmul in forum AWT / Java Swing
    Replies: 1
    Last Post: July 27th, 2009, 09:02 AM