Add a JLabel below a JTextArea in a boxlayout

I have a JTextArea in a JScrollPane in a GUI, and I'm using BoxLayout. Occasionally, I wish to add a JLabel to my JScrollPane, which works fine.

However, since my BoxLayout is set to the Y_Axis layout, the JLabel always ends up being centered below the JTextArea, and I want it, as well as the JTextArea, to be aligned to the left. I know there is a LEFT_ALIGNMENT operator, but I am unsure of how to use it, although I've read the API for java.awt.Component.

Also, my JTextArea has a maximum column size, but when the JLabel is displayed, the JTextArea stretches across my entire JScrollPane window. I've tried setting a maximum size, but for some reason that tends to cut off some text on the left side of the JTextArea.

The JLabel will almost always be wider than the JTextArea. Perhaps that is causing some problems with BoxLayout?

If there's any more information I should provide, please let me know.

Thanks for any help you can provide,

Dan

Message was edited by:

Djaunl

[1038 byte] By [Djaunla] at [2007-11-26 16:09:18]
# 1

Ok I figured out how the align the JLabel to the left:

myJLabel.setAlignmentX(Component.LEFT_ALIGNMENT);

However, I'm having an odd problem with my JTextArea. If my GUI displays the text normally, everything is fine. But once I add a JLabel to my JScrollPane that is wider than the view (i.e. so I need to scroll horizontally to see the entire JLabel), when I load more text, the text will not position itself within the JScrollPane view, and I will have to scroll over to see all of the text, which I do not want to do.

Is there a way to keep the JTextArea at the same maximum column number, even though there is a JLabel below it that is much wider than the JTextArea should be?

I get the feeling this isn't making sense. Here's the code to create my components:

// Create min/max dimensions

Dimension minimumSize = new Dimension(200, 500);

Dimension maximumSize = new Dimension(500, 500);

Dimension buttonSize = new Dimension (125, 20);

int cols = 30;

// Create Tree viewing pane

JScrollPane treeView = new JScrollPane(tree);

treeView.setMinimumSize(minimumSize);

treeView.setMaximumSize(maximumSize);

// Create HTML viewing pane 1

final JPanel htmlView1 = new JPanel();

BoxLayout BL = new BoxLayout(htmlView1, BoxLayout.Y_AXIS);

htmlView1.setLayout(BL);

final JScrollPane htmlPane = new JScrollPane(htmlView1);

htmlPane.setMinimumSize(minimumSize);

htmlPane.setMaximumSize(maximumSize);

// Create text area1

final JTextArea textArea1 = new JTextArea();

textArea1.setLineWrap(true);

textArea1.setWrapStyleWord(true);

textArea1.setColumns(cols);

textArea1.setAlignmentX(Component.LEFT_ALIGNMENT);

JLabel pic = new JLabel (new ImageIcon(new URL(DBQuery.list.get(1))));

pic.setAlignmentX(Component.LEFT_ALIGNMENT);

htmlView1.add(pic);

Later in the code, I add textArea1 to htmlView1.

I'm sorry if this is very confusing. If there is anything I can do to help, please let me know.

Djaunla at 2007-7-8 22:31:36 > top of Java-index,Desktop,Core GUI APIs...
# 2

BoxLayout should respect the maximum size of a component so I'm not sure whats wrong.

Try changing the alignmentX of both components to make sure they are the same.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-8 22:31:36 > top of Java-index,Desktop,Core GUI APIs...