Results 1 to 4 of 4
  1. #1
    satanfx55 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default Help making a grid inside of a JPanel

    I thought the logical way to do this was with a GridLayout. However I have found that to not be the case. I need to make a grid of perfect squares that is also a square (10 x 10) or (50 x 50). When I use GridLayout, it decides to make all of my buttons rectangles. Is there a better way to do this?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class buttonGrid extends JFrame
    {
    	private JPanel buttonPanel, leftPanel, rightPanel, bottomPanel;
    	private JScrollPane buttonScroll, leftScroll, rightScroll, bottomScroll;
    	
    	public buttonGrid()
    	{
    		super("GridLayout Sucks");
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setLayout(new BorderLayout());
    		this.setSize(400, 400);
    		
    		leftPanel = new JPanel();
    		leftPanel.setLayout(new GridLayout(10, 1, 1, 1));
    		
    		for (int i = 0; i < 10; i++)
    		{
    			JButton button = new JButton("L" + i);
    			leftPanel.add(button, i);
    		}
    		
    		leftScroll = new JScrollPane(leftPanel);
    		leftScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    		
    		rightPanel = new JPanel();
    		rightPanel.setLayout(new GridLayout(10, 1, 1, 1));
    		
    		for (int i = 0; i < 10; i++)
    		{
    			JButton button = new JButton("R" + i);
    			rightPanel.add(button, i);
    		}
    		
    		rightScroll = new JScrollPane(rightPanel);
    		rightScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    		
    		buttonPanel = new JPanel();
    		buttonPanel.setLayout(new GridLayout(30, 30, 1, 1));
    		
    		for (int i = 0; i < 30; i++)
    		{
    			for (int j = 0; j < 30; j++)
    			{
    				JButton button = new JButton(j + "," + i);
    				buttonPanel.add(button);
    			}
    		}
    		
    		buttonScroll = new JScrollPane(buttonPanel);
    		buttonScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    		buttonScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    		
    		this.add(buttonScroll, BorderLayout.CENTER);
    		this.add(rightScroll, BorderLayout.EAST);
    		this.add(leftScroll, BorderLayout.WEST);
    		this.setVisible(true);
    	}
    	
    	public static void main(String[] args)
    	{
    		new buttonGrid();
    	}
    }

    Thanks for reading my post. I know its not the best code. I actually have an entire project and this is just an aspect of the project that I am trying to get right.

  2. #2
    Join Date
    May 2011
    Location
    Munich
    Posts
    15
    Rep Power
    0

    Default

    maybe you could try setPreferredSize(x,x) on the buttons?
    read my blog : www.blue-walrus.com

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    It's generally better practice to override getPreferredSize. This prevents client code from changing the desired behavior with another call to getPreferredSize.

    Typed here, may have typos or other errors...
    Java Code:
    public class SquareButton extends JButton {
    
      public SquareButton(String text) {
        super(text);
      }
    
      // other constructors as desired
    
      public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();
        if (d.width > d.height) {
          d.height = d.width;
        } else {
          d.width = d.height;
        }
        return d;
      }
    }
    db

  4. #4
    satanfx55 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Making a grid using a String.
    By josh2992 in forum New To Java
    Replies: 5
    Last Post: 05-10-2011, 02:11 PM
  2. Replies: 10
    Last Post: 01-12-2011, 07:16 PM
  3. JPanel inside Jtabbed isn't display
    By Ravanelly in forum Advanced Java
    Replies: 7
    Last Post: 06-08-2010, 10:19 PM
  4. How to add JFrame inside JPanel
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 8
    Last Post: 12-13-2009, 08:41 PM
  5. Making grid using pictures
    By racewithferrari in forum New To Java
    Replies: 1
    Last Post: 11-01-2009, 12:59 AM

Tags for this Thread

Posting Permissions

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