This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Alignment and Spacing between components Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Alignment and Spacing between components " Watch "Alignment and Spacing between components " New topic

Alignment and Spacing between components

Sanjit Singh

Joined: Nov 12, 2001
Posts: 27
Hi, Here is my code
import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class PacketGUI extends JFrame {
private Container container;
private GridBagLayout gbLayout;
private GridBagConstraints gbConstraints;
private JComboBox protocolName;
private String protocolOptions[] = {"tcp", "udp", "icmp"};
private JTextField sourceAddressField;
private JTextField sourcePortField;
private JTextField srcMaskField;
private JTextField destinationAddressField;
private JTextField destinationPortField;
private JTextField dstMaskField;
private JComboBox flagType;
private String tcpFlagOptions[] = {"Acknowledgement-A", "Reset-R", "Fin-F", "Syn-S"};
private JButton sendButton;
private JButton resetButton;
private JLabel introMessageLabel;
private JLabel protocolNameLabel;
private JLabel flagTypeLabel;
private JLabel sourceAddressLabel;
private JLabel sourcePortLabel;
private JLabel srcMaskLabel;
private JLabel destinationAddressLabel;
private JLabel destinationPortLabel;
private JLabel dstMaskLabel;
public PacketGUI() {
super("Packet GUI");
container = getContentPane();
gbLayout = new GridBagLayout();
gbConstraints = new GridBagConstraints();
introMessageLabel = new JLabel("Kindly enter the details of the IP Packet to be sent:", SwingConstants.LEFT);
protocolNameLabel = new JLabel("Protocol:", SwingConstants.RIGHT);
protocolName = new JComboBox(protocolOptions);
flagTypeLabel = new JLabel("TCP Flag:", SwingConstants.RIGHT);
flagType = new JComboBox(tcpFlagOptions);
sourceAddressLabel = new JLabel("Source Address:", SwingConstants.RIGHT);
sourceAddressField = new JTextField(15);
sourcePortLabel = new JLabel("Source Port:", SwingConstants.RIGHT);
sourcePortField = new JTextField(5);
srcMaskLabel = new JLabel("Src Mask:", SwingConstants.RIGHT);
srcMaskField = new JTextField(15);
destinationAddressLabel = new JLabel("Destination Address:", SwingConstants.RIGHT);
destinationAddressField = new JTextField(15);
destinationPortLabel = new JLabel("Destination Port:", SwingConstants.RIGHT);
destinationPortField = new JTextField(5);
dstMaskLabel = new JLabel("Dst Mask:", SwingConstants.RIGHT);
dstMaskField = new JTextField(15);
resetButton = new JButton("RESET");
sendButton = new JButton("SEND");
addComponent(introMessageLabel, 0, 0, 2, 1);
addComponent(protocolNameLabel, 2, 0, 1, 2);
addComponent(protocolName, 2, 1, 1, 1);
addComponent(flagTypeLabel, 2, 2, 1, 1);
addComponent(flagType, 2, 3, 1, 1);
addComponent(sourceAddressLabel, 4, 0, 1, 1);
addComponent(sourceAddressField, 4, 1, 1, 1);
addComponent(sourcePortLabel, 4, 2, 1, 1);
addComponent(sourcePortField, 4, 3, 1, 1);
addComponent(srcMaskLabel, 5, 2, 1, 1);
addComponent(srcMaskField, 5, 3, 1, 1);
addComponent(destinationAddressLabel, 7, 0, 1, 1);
addComponent(destinationAddressField, 7, 1, 1, 1);
addComponent(destinationPortLabel, 7, 2, 1, 1);
addComponent(destinationPortField, 7, 3, 1, 1);
addComponent(dstMaskLabel, 8, 2, 1, 1);
addComponent(dstMaskField, 8, 3, 1, 1);
addComponent(resetButton, 10, 1, 1, 1);
addComponent(sendButton, 10, 2, 1, 1);
setSize(600, 200);
private void addComponent(Component c, int row, int column, int width, int height) {
gbConstraints.gridx = column;
gbConstraints.gridy = row;
gbConstraints.gridwidth = width;
gbConstraints.gridheight = height;
gbLayout.setConstraints(c, gbConstraints);
public static void main(String[] args) {
PacketGUI packetGUI = new PacketGUI();
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
********************************************************** As you can see above, I have used GridBag layout. I would like to achieve the folowing but somehow cant achieve it. Any help in these points would behighly appreciated:
1. Alignment of Components --> I would like to align my components, like buttons, text fields, labels and jcombobox towards the left and right. help?
2.Spacing between Components --> As you an see inthe code, I am trying something wierd by skipping row numbers, to leave a blank between the components (vertically). I think the right way is to use insets or insert empty border or somehow idnerst spaces. COuld somebody please shed some light on this and tell me how to doit>
3.Note:Swingconstants.RIGHT is not working!!!
Thanks in advance! John
Tris Rabar
Ranch Hand

Joined: Feb 27, 2002
Posts: 72
Do you have to use GridBagLayout? You can try the following code if you don't. I'm not sure if it's exactly what you want though, because:
1) the JtextFields are all the same width. But, note that although you set the textfield width to a certain value, this does not limit the amount of characters that can be entered.
2) you can eliminate the space between the first textfield and the second label if it's not necessary, because it results in the selected text in JComboBox to be cut.
Let me know if this is what you want please..
All the best!

- Trish -
Sanjit Singh

Joined: Nov 12, 2001
Posts: 27
Hi Trish,
THANKS!!! a lot for your help. This is exactly what I wanted to achieve...
Thanks once again!
Tris Rabar
Ranch Hand

Joined: Feb 27, 2002
Posts: 72
I agree. Here's the link:
subject: Alignment and Spacing between components
It's not a secret anymore!