User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 374,011 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,783 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 462 | Replies: 6
Reply
Join Date: Mar 2006
Posts: 127
Reputation: degamer106 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

JPanel's size and JButton

  #1  
May 8th, 2008
When I add buttons to a panel, the buttons seem to take a huge amount of space. How do I get rid of the space? For example:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Tester
{
	public static void main(String[] args)
	{
		JButton dayButton = new JButton("Day");
		JButton weekButton = new JButton("Week");
		JButton monthButton = new JButton("Month");
		JButton leftButton = new JButton("<<");
		JButton rightButton = new JButton(">>");

		JPanel topButtonPanel = new JPanel();
		topButtonPanel.add(dayButton);
		topButtonPanel.add(weekButton);
		topButtonPanel.add(monthButton);

		JPanel bottomButtonPanel = new JPanel();
		bottomButtonPanel.add(leftButton);
		bottomButtonPanel.add(rightButton);

		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
		buttonPanel.add(topButtonPanel);
		buttonPanel.add(bottomButtonPanel);

		JFrame frame = new JFrame();

		frame.add(buttonPanel);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		frame.setVisible(true);
	}
	private static final int FRAME_WIDTH = 1000;
	private static final int FRAME_HEIGHT = 1000;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,494
Reputation: Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice 
Rep Power: 9
Solved Threads: 251
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Nearly a Posting Maven

Re: JPanel's size and JButton

  #2  
May 8th, 2008
By using different layout managers: http://java.sun.com/docs/books/tutor...out/index.html
Reply With Quote  
Join Date: Mar 2006
Posts: 127
Reputation: degamer106 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: JPanel's size and JButton

  #3  
May 8th, 2008
Ok I got it.

A question related to layout managers though: I know BoxLayout tends to "stretch" the components to make them fit inside a panel. How do I modify the panel's dimensions?
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,494
Reputation: Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice 
Rep Power: 9
Solved Threads: 251
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Nearly a Posting Maven

Re: JPanel's size and JButton

  #4  
May 8th, 2008
You can setPreferredSize() and some layout managers will honor it, but it's not guaranteed. It depends on the layout manager how component sizing is handled. You would need to be more specific about the placement and sizing behavior you are wanting to give a more direct answer.
Reply With Quote  
Join Date: Mar 2006
Posts: 127
Reputation: degamer106 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: JPanel's size and JButton

  #5  
May 8th, 2008
I have a bunch of text and combo boxes. For example,

Day: [Day Combo Box]
Date: [Date Combo Box]
AMPM: [AMPM Combo Box]

I used BoxLayout.Y_AXIS to align them vertically but the boxes seem to stretch out. Also, the sizes of the boxes and the text alignment get kinda crooked just like what I typed in.
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,494
Reputation: Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice Ezzaral is just really nice 
Rep Power: 9
Solved Threads: 251
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Nearly a Posting Maven

Re: JPanel's size and JButton

  #6  
May 9th, 2008
Those are the things you have to consider when choosing which layouts to use. Each has it's own purpose and constraints. What you are describing above would be easily tamed with a GridBagLayout, which is highly flexible but trickier to get used to. Here's an example
  1. JLabel lblDay = new JLabel("Day");
  2. JLabel lblWeek = new JLabel("Week");
  3. JLabel lblMonth = new JLabel("Month");
  4.  
  5. Dimension comboDimension = new Dimension(120, 18);
  6. JComboBox cboDay = new JComboBox();
  7. cboDay.setPreferredSize(comboDimension);
  8. JComboBox cboWeek = new JComboBox();
  9. cboWeek.setPreferredSize(comboDimension);
  10. JComboBox cboMonth = new JComboBox();
  11. cboMonth.setPreferredSize(comboDimension);
  12.  
  13. JButton leftButton = new JButton("<<");
  14. JButton rightButton = new JButton(">>");
  15.  
  16. JPanel topButtonPanel = new JPanel();
  17. topButtonPanel.setLayout(new GridBagLayout());
  18. GridBagConstraints constraints = new GridBagConstraints();
  19. constraints.gridx = 0;
  20. constraints.anchor = GridBagConstraints.EAST;
  21. constraints.insets = new Insets(2, 4, 2, 4);
  22. topButtonPanel.add(lblDay, constraints);
  23. topButtonPanel.add(lblWeek, constraints);
  24. topButtonPanel.add(lblMonth, constraints);
  25.  
  26. constraints.gridx = 1;
  27. constraints.anchor = GridBagConstraints.WEST;
  28. constraints.insets = new Insets(2, 4, 2, 4);
  29. topButtonPanel.add(cboDay, constraints);
  30. topButtonPanel.add(cboWeek, constraints);
  31. topButtonPanel.add(cboMonth, constraints);
  32.  
  33. JPanel bottomButtonPanel = new JPanel();
  34. bottomButtonPanel.add(leftButton);
  35. bottomButtonPanel.add(rightButton);
  36.  
  37. JPanel buttonPanel = new JPanel();
  38. buttonPanel.setLayout(new BorderLayout());
  39. buttonPanel.add(topButtonPanel, BorderLayout.NORTH);
  40. buttonPanel.add(bottomButtonPanel, BorderLayout.SOUTH);
  41.  
  42. JFrame frame = new JFrame();
  43.  
  44. frame.add(buttonPanel);
  45.  
  46. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  47. frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
  48. frame.setVisible(true);
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,588
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 187
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: JPanel's size and JButton

  #7  
May 9th, 2008
BoxLayout will stretch to the size of the largest component in it.
All layout managers generally behave like that (or similar).
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 10:53 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC