How to align components
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);
}
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
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.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
OK, I started to break down even this two components panels, I though there is some simple/smart solution to this :confused:
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
Check out the GridBagConstraints.anchor field.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
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
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902