Results 1 to 6 of 6
  1. #1
    batia is offline Member
    Join Date
    Feb 2011
    Location
    Lithuania
    Posts
    16
    Rep Power
    0

    Default GroupLayout problem

    Hello,

    What do i do wrong,

    My layout manager does not work, or i left some mistakes.

    I want to make my buttons on jpanel look like this:

    o o o o
    o o o
    o o
    o

    Thanks in advance.
    Here is the code:

    Java Code:
    import static javax.swing.GroupLayout.Alignment.BASELINE;
    import static javax.swing.GroupLayout.Alignment.LEADING;
    
    import javax.swing.GroupLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class testPanel1 extends JPanel{
    	testPanel1(){
    		super();
    		JButton [] buttons = new JButton[10];
    		for (int i = 0; i< buttons.length; i ++){
    			buttons[i] = new JButton("Button " + (i + 1));
    		}
    		GroupLayout layout = new GroupLayout(this);		
    		layout.setAutoCreateGaps(true);
            layout.setAutoCreateContainerGaps(true);
            
            layout.setHorizontalGroup(layout.createSequentialGroup()
                    .addGroup(layout.createSequentialGroup()
                    	.addComponent(buttons[0])
                        .addComponent(buttons[1])
                        .addComponent(buttons[2])
                        .addComponent(buttons[3]))
                    .addGroup(layout.createSequentialGroup()
                    	.addComponent(buttons[4])
                        .addComponent(buttons[5])
                        .addComponent(buttons[6]))
                    .addGroup(layout.createSequentialGroup()
                    	.addComponent(buttons[7])
                        .addComponent(buttons[8]))
                    .addComponent(buttons[9])
             );
            layout.setVerticalGroup(layout.createSequentialGroup()
            		.addGroup(layout.createSequentialGroup()
            			.addComponent(buttons[0])
                    	.addComponent(buttons[1])
                    	.addComponent(buttons[2])
                    	.addComponent(buttons[3]))
                    .addGroup(layout.createSequentialGroup()
                    	.addComponent(buttons[4])
                    	.addComponent(buttons[5])
                    	.addComponent(buttons[6]))
                    .addGroup(layout.createSequentialGroup()
                    	.addComponent(buttons[7])
                    	.addComponent(buttons[8]))
                    .addComponent(buttons[9])
             );
            
    	}
    	public static void main(String args[]){
    		JFrame frame1 = new JFrame ("Test");
    		frame1.setContentPane(new testPanel1());
    		frame1.setVisible(true);
    		frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		frame1.setSize(1000, 1000);
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For my money, for ease I'd use a GridLayout and add JButtons where I want the buttons and JLabels where I want a non-button placeholder.

  3. #3
    batia is offline Member
    Join Date
    Feb 2011
    Location
    Lithuania
    Posts
    16
    Rep Power
    0

    Default

    Fubarable,

    Thanks for your answer.

    In GridLayout, how to add null component to go to the "next line"?

    In my example
    o o o o
    o o o
    o o
    o

    algorithm could look like:
    setLayout( new GridLayout(4,4) );

    add(button); add(button); add(button); add(button);
    add(button); add(button); add(button); nextLine
    add(button); add(button); nextLine
    add(button);

    I do not find any methods how to do that?

    Or do i have to create many panels and add then on one panel using grid layout?
    Maybe in this case its more simple to use GroupLayout?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    batia is offline Member
    Join Date
    Feb 2011
    Location
    Lithuania
    Posts
    16
    Rep Power
    0

    Default

    Thought about that after posting.

    One more question.
    GridLayout makes surface look like "chess Table".

    Is it possible to, merge cells in this layout?

  6. #6
    batia is offline Member
    Join Date
    Feb 2011
    Location
    Lithuania
    Posts
    16
    Rep Power
    0

    Default

    Ok, i found how to do it using GroupLayout.
    Besides, i also added spanning.
    So GroupLayout is worthy after all.

    If it will be useful i add code.

    Java Code:
    import static javax.swing.GroupLayout.Alignment.LEADING;
    
    import javax.swing.GroupLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    
    
    public class testPanel1 extends JPanel{
    	testPanel1(){
    		super();
    		JButton [] buttons = new JButton[11];
    		for (int i = 0; i< buttons.length; i ++){
    			buttons[i] = new JButton("Button " + (i + 1));
    		}
    		buttons[10].setText("Cell span");
    
    		GroupLayout layout = new GroupLayout(this);
    		setLayout(layout);
    		layout.setAutoCreateGaps(true);
            layout.setAutoCreateContainerGaps(true);
            
            layout.setHorizontalGroup(
            		layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(LEADING)
                    	.addComponent(buttons[0])
                        .addComponent(buttons[1])
                        .addComponent(buttons[2])
                        .addComponent(buttons[3]))
                    .addGroup(layout.createParallelGroup(LEADING)
                    	.addGroup(layout.createSequentialGroup()
                    		.addGroup(layout.createParallelGroup(LEADING)
    		                	.addComponent(buttons[4])
    		                    .addComponent(buttons[5])
    		                    .addComponent(buttons[6]))
    		                .addGroup(layout.createParallelGroup(LEADING)
    		                	.addComponent(buttons[7])
    		                	.addComponent(buttons[8]))
    		             )
    		             .addComponent(buttons[10],170,170,170))
                    .addComponent(buttons[9])
             );
            
            layout.linkSize(SwingConstants.HORIZONTAL,buttons[0],buttons[1]);
            layout.setVerticalGroup(
            	layout.createSequentialGroup()
        		   	.addGroup(
        		   		layout.createParallelGroup(LEADING)
    	    		   		.addComponent(buttons[0])
    	                	.addComponent(buttons[4])
    	                	.addComponent(buttons[7])
    	                	.addComponent(buttons[9])
    	                )
                    .addGroup(layout.createParallelGroup(LEADING)
                    	.addComponent(buttons[1])
                    	.addComponent(buttons[5])
                    	.addComponent(buttons[8]))
                    .addGroup(layout.createParallelGroup(LEADING)
                    	.addComponent(buttons[2])
                    	.addComponent(buttons[6]))
                    .addComponent(buttons[3])
                    .addComponent(buttons[10],50,50,50));
            
    	}
    	public static void main(String args[]){
    		JFrame frame1 = new JFrame ("Test");
    		frame1.setContentPane(new testPanel1());
    		frame1.setVisible(true);
    		frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		frame1.setSize(400, 400);
    	}
    }

Similar Threads

  1. Problem in GroupLayout ?
    By makpandian in forum New To Java
    Replies: 10
    Last Post: 04-18-2011, 08:26 AM
  2. GroupLayout
    By Itun in forum AWT / Swing
    Replies: 3
    Last Post: 03-24-2011, 05:19 PM
  3. Anyone know how GroupLayout works?
    By ProgrammingPup in forum Advanced Java
    Replies: 5
    Last Post: 12-01-2009, 11:12 PM
  4. Doubt with GroupLayout
    By emylyano3 in forum AWT / Swing
    Replies: 1
    Last Post: 06-28-2009, 02:16 AM
  5. "Problem With GroupLayout", Please Help !
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-04-2008, 10:29 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
  •