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.


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

Results 1 to 5 of 5

Thread: Position TextBox Under Label In Panel

  1. #1
    Join Date
    May 2012
    Thanked 0 Times in 0 Posts

    Default Position TextBox Under Label In Panel

    Hi - I am attempting to dump the GUI editor and code everything by hand - big task for me! I am setting up my JForm with 4 panels, 3 to hold my data and a 4th as a "parent" that will house everything before adding to the JForm.

    Now my issue is, in the top most form, I have the positioning set to center for both the label and the text box and they appear on the same line. I want the text area to appear on the line BELOW the label and fill about half of that row.

    How should this be altered so that takes place?

    import javax.swing.*;
    import java.awt.*;
    public class Data {
        public static void main(String args[]) {
            JFrame frMain;
            JPanel pnlName;
            JPanel pnlCharacterSelect;
            JPanel pnlButtonHolder;
            JLabel lblCharaterSelect;
            JTextField txtName;
            JButton btnStartGame;
            JLabel lblWelcome;
            frMain = new JFrame();
            pnlName = new JPanel();
            pnlCharacterSelect = new JPanel();
            pnlButtonHolder = new JPanel();
            pnlName.setMaximumSize(( new Dimension( 1000, 100)));
            lblWelcome = new JLabel("Input Player Name:", SwingConstants.CENTER);
            txtName = new JTextField(SwingConstants.CENTER);
            pnlCharacterSelect.setMaximumSize(( new Dimension( 1000, 600)));
            pnlButtonHolder.setMaximumSize(( new Dimension( 1000, 50)));
            JPanel holder = new JPanel();
            holder.setLayout(new BoxLayout(holder, BoxLayout.Y_AXIS));
            frMain.add(holder, BorderLayout.CENTER);
    Last edited by jo15765; January 13th, 2019 at 05:27 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,710 Times in 2,660 Posts

    Default Re: Position TextBox Under Label In Panel

    The posted code is missing the import statements, the class definition and the definitions for all the variables.
    It is not possible to compile and test the code without them.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Join Date
    May 2012
    Thanked 0 Times in 0 Posts

    Default Re: Position TextBox Under Label In Panel

    Quote Originally Posted by Norm View Post
    The posted code is missing the import statements, the class definition and the definitions for all the variables.
    It is not possible to compile and test the code without them.
    Updated OP to include imports and class information.

  4. #4
    Join Date
    Sep 2018
    My Mood
    Thanked 38 Times in 36 Posts

    Default Re: Position TextBox Under Label In Panel

    Ok, normally I don't do this so think of it as a tutorial. Explaining layouts and such can be complicated and takes practice (which is why you need to read the API and try out different things. Also, check out the tutorials in my signature below. It contains pretty good examples on layout managers etc. It is self contained and should compile and execute. Notice also that if you decrease the width of the inner panels, at some point they will no longer align properly. This is the nature of the flowout layout manager.

    What you see below is a Frame, a main panel, and sub panels within the main panel. The first subpanel groups the label and text field together. This is a common technique. I also included a method to illustrate how you can create special panels using a method and specific arguments. The borders are shown to delineate the panels. Also, note that some layout managers ignore preferred sizes and other features. It depends on what layout you choose. Like I said you need to read the API, and the tutorials and experiment. But I think you may agree that the code below is easier to read than the "garbage" produced by the GUI designers.


    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.border.LineBorder;
    public class GuiDemo {
       JFrame frame		= new JFrame();	// the containing frame.
       JPanel mainPanel	= new JPanel();	// the panel,where everything else goes.
       public static void main(String args[]) {
    	  new GuiDemo().start(); // get out of static context
       public void start() {
    	  JLabel myLabel = new JLabel("Please enter your data below");
    	  JPanel myField = new JPanel(); // a subpanel to group the label and text
    	  JTextField myText = new JTextField(20); // 20 columns
    	  myField.setPreferredSize(new Dimension(300, 60));
    	  myField.setLayout(new FlowLayout());
    	  myField.setBorder(new LineBorder(Color.RED, 3));
    	  mainPanel.setPreferredSize(new Dimension(400, 400)); // arbitrary size for
    	                                                       // demo only
    	  mainPanel.add(createPanel(300, 100, true));
    	  mainPanel.add(createPanel(300, 150, true));
    	  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // check the API
    	  frame.pack(); // invoke the layout components and resize if necessary
    	  frame.setLocationRelativeTo(null); // center frame on screen
       public JPanel createPanel(int width, int height, boolean withBorder) {
    	  JPanel panel = new JPanel();
    	  panel.setPreferredSize(new Dimension(width, height));
    	  if (withBorder) {
    		 panel.setBorder(new LineBorder(Color.BLUE, 3));
    	  return panel;

    EDIT: Note that I use constants above (e.g. 300, 60, 150). It is best to provide these names because it helps document code and if you need to change a common value, you only need to do it in one place. Usually like

    static int SUB_PANEL_WIDTH = 300;

  5. The Following User Says Thank You to jim829 For This Useful Post:

    jo15765 (January 13th, 2019)

  6. #5
    Join Date
    May 2012
    Thanked 0 Times in 0 Posts

    Default Re: Position TextBox Under Label In Panel

    @Jim829 - I greatly appreciate this!! I like your approach of creating a method to createPanel - and your syntax properly aligns the label above the textbox. I had spent a great deal of time googling and found this solution which is close, but not exactly what I was after for the label and text box alignment.

    I'll def follow the tuts in your sig! Thank you so much again!

            pnlName.setMaximumSize(( new Dimension( 1000, 100)));
            pnlName.setLayout(new BorderLayout());
            pnlName.setBorder(BorderFactory.createEmptyBorder(5, 100, 5, 100));
            JLabel lblWelcome = new JLabel("Input Player Name:");
            JTextField txtName = new JTextField();

Similar Threads

  1. Replies: 6
    Last Post: October 2nd, 2014, 08:12 AM
  2. Embedding an image in textbox
    By Skywola in forum Java Theory & Questions
    Replies: 4
    Last Post: March 4th, 2014, 06:53 PM
  3. Replies: 0
    Last Post: March 8th, 2011, 07:28 AM
  4. TextBox
    By tryingtoJava in forum AWT / Java Swing
    Replies: 3
    Last Post: October 7th, 2009, 10:05 AM
  5. make textbox in canvas
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: October 6th, 2009, 07:10 AM