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.

[1107 byte] By [NegativeSpace13579a] at [2007-11-27 9:24:32]
# 1
post demo code here
dayanandabva at 2007-7-12 22:20:30 > top of Java-index,Desktop,Core GUI APIs...
# 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);

}

NegativeSpace13579a at 2007-7-12 22:20:30 > top of Java-index,Desktop,Core GUI APIs...
# 3
Try creating the JTextArea using the constructor that take a row and column as a paramenter. That was the text area will have a preferred size.
camickra at 2007-7-12 22:20:30 > top of Java-index,Desktop,Core GUI APIs...
# 4
This doesn't change things.
NegativeSpace13579a at 2007-7-12 22:20:30 > top of Java-index,Desktop,Core GUI APIs...
# 5
Hi,I have yet to resolve this issue and it is becoming increasingly frustrating.Can anyone suggest something?Regards.
NegativeSpace13579a at 2007-7-12 22:20:30 > top of Java-index,Desktop,Core GUI APIs...
# 6

If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",

see http://homepage1.nifty.com/algafield/sscce.html,

that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the "Code Formatting Tags",

see http://forum.java.sun.com/help.jspa?sec=formatting,

so the posted code retains its original formatting.

camickra at 2007-7-12 22:20:30 > top of Java-index,Desktop,Core GUI APIs...