Results 1 to 3 of 3
  1. #1
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Rep Power

    Default set Components to GridbagLayout

    Hello all and Happy new Year!!

    I decide to make a new GUI just for fun and practise and i want to learn more about Layouts.. right know i am try to understand some staff about GridBagLayout.

    I want to make a GUI with this Layout :
    set Components to GridbagLayout-gui.png

    So..first i am a frame with borderlayout then i put a JMenuBar and bla bla bla and i create panel with GridLayout which i put on myFrame.add(panel1,BorderLayout.WEST) and also i make an another JPanel which i put of the Center of main Frame (myFrame.add(panel2,BorderLayout.Center))
    when i run the program i get this GUI :
    set Components to GridbagLayout-screenshot_1.png

    now the problem is that i can't put the JButton on the top of the gridLayout just to look like the first picture.. i Read about anchor and i try to
    gbc.anchor = GridBagConstraints.NORTH; but without results.. still the components are on the middle of the JPanel.. and i can't fix it.. any suggestions ?

    Here is my Code () :
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    public class ProgramFrame extends JFrame implements ActionListener{
    	JButton libraryButton;
    	JButton costumersButton;
    	LibraryPanel libraryPanel;
    	public ProgramFrame(){
    		setTitle("bla bla bla");
    		setLayout(new BorderLayout());
    	public void unit(){
    		//Create the LibraryPanel and MenuPanel and setLayouts and GridBackConstraints insets
    		libraryPanel = new LibraryPanel();
    		JPanel menuItemPanel = new JPanel();
    		menuItemPanel.setLayout(new GridBagLayout());
    		GridBagConstraints gbc = new GridBagConstraints();
    		gbc.anchor = GridBagConstraints.NORTHWEST;
    		gbc.insets = new Insets(2,2,2,2);
    		// Create Button for libraryPanel
    		libraryButton = new JButton();
    		costumersButton = new JButton();
    		// Set PreferredSize for JButtons in libraryPanel
    		libraryButton.setPreferredSize(new Dimension(100,50));
    		costumersButton.setPreferredSize(new Dimension(100,50));
    		// change the x and y of dridbackLayout to give some space between JButtons
    		gbc.anchor = GridBagConstraints.NORTH;
    		gbc.gridx = 0;
    		gbc.gridy = 0;
    		gbc.gridx = 0;
    		gbc.gridy = 2;
    		//Create a MenuBar and menuItems 
    		JMenuBar menuBar = new JMenuBar();
    		JMenu file = new JMenu("File");
    		JMenuItem exit = new JMenuItem("Exit");
    		// Add LibraryPanel to the Center of Frame , the MenuItemPanel on the West of JFrame and the MenuBar on North of JFrame
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		if(e.getSource() == libraryButton){
    		if(e.getSource() == costumersButton){
    	public static void main (String args []){
    		ProgramFrame myFrame = new ProgramFrame();
    and LibraryPanel is always a class which exteds JPanel nothing more..

    PS.. I can't remove the last Attached Images if any moderator can pls just remove it ;)
    Attached Thumbnails Attached Thumbnails set Components to GridbagLayout-screenshot_1.png  
    Last edited by ShadowWalker; 01-01-2014 at 04:28 PM.

  2. #2
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Rep Power

    Default Re: set Components to GridbagLayout

    You could make a JPanel with GridLayout with 5 or 6 rows and 1 column, add your buttons to that, then add the JPanel to the JFrame using BorderLayout.WEST, then add whatever that thing is thats taking up the rest of the JFrame and add it to BorderLayout.CENTER. Read up on GridLayout, you can adding padding in between your components with it as well. The only problem with this is that your buttons will only be as wide as the button with the longest text/label

  3. #3
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Rep Power

    Default Re: set Components to GridbagLayout

    Thx for your reply.. i understand what you are talking about ..but in that case i will have something like this if i have right.. : set Components to GridbagLayout-eee.png and i would like to have some space between buttons and to leave a space down from the Jbuttons..hmm anyway i will give it a try.. maybe if i make a gridLayout of 2 rows and 1 column and then on the first row i put onother JPanel with gridLayout too and put the JButton there ..maybe it will work.. but the point of all this is to learn more about Layouts.. and i would like to know if there are some Layouts to solve problems like this more easily..

Similar Threads

  1. GridBagLayout - forcing all components to stay on top
    By Serpenthelm in forum AWT / Swing
    Replies: 3
    Last Post: 01-05-2012, 07:52 PM
  2. Replies: 16
    Last Post: 11-17-2010, 02:02 PM
  3. Adding and removing components from a GridBagLayout
    By peterhabe in forum New To Java
    Replies: 4
    Last Post: 09-19-2010, 11:13 PM
  4. Arrange components in a GridBagLayout
    By ze snow in forum New To Java
    Replies: 1
    Last Post: 02-27-2010, 03:22 PM
  5. gridbaglayout: increase/decrease size of components.
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 08:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts