944,134 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 8530
  • Java RSS
Feb 11th, 2007
0

How to align components

Expand Post »
In the JPanel I placed JTextField and JButton, as the layout I'm using GridBagLayout which is left to default align (center). However because of the size of whole JFrame I would like to get JTextField to left and JButton to right. How do I do that

private GridBagLayout gbag = new GridBagLayout();
private GridBagConstraints gbc = new GridBagConstraints();
private Border bs = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
.
.
.
.    
    public void setupIndexPanel()
    {
    	panelIndex = new JPanel();
    	panelIndex.setBorder(new TitledBorder(bs, "Index File Path"));
    	panelIndex.setLayout(gbag);
    	tfIndex = new JTextField(20);
    	tfIndex.setEditable(false);
    	gbc.gridx = 0;
    	gbc.gridy = 0;
    	gbag.setConstraints(tfIndex, gbc);
    	panelIndex.add(tfIndex);
    	btnIndex = new JButton("Get index");
    	btnIndex.setPreferredSize(new Dimension(100, 30));
    	btnIndex.addActionListener(this);
    	gbc.gridx = 1;
    	gbc.gridy = 0;
    	gbag.setConstraints(btnIndex, gbc);
    	panelIndex.add(btnIndex);
    }
Similar Threads
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Feb 11th, 2007
0

Re: How to align components

Don't try to force your design into a single layoutmanager.
Layer your layout.
You could for example place a panel in the region of the gridbag that you want those two components to appear, set that to gridlayout, and place the components in the cells of that grid.
These cells can be individually configured to have different alignment properties.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Feb 13th, 2007
0

Re: How to align components

OK, I started to break down even this two components panels, I though there is some simple/smart solution to this
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Feb 14th, 2007
0

Re: How to align components

Check out the GridBagConstraints.anchor field.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Feb 14th, 2007
0

Re: How to align components

Click to Expand / Collapse  Quote originally posted by masijade ...
Check out the GridBagConstraints.anchor field.
I used it but get no changes when I used Absolute values. Only changes been when Baseline Relative values applied but with this I can acive my disired alignment
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 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: Accessing frame from Action listener
Next Thread in Java Forum Timeline: more syntax questions





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


Follow us on Twitter


© 2011 DaniWeb® LLC