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

Thread: Java GridLayout - 2 columns

  1. #1
    Junior Member
    Join Date
    Nov 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java GridLayout - 2 columns

    Hi everyone,

    I'm trying to create a basic GUI for booking classes in a gym. I'm not very familiar with the grid layout and struggling with creating a 2 column grid.

    Here's what I'm trying to achieve: gym.jpg

    And here's my grid code so far:

    private void createPanel() {
     
        setLayout(new GridLayout(4, 1));
     
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        JPanel panel4 = new JPanel();
     
        //Panel 1: member ID field
        panel1.add(memberIDLabel);
        panel1.add(memberIDField);
     
        panel1.add(courseNameLabel);
        panel1.add(courseNameField);
     
        panel1.add(bookButton);
     
        //Panel 4: 'view courses' and 'view members' buttons
        panel4.add(coursesButton);
        panel4.add(bookingsButton);
     
        //Attaches panels to frame
        this.add(panel1);
        this.add(panel2);
        this.add(panel3);
        this.add(panel4);
    }

    Any guidance would be much appreciated. Many thanks.

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Java GridLayout - 2 columns

    Please post complete code here
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Nov 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java GridLayout - 2 columns

    import javax.swing.*;
     
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class GymGUI extends JFrame implements ActionListener {
     
    	private JLabel memberIDLabel, courseNameLabel;
    	private JTextField memberIDField, courseNameField;
    	private JButton bookButton, coursesButton, bookingsButton;
     
    	private static final int FRAME_WIDTH = 500;
    	private static final int FRAME_HEIGHT = 400;
     
    	public GymGUI() {
     
    		//Creates frame elements
    		createMemberIDField();
    		createCourseNameField();
    		createBookButton();
    		createCoursesButton();
    		createBookingsButton();
    		createPanel();
    		setSize(FRAME_WIDTH, FRAME_HEIGHT);
     
    		//Initialises frame properties
    		this.setTitle("Gym");
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setLocationRelativeTo(null);
    		this.setResizable(false);
    		this.setVisible(true);
     
    	}
     
    	//Creates member ID label and field
    	private void createMemberIDField() {
     
    		memberIDLabel = new JLabel("Member ID: ");
    		final int FIELD_WIDTH = 10;
    		memberIDField = new JTextField(FIELD_WIDTH);
    	}
     
    	//Creates course name label and field
    	private void createCourseNameField() {
     
    		courseNameLabel = new JLabel("Course name: ");
    		final int FIELD_WIDTH = 15;
    		courseNameField = new JTextField(FIELD_WIDTH);
     
    	}
     
    	//Creates 'confirm booking' button and attaches listener
    	private void createBookButton() {
     
    		bookButton = new JButton("Confirm Booking");
    		bookButton.addActionListener(this);
    	}
     
    	//Creates 'view courses' button and attaches listener
    	private void createCoursesButton() {
    		coursesButton = new JButton("View Courses");
    		coursesButton.addActionListener(this);
     
    	}
     
    	//Creates 'view bookings' button and attaches listener
    	private void createBookingsButton() {
     
    		bookingsButton = new JButton("View Bookings");
    		bookingsButton.addActionListener(this);
    	}
     
    	// Creates panels and attach labels, fields and buttons
    	private void createPanel() {
     
    		setLayout(new GridLayout(4, 1));
     
    		JPanel panel1 = new JPanel();
    		JPanel panel2 = new JPanel();
    		JPanel panel3 = new JPanel();
    		JPanel panel4 = new JPanel();
     
    		//Panel 1: member ID field
    		panel1.add(memberIDLabel);
    		panel1.add(memberIDField);
     
    		panel1.add(courseNameLabel);
    		panel1.add(courseNameField);
     
    		panel1.add(bookButton);
     
    		//Panel 4: 'view courses' and 'view members' buttons
    		panel4.add(coursesButton);
    		panel4.add(bookingsButton);
     
    		//Attaches panels to frame
    		this.add(panel1);
    		this.add(panel2);
    		this.add(panel3);
    		this.add(panel4);
    	}
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
    	}
     
    }

  4. #4
    Member
    Join Date
    Apr 2014
    Posts
    92
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Java GridLayout - 2 columns

    setLayout(new GridLayout(4, 1));
    This makes four rows and one column. If you want two columns instead, can you see which parameter needs to be changed?

Similar Threads

  1. Replies: 1
    Last Post: February 5th, 2014, 09:22 AM
  2. Replies: 2
    Last Post: July 9th, 2013, 09:51 PM
  3. Replies: 1
    Last Post: July 28th, 2012, 12:05 AM
  4. GridLayout help
    By sambar89 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 26th, 2011, 11:00 PM
  5. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM

Tags for this Thread