Flowlayout in a vertical fashion

S

sanmits

I am new to layout managers and I am not sure how to do this. I want to
have text paragraphs of various size shown in a vertical fashion in a
panel. I don't want to specify preferred size for the purpose of
internationalization though I have panel width and height specified
within which all the paragraphs should fit in.

I know I can use textarea but for flexibilty purpose (like having some
text underlined or different font,color) I tried using jlables with
html text for line wrapping and it works fine as long as I specify the
preferred size of the label such that height is set properly for
displaying multiple rows. Is there anyway I can do this without
specifying the height so that if text grows so does the height of
jlable automatically?

my code sample:

JPanel p1 = new JPanel(new FlowLayout());
ParentPanel.add(p1, Borderlayout.South); // panel added to south of
parent panel

//now add labels to p1
JLabel jLabel1 = new javax.swing.JLabel();
jLabel1.setFont(new java.awt.Font("Arial", 0, 12));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
jLabel1.setText("<html>Long Text.</html>");
//jLabel1.setPreferredSize(new java.awt.Dimension(250, 45)); //code i
want to avoid
p1.add(jLabel1);

JLabel jLabel2= new javax.swing.JLabel();
jLabel2.setFont(new java.awt.Font("Arial", 0, 12));
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
jLabel2.setText("<html>Another long text.</html>");
p1.add(jLabel2);

Any help is appreciated.

Thanks.
 
A

Andrew T.

I am new to layout managers and I am not sure how to do this. I want to
have text paragraphs of various size shown in a vertical fashion in a
panel.
<big snip>

I suggest you put all your paragraphs in a single JEditorPane
in a JScrollPane in the JPanel for which you setPreferredSize().

As an aside, the GUI experts mostly read..
http://groups.google.com/group/comp.lang.java.gui

HTH

Andrew T.
 
A

Andrey Kuznetsov

I know I can use textarea but for flexibilty purpose (like having some
text underlined or different font,color) I tried using jlables with
html text for line wrapping and it works fine as long as I specify the
preferred size of the label such that height is set properly for
displaying multiple rows. Is there anyway I can do this without
specifying the height so that if text grows so does the height of
jlable automatically?

you should revalidate JLabel after text has changed.

Andrey
 
B

Bart Rider

Andrey said:
you should revalidate JLabel after text has changed.

Andrey

Should not be neccessary. If changing a text in a JLabel, it
is automatically updated and repainted.

Bart
 
B

Bart Rider

I know I can use textarea but for flexibilty purpose (like having some
text underlined or different font,color) I tried using jlables with
html text for line wrapping and it works fine as long as I specify the
preferred size of the label such that height is set properly for
displaying multiple rows. Is there anyway I can do this without
specifying the height so that if text grows so does the height of
jlable automatically?

[..skip example...]
Any help is appreciated.

Thanks.

Don't forget to call pack() to tell the LayouyManager to layout
ALL components and subcomponents. Normally a JLabel shows
text of every length correctly, because it knows the
width and height of the text due to the font settings. But
as I said, you must give the chance to the label to call its
size to the layout manager, this is done by calling pack()
before setVisible( true ).

Bart
 
A

Andrey Kuznetsov

Should not be neccessary. If changing a text in a JLabel, it
is automatically updated and repainted.

but you must tell LayoutManager that something has changed.

Andrey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top