Re: GridBagLayouts
On 28 Apr, 14:24, RedGrittyBrick <RedGrittyBr...@spamweary.invalid>
wrote:
Pete wrote:
On 28 Apr, 12:17, RedGrittyBrick <RedGrittyBr...@spamweary.invalid>
wrote:
Pete wrote:
Hi,
I have a query w.r.t using the GridBagLayout which I hope someone can
help with.
Basically, I have two GridBagLayouts, (GridBagLayoutWest &
GridBagLayoutCenter).
In GridBagLayoutWest, there are four rows & 3 coloumns.
In GridBagLayoutCenter, there is only one row & 3 coloumns.
I put both of the above inside a border layout, GridBagLayoutWest in
WEST and GridBagLayoutCenter in CENTER.
When I display this, GridBagLayoutWest displays fine but
GridBagLayoutCenter with its one does display as I expect.
I expected row zero of GridBagLayoutCenter to be in line with row zer=
o
GridBagLayoutWest but it is in the middle
GridBagLayoutWest GridBagLayoutCenter
Row1
Row2 =
Row1
Row3
Row4
I appreciate that the solution will need to use GridBagConstraints
correctly, but I tried with no luck. I don't understand this area
well. I have tried to understand the Sun's tutorial.
Any help would be appreciated.
Borderlayout makes anything in the CENTER expand to fill available
space. To align the center Row1 at the top of the available space I'd
try setting the center Row1 constraint's anchor to PAGE_START.
If precise alignment is important, I'd try using one Gridbaglayout for
all the West and Center components without using BorderLayout to split
them into groups.
--
RGB- Hide quoted text -
- Show quoted text -
Please don't quote signatures, thanks.
Thanks for the suggestion RGB. I tried it with no luck
I have provided snippet of the code. If you can see an issue please
let me. Thanks
SSCCEs are much better than snippets, here's an SSCCE that solves your
problem. By preparing an SSCCE you'd probably get more people involved
in helping you, seehttp://sscce.org.
------------------------------8<--------------------------------------
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
public class PetesPanels {
/**
* @author: RedGrittyBrick, Pete
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PetesPanels().createAndShowGUI();
}
});
}
private void createAndShowGUI() {
JFrame f = new JFrame("Pete's Panels");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(makePetesPanels());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private JPanel makePetesPanels() {
JPanel petesPanel = new JPanel();
GridBagConstraints c = new GridBagConstraints();
JRadioButton
nameRadioButton = new JRadioButton("=
Name"),
locationRadioButton = new JRadioButt=
on("Location"),
nameAndLocationRadioButton
= new JRadioButton("=
Name & Location"),
allContractorsRadioButton
= new JRadioButton("=
All contractors");
JLabel
searchNameLabel = new JLabel("Search=
name"),
searchLocationLabel = new JLabel("Se=
arch Location"),
customerIdLabel = new JLabel("Custom=
er ID");
JTextField
searchNameField = new JTextField(20)=
,
searchLocationField = new JTextField=
(20),
customerIdField = new JTextField(20)=
;
;
// ---------------- Start of Pete's snippet ----------=
--------
JPanel GridBagLayoutWest = new JPanel(new GridBagLay=
out());
JPanel GridBagLayoutCenter = new JPanel(new GridBagL=
ayout());
//
// Configure GridBagLayoutWest
c.gridx = 0;
c.gridy = 0;
c.ipadx = 0;
c.ipady = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.NONE;
c.insets = new Insets(2, 2, 0, 0);
c.weightx = 0.0;
c.weighty = 0.0;
GridBagLayoutWest.add(nameRadioButton, c);
c.gridx = 0;
c.gridy = 1;
GridBagLayoutWest.add(locationRadioButton, c);
c.gridy = 2;
GridBagLayoutWest.add(nameAndLocationRadioButton, c);
c.gridy = 3;
GridBagLayoutWest.add(allContractorsRadioButton, c);
c.gridy = 0;
c.gridx = 1;
GridBagLayoutWest.add(searchNameLabel, c);
c.gridy = 0;
c.gridx = 2;
GridBagLayoutWest.add(searchNameField, c);
c.gridy = 1;
c.gridx = 1;
GridBagLayoutWest.add(searchLocationLabel, c);
c.gridy = 1;
c.gridx = 2;
GridBagLayoutWest.add(searchLocationField, c);
//
// Configure GridBagLayoutCenter
c.gridx = 0;
c.gridy = 0;
c.ipadx = 0;
c.ipady = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.anchor = GridBagConstraints.PAGE_START;
c.fill = GridBagConstraints.NONE;
c.insets = new Insets(2, 2, 0, 0);
c.weightx = 0.0;
c.weighty = 1.0; // <===========
==================== LOOK! RGB.
c.gridy = 0;
c.gridx = 0;
GridBagLayoutCenter.add(customerIdLabel, c);
c.gridy = 0;
c.gridx = 1;
GridBagLayoutCenter.add(customerIdField, c);
JButton bookButton = new JButton("Book");
c.gridy = 0;
c.gridx = 2;
GridBagLayoutCenter.add(bookButton, c);
JPanel bottomPanel = new JPanel(new BorderLayout());
bottomPanel.add(GridBagLayoutWest, BorderLayout.WEST);
bottomPanel.add(GridBagLayoutCenter, BorderLayout.CENT=
ER);
// ------------------- End of Pete's snippet ---------=
------
petesPanel.setLayout(new BorderLayout());
petesPanel.add(GridBagLayoutWest, BorderLayout.WEST);
petesPanel.add(GridBagLayoutCenter, BorderLayout.CENTE=
R);
return petesPanel;
}
}
------------------------------------8<--------------------------------
P.S.
Don't forget, variable names should start with a lowercase letter. I'd
rename GridBagLayoutWest as westPanel since it is a JPanel and not a Layo=
ut.
HTH
--
RGB- Hide quoted text -
- Show quoted text -
RGB
Thanks for :-
1) enlighting me on SSCCEs.
2) sorting out the issue which has worked. I have been on this for a
couple of days.
I also got the same effect by using "c.insets = new Insets(2, 0, 60,
0);" ie change 0 to 60.
I have the Sun's Tutorial Swing book on layouts infront of me, but I
just can't appreciate these "Constraints". My suggestion of 60, is
simply padding of 60 "spaces ?" below Row0. Could you explain how your
suggestions works please ?
Again thank you.
Pete