| | |
JPanel's size and JButton
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
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)
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; }
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
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.
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.
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)
JLabel lblDay = new JLabel("Day"); JLabel lblWeek = new JLabel("Week"); JLabel lblMonth = new JLabel("Month"); Dimension comboDimension = new Dimension(120, 18); JComboBox cboDay = new JComboBox(); cboDay.setPreferredSize(comboDimension); JComboBox cboWeek = new JComboBox(); cboWeek.setPreferredSize(comboDimension); JComboBox cboMonth = new JComboBox(); cboMonth.setPreferredSize(comboDimension); JButton leftButton = new JButton("<<"); JButton rightButton = new JButton(">>"); JPanel topButtonPanel = new JPanel(); topButtonPanel.setLayout(new GridBagLayout()); GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = 0; constraints.anchor = GridBagConstraints.EAST; constraints.insets = new Insets(2, 4, 2, 4); topButtonPanel.add(lblDay, constraints); topButtonPanel.add(lblWeek, constraints); topButtonPanel.add(lblMonth, constraints); constraints.gridx = 1; constraints.anchor = GridBagConstraints.WEST; constraints.insets = new Insets(2, 4, 2, 4); topButtonPanel.add(cboDay, constraints); topButtonPanel.add(cboWeek, constraints); topButtonPanel.add(cboMonth, constraints); JPanel bottomButtonPanel = new JPanel(); bottomButtonPanel.add(leftButton); bottomButtonPanel.add(rightButton); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new BorderLayout()); buttonPanel.add(topButtonPanel, BorderLayout.NORTH); buttonPanel.add(bottomButtonPanel, BorderLayout.SOUTH); JFrame frame = new JFrame(); frame.add(buttonPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setVisible(true);
![]() |
Similar Threads
- Adding an Image to a JPanel (Java)
- Creating a "Data" folder in my C: Drive using my Java program (Java)
- Delete button not working on my Java GUI Inventory (Java)
- GUI problem.. (Java)
- How to align components (Java)
- Java GUI JOptionPane.showMessageDialog (Java)
- help!! My applet can't run... Urgent!! (Java)
Other Threads in the Java Forum
- Previous Thread: Read and update a ini file
- Next Thread: need help with for loop
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application array arrays automation binary bluetooth button character chat class classes client code component consumer css csv database eclipse ee error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javaprojects jmf jni jpanel julia jvm key linked linux list loan loop map method methods mobile netbeans newbie objects oriented output panel phone print printf problem program programming project projects recursion replaydirector reporting researchinmotion robot rotatetext scanner screen se server service set size sms software sort sql string swing test threads transfer tree ubuntu windows






