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 3 of 3

Thread: how to position gui components

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to position gui components

    hello everybody i have been learning java for the summer for next semester i am currently looking at gui i was wondering how you can position components such as JLabels and JTextFields where ever you want. here is a peice of code i have been working on

    import java.awt.FlowLayout;
     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
     
     
     
    public class gui extends JFrame{
    	private JTextField num1;
    	private JTextField num2;
    	//private JTextField num3;
     
    	private JLabel lnum1;
    	private JLabel lnum2;
    	//private JLabel lnum3;
     
    		public gui(){
    			super("sum program");
    			setLayout(new FlowLayout());
     
    			lnum1 = new JLabel("first number");
    			lnum1.setToolTipText("enter the first number here.");
    			num1 = new JTextField(10);
    			lnum1.setHorizontalTextPosition(SwingConstants.LEFT);
    			lnum1.setVerticalTextPosition(SwingConstants.BOTTOM);
     
     
    			lnum2 = new JLabel("second number");
    			lnum2.setToolTipText("enter the second number here.");
    			lnum2.setHorizontalTextPosition(SwingConstants.RIGHT);
    			lnum2.setVerticalTextPosition(SwingConstants.TOP);
    			num2 = new JTextField(10);
     
    			add(lnum1);
    			add(num1);
    			add(lnum2);
    			add(num2);
    		}
     
    }

    i know about flowlayout and i have hearded about gridlayout. but when i run this program both labels and textfields are in the top and center. what i want to do is position the first textfield and label in the bottom left hand corner and the second label and textfield in the top right hand corner. i know i sounds weird but i just need an example like that so i can understand how to do it.

    thanks in advance.


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: how to position gui components

    setHorizontalTextPosition and setVerticalTextPosition refer to the position of the text within the text component, not where the component displays.

    I find BorderLayout most convenient for placing components top, bottom, left, or right because the centre part of BorderLayout expands to take up all available space. Generally, the simplest way to use layouts is to combine them, nesting layouts within panels. In this case, I'd have a top (North) panel and a bottom (South) panel and use a BorderLayout within each to place the components to the right (East) and left (West) respectively. To make the label and text fields sit next to each other, I'd put them into their own sub-panels:
    public Gui() {
        super("sum program");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(400, 300);
        setLayout(new BorderLayout());
     
        lnum1 = new JLabel("first number");
        lnum1.setToolTipText("enter the first number here.");
        num1 = new JTextField(10);
     
        JPanel inputPanel1 = new JPanel();
        inputPanel1.add(lnum1);
        inputPanel1.add(num1);
     
        JPanel southPanel = new JPanel(new BorderLayout());
        southPanel.add(inputPanel1, BorderLayout.WEST);
        add(southPanel, BorderLayout.SOUTH);
     
        lnum2 = new JLabel("second number");
        lnum2.setToolTipText("enter the second number here.");
        num2 = new JTextField(10);
     
        JPanel inputPanel2 = new JPanel();
        inputPanel2.add(lnum2);
        inputPanel2.add(num2);
     
        JPanel northPanel = new JPanel(new BorderLayout());
        northPanel.add(inputPanel2, BorderLayout.EAST);
        add(northPanel, BorderLayout.NORTH);
     
        setVisible(true);
    }
    Please note that the Java naming conventions are that class names begin with an upper case letter, method and variable names begin with a lower case letter.
    Last edited by dlorde; June 11th, 2011 at 11:30 AM. Reason: oops - wrong way round...

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to position gui components

    thank for your help dlorde it was annoying me for a few days now, and also thanks for the tip on java naming convention, its good to stop bad habits early. thanks again.

Similar Threads

  1. How to get Mouse Position even if it is not within our application?
    By Freaky Chris in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 4th, 2012, 10:57 AM
  2. [SOLVED] last character position
    By nasi in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2010, 05:31 AM
  3. Incrementing every letter in a string that occupies an odd position.
    By haktheplanet in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2010, 04:45 AM
  4. The Frame to be Center Position
    By r12ki in forum AWT / Java Swing
    Replies: 3
    Last Post: October 1st, 2009, 10:36 AM
  5. Painting swing components to an arbitrary position?
    By ScummyChimp in forum AWT / Java Swing
    Replies: 1
    Last Post: September 1st, 2009, 11:06 PM