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"));
    	tfIndex = new JTextField(20);
    	gbc.gridx = 0;
    	gbc.gridy = 0;
    	gbag.setConstraints(tfIndex, gbc);
    	btnIndex = new JButton("Get index");
    	btnIndex.setPreferredSize(new Dimension(100, 30));
    	gbc.gridx = 1;
    	gbc.gridy = 0;
    	gbag.setConstraints(btnIndex, gbc);

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.

OK, I started to break down even this two components panels, I though there is some simple/smart solution to this :confused:

Check out the GridBagConstraints.anchor field.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.