JPanel's size and JButton

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

JPanel's size and JButton

 
0
  #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:

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class Tester
  6. {
  7. public static void main(String[] args)
  8. {
  9. JButton dayButton = new JButton("Day");
  10. JButton weekButton = new JButton("Week");
  11. JButton monthButton = new JButton("Month");
  12. JButton leftButton = new JButton("<<");
  13. JButton rightButton = new JButton(">>");
  14.  
  15. JPanel topButtonPanel = new JPanel();
  16. topButtonPanel.add(dayButton);
  17. topButtonPanel.add(weekButton);
  18. topButtonPanel.add(monthButton);
  19.  
  20. JPanel bottomButtonPanel = new JPanel();
  21. bottomButtonPanel.add(leftButton);
  22. bottomButtonPanel.add(rightButton);
  23.  
  24. JPanel buttonPanel = new JPanel();
  25. buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
  26. buttonPanel.add(topButtonPanel);
  27. buttonPanel.add(bottomButtonPanel);
  28.  
  29. JFrame frame = new JFrame();
  30.  
  31. frame.add(buttonPanel);
  32.  
  33. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34. frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
  35. frame.setVisible(true);
  36. }
  37. private static final int FRAME_WIDTH = 1000;
  38. private static final int FRAME_HEIGHT = 1000;
  39. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: JPanel's size and JButton

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

Re: JPanel's size and JButton

 
0
  #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 Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: JPanel's size and JButton

 
0
  #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 Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: JPanel's size and JButton

 
0
  #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 Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: JPanel's size and JButton

 
0
  #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 Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: JPanel's size and JButton

 
0
  #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).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC