Flowlayout in a vertical fashion

Discussion in 'Java' started by sanmits@gmail.com, Jul 7, 2006.

  1. Guest

    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.
    , Jul 7, 2006
    #1
    1. Advertising

  2. Andrew T. Guest

    wrote:
    > 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.
    Andrew T., Jul 7, 2006
    #2
    1. Advertising

  3. > 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

    --
    http://uio.imagero.com Unified I/O for Java
    http://reader.imagero.com Java image reader
    http://jgui.imagero.com Java GUI components and utilities
    Andrey Kuznetsov, Jul 8, 2006
    #3
  4. Bart Rider Guest

    Andrey Kuznetsov wrote:
    >>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
    >


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

    Bart
    Bart Rider, Jul 11, 2006
    #4
  5. Bart Rider Guest

    wrote:
    >
    > 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
    Bart Rider, Jul 11, 2006
    #5
  6. Andrey Kuznetsov, Jul 12, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen

    flowlayout vs. gridlayout

    Stephen, Dec 5, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    5,918
    mikeb
    Dec 5, 2003
  2. dee
    Replies:
    2
    Views:
    2,907
  3. Replies:
    1
    Views:
    3,796
    JIMCO Software
    Sep 9, 2005
  4. The Crow

    fixed layout or flowlayout ?

    The Crow, Nov 23, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    429
    Eliyahu Goldin
    Nov 23, 2005
  5. Ulf Meinhardt
    Replies:
    0
    Views:
    456
    Ulf Meinhardt
    Nov 12, 2005
Loading...

Share This Page