• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Alignment and Spacing between components

Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

Sanjit Singh
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Trish,
THANKS!!! a lot for your help. This is exactly what I wanted to achieve...
Thanks once again!
Tris Rabar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Villains always have antidotes. They're funny that way. Here's an antidote disguised as a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic