•
•
•
•
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
![]() |
•
•
Join Date: Mar 2006
Posts: 127
Reputation:
Rep Power: 3
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:
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;
} By using different layout managers: http://java.sun.com/docs/books/tutor...out/index.html
•
•
Join Date: Mar 2006
Posts: 127
Reputation:
Rep Power: 3
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);
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
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



Linear Mode