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

    Default Help with resizing Buttons in a grid

    Ok so this is code that I used in a previous question. Now my goal is to resize the buttons in the grid (effectively zooming in and zooming out) but I am running into three issues. The first issue is that the buttons dont actually resize until I maximize the JFrame (either by double clicking the title bar or by clicking the maximize button). The second issue is that the smaller zoom level doesn't hold the square shape when maximized the first time but when you resize the window they are made into squares like they are supposed to be. The third issue is that when I resize (zoom out) to large, I cannot zoom back in (making the objects any smaller). I have put in debug code but it just simply wont resize the buttons back down to either "normal" or "small" once it goes to large. It will go back and forth between "small" and "normal" without problems (other than the two i mentioned). Please help!

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class buttonGrid extends JFrame implements ActionListener
    {
    	private static final int NUM = 30;
    	
    	private JMenuBar menuBar;
    	private JMenu viewMenu;
    	private JMenuItem smallItem, normalItem, largeItem;
    	private JPanel buttonPanel, leftPanel, rightPanel, bottomPanel;
    	private JScrollPane buttonScroll, leftScroll, rightScroll, bottomScroll;
    	private SquareButton[][] buttons = new SquareButton[NUM][NUM];
    	
    	public buttonGrid()
    	{
    		super("GridLayout Sucks");
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setLayout(new BorderLayout());
    		this.setSize(400, 400);
    		
    		constructMenuBar();
    		
    		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(NUM, NUM, 1, 1));
    		
    		for (int i = 0; i < NUM; i++)
    		{
    			for (int j = 0; j < NUM; j++)
    			{
    				buttons[j][i] = new SquareButton(j + "," + i);
    				buttons[j][i].setPreferredSize(new Dimension(50, 50));
    				buttonPanel.add(buttons[j][i]);
    			}
    		}
    		
    		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 void constructMenuBar()
    	{
    		menuBar = new JMenuBar();
    		
    		viewMenu = new JMenu("View");
    		smallItem = new JMenuItem("Small");
    		smallItem.addActionListener(this);
    		normalItem = new JMenuItem("Normal");
    		normalItem.addActionListener(this);
    		largeItem = new JMenuItem("Large");
    		largeItem.addActionListener(this);
    		
    		viewMenu.add(smallItem);
    		viewMenu.add(normalItem);
    		viewMenu.add(largeItem);
    		
    		menuBar.add(viewMenu);
    		this.setJMenuBar(menuBar);
    	}
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		if (e.getActionCommand().equals("Small"))
    		{
    			Dimension d = new Dimension(20, 20);
    			if (! (buttons[0][0].getPreferredSize().equals(d)))
    			{
    				for (int i = 0; i < NUM; i++)
    				{
    					for (int j = 0; j < NUM; j++)
    					{
    						buttons[j][i].setPreferredSize(d);
    					}
    				}
    			}
    		}
    		else if (e.getActionCommand().equals("Normal"))
    		{
    			Dimension d = new Dimension(50, 50);
    			if (! (buttons[0][0].getPreferredSize().equals(d)))
    			{
    				for (int i = 0; i < NUM; i++)
    				{
    					for (int j = 0; j < NUM; j++)
    					{
    						buttons[j][i].setPreferredSize(d);
    					}
    				}
    			}
    		}
    		else if (e.getActionCommand().equals("Large"))
    		{
    			Dimension d = new Dimension(100, 100);
    			if (! (buttons[0][0].getPreferredSize().equals(d)))
    			{
    				for (int i = 0; i < NUM; i++)
    				{
    					for (int j = 0; j < NUM; j++)
    					{
    						buttons[j][i].setPreferredSize(d);
    					}
    				}
    			}
    		}
    	}
    	
    	public static void main(String[] args)
    	{
    		new buttonGrid();
    	}
    }
    
    class SquareButton extends JButton
    {
    	public SquareButton(String t)
    	{
    		super(t);
    	}
    	
    	public Dimension getPreferredSize() 
    	{
    		Dimension d = super.getPreferredSize();
    		if (d.width > d.height) 
    		{
    			d.height = d.width;
    		} 
    		else 
    		{
    			d.width = d.height;
    		}
    		return d;
    	}
    }

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

    Default Figured it out

    The solution to this problem is to switch from that ghastly GridLayout to GridBagLayout and the fix for the issue where the panel doesnt update is to call the updateUI() method in the JPanel class when changing the preferred size of the buttons.

    Thanks for reading my post and I hope this helps someone.

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

    Default

    Quote Originally Posted by satanfx55 View Post
    The solution to this problem is to switch from that ghastly GridLayout to GridBagLayout and the fix for the issue where the panel doesnt update is to call the updateUI() method in the JPanel class when changing the preferred size of the buttons.

    Thanks for reading my post and I hope this helps someone.
    There is never a reason to call updateUI(). Perhaps you meant to call revalidate()?

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    The second issue is that the smaller zoom level doesn't hold the square shape
    The default behaviour for a scrollpane is that components are resized to fill the entire area of the viewport when the component is smaller than the viewport. Therefore your button panel is resized and the rules of the GridLayout override the preferred size of your button components.

    Simple solution is to use a "wrapper" panel so the wrapper panel gets resized and your button panel stays at its preferred size:

    Java Code:
    JPanel wrapper = new JPanel( new FlowLayout(FlowLayout.LEFT, 0, 0) );
    wrapper.add( buttonPanel );
    buttonScroll = new JScrollPane(wrapper);
    //buttonScroll = new JScrollPane(buttonPanel);
    Another option, although more complicated, is to implement the Scrollable interface on your button panel so that the panel is always displayed at its preferred size. To make life easier you can use the Scrollable Panel Java Tips Weblog. The default is to use NONE for the size hint which is what you want. So in this case the only change to your code would be:

    Java Code:
    //buttonPanel = new JPanel();
    buttonPanel = new ScrollablePanel();

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

    Default revalidate or updateUI

    Quote Originally Posted by Fubarable View Post
    There is never a reason to call updateUI(). Perhaps you meant to call revalidate()?
    Both revalidate() and updateUI() have the same effect in terms of my program. They both resize the buttons immediately and do not require me to maximize the window for the resize to take place. What is the difference between revalidate() and updateUI()?

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

    Default Two ways to solve the problem

    Quote Originally Posted by camickr View Post
    Simple solution is to use a "wrapper" panel so the wrapper panel gets resized and your button panel stays at its preferred size:

    Java Code:
    JPanel wrapper = new JPanel( new FlowLayout(FlowLayout.LEFT, 0, 0) );
    wrapper.add( buttonPanel );
    buttonScroll = new JScrollPane(wrapper);
    //buttonScroll = new JScrollPane(buttonPanel);
    Thank you! This code also worked. The only difference in outcome between the code I went with and the code you suggested is that the buttons stay aligned in the top corner with yours and with mine they are all centered.
    My code:

    Java Code:
    buttonPanel = new JPanel();
    		
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    buttonPanel.setLayout(gbl);
    		
    for (int i = 0; i < NUM; i++)
    {
    	for (int j = 0; j < NUM; j++)
    	{
    		buttons[j][i] = new SquareButton(j + "," + i);
    		buttons[j][i].setPreferredSize(new Dimension(25, 25));
    		c.gridx = i;
    		c.gridy = j;
    		gbl.setConstraints(buttons[j][i], c);
    		buttonPanel.add(buttons[j][i]);
    	}
    }
    		
    buttonScroll = new JScrollPane(buttonPanel);
    		buttonScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    		buttonScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    Is there anything about the code that I have chosen that I should be conerned with?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    What is the difference between revalidate() and updateUI()?
    Have you read the API of both the methods?

    revalidate
    Supports deferred automatic layout.

    updateUI
    Resets the UI property to a value from the current look and feel.

    Both revalidate() and updateUI() have the same effect in terms of my program.
    That's only because updateUI() cals setUI(...) which updates the UI and then calls revalidate().

    db

Similar Threads

  1. resizing a rectangle?
    By ludis in forum New To Java
    Replies: 9
    Last Post: 12-12-2010, 01:00 PM
  2. button not resizing in awt
    By alinaqvi90 in forum AWT / Swing
    Replies: 1
    Last Post: 08-17-2010, 03:43 PM
  3. panel resizing
    By simo_mon in forum AWT / Swing
    Replies: 1
    Last Post: 08-15-2009, 02:09 PM
  4. JFrame resizing
    By carderne in forum New To Java
    Replies: 3
    Last Post: 07-22-2009, 07:42 PM
  5. Image resizing
    By alley in forum Java 2D
    Replies: 2
    Last Post: 11-13-2007, 10:10 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
  •