GridBagLayout and JTextArea.setLineWrap(true)
Hi,
I am writing a GUI which uses a GridBagLayout as its layout manager.
It basically has two columns: the first fills 30%, and the second 70% the width of the JFrame.
This seems to work fine except that when I add a JTextArea and set it to line wrap, it expands the second column to fill as far as it can without overwriting and of the components in the first column.
before setLineWrap(true);
col1col2
|a| |
|b| JTexArea|
|c| |
after:
col1col2
|a||
|b|JTextArea.setLineWrap(true)|
|c||
I hope the above helps to explain the situation.
This _only_ happens when I set the line wrapping. I want its fill setting to be BOTH, so that it fills its 70% of the width of JFrame. I have tried setting it to NONE, which shrinks it so it looks ridiculous. Setting its preferred and/or maximum size doesn't do anything, either.
Curiously, it doesn't do the same thing vertically, only horizontally.
Can anyone suggest why this is happening and how to fix it?
Cheers.
# 2
JButton jbAdd;
JTextField jtDescription, jtSIM, jtHash, jtIMEI;
JPanel mainPanel;
JTextArea jtaNotes;
JScrollPane jsNotes;
public TheClass(){
// initialising components...
...
jtaNotes = new JTextArea("");
jtaNotes.setLineWrap(true);
jtaNotes.setWrapStyleWord(true);
jsNotes = new JScrollPane(jtaNotes);
mainPanel.setLayout(gbl);
addComponent(new JLabel("Description"),0, 0, 1, 1, 70, 10, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
addComponent(jtDescription, 1, 0, 1, 1,30, 10, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER);
addComponent(new JLabel("SIM"), 0, 1, 1, 1, 70, 10, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
addComponent(jtSIM, 1, 1, 1, 1, 30, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER);
addComponent(new JLabel("Hash"), 0, 2, 1, 1, 70, 10, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
addComponent(jtHash, 1, 2, 1, 1, 30, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER);
addComponent(new JLabel("IMEI"), 0, 3, 1, 1, 70, 10, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
addComponent(jtIMEI, 1, 3, 1, 1, 30, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER);
addComponent(new JLabel("Notes"), 0, 4, 1, 1, 70, 40, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
addComponent(jsNotes, 1, 4, 1, 1, 30, 40, GridBagConstraints.VERTICAL, GridBagConstraints.CENTER);
addComponent(new JLabel("On Loan"), 0, 5, 1, 1, 70, 10, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
addComponent(jcIsOnLoan, 1, 5, 1, 1, 30, 10, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER);
addComponent(jbAdd, 0, 6, 2, 1, 100, 10, GridBagConstraints.NONE, GridBagConstraints.CENTER);
setLayout(new BorderLayout());
add(mainPanel, BorderLayout.CENTER);
// setup JFrame and go...
}
public void addComponent(Component c, int gridx, int gridy, int gridwidth, int gridheight, int weightx,int weighty, int fill, int anchor) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.fill = fill;
gbc.anchor = anchor;
gbl.setConstraints(c, gbc);
// add the component to the panel.
mainPanel.add(c);
}