User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 397,816 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,596 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: 1414 | Replies: 4
Reply
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,183
Reputation: peter_budo has a spectacular aura about peter_budo has a spectacular aura about peter_budo has a spectacular aura about 
Rep Power: 10
Solved Threads: 265
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

How to align components

  #1  
Feb 11th, 2007
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);
    }
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,661
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 193
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: How to align components

  #2  
Feb 11th, 2007
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.
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,183
Reputation: peter_budo has a spectacular aura about peter_budo has a spectacular aura about peter_budo has a spectacular aura about 
Rep Power: 10
Solved Threads: 265
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: How to align components

  #3  
Feb 13th, 2007
OK, I started to break down even this two components panels, I though there is some simple/smart solution to this
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Join Date: Feb 2006
Posts: 1,363
Reputation: masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough 
Rep Power: 8
Solved Threads: 117
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: How to align components

  #4  
Feb 14th, 2007
Check out the GridBagConstraints.anchor field.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,183
Reputation: peter_budo has a spectacular aura about peter_budo has a spectacular aura about peter_budo has a spectacular aura about 
Rep Power: 10
Solved Threads: 265
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: How to align components

  #5  
Feb 14th, 2007
Originally Posted by masijade View Post
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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 6:37 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC