| | |
How to align components
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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.
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
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)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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
----------------------------------------------
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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- adding scrollPane to panel with components aligned at fixed bounds (Java)
- Cant add/remove win components (Windows NT / 2000 / XP)
- Hardware - Cases - Want to separate components (Troubleshooting Dead Machines)
Other Threads in the Java Forum
- Previous Thread: Accessing frame from Action listener
- Next Thread: more syntax questions
Views: 3863 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application apps arguments array arrays automation binary bluetooth businessintelligence card chat class classes client code collision component crashcourse database detection draw eclipse error event exception file fractal free game gis givemetehcodez graphics gui helpwithhomework html ide image input integer integration j2me java javadoc javafx javaprojects jmf jni jpanel julia jvm linux list loop machine map method methods migrate mobile netbeans newbie nls number object oracle os physics print problem program programming project radio recursion remote scanner screen security server set size sms socket sort sql string swing test textfield threads time transfer tree trolltech utility windows






