943,870 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 9176
  • Java RSS
May 8th, 2008
0

JPanel's size and JButton

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
May 8th, 2008
0

Re: JPanel's size and JButton

By using different layout managers: http://java.sun.com/docs/books/tutor...out/index.html
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
May 8th, 2008
0

Re: JPanel's size and JButton

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
May 8th, 2008
0

Re: JPanel's size and JButton

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
May 8th, 2008
0

Re: JPanel's size and JButton

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
May 9th, 2008
0

Re: JPanel's size and JButton

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
java Syntax (Toggle Plain Text)
  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);
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
May 9th, 2008
0

Re: JPanel's size and JButton

BoxLayout will stretch to the size of the largest component in it.
All layout managers generally behave like that (or similar).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java Certification Exam Info
Next Thread in Java Forum Timeline: need help with for loop





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC