开发者

JTextarea with dynamic text length and wrapping in BoxLayout wrong height

开发者 https://www.devze.com 2023-03-31 06:02 出处:网络
I\'m trying to have multiline labels and image labels in a veritcal BoxLayout. For the multiline labels I use a JTextArea with setEditable(false). For the image labels I use a JLabel([ImageIcon]).

I'm trying to have multiline labels and image labels in a veritcal BoxLayout. For the multiline labels I use a JTextArea with setEditable(false). For the image labels I use a JLabel([ImageIcon]).

The following code shows that the textarea has a lot of space below it and I don't want that. To keep it simple I added text labels instead of image labels.

What I want is to stack the textarea and the labels from top to bottom. After each textarea the label should follow immediately below and after the last label there should be empty space up to the bottom of the window.

Maybe another Layout Manager is better, but I think it is a JTextArea issue. Any solution would help.

Thanks.

here is the compilable code:

import java.awt.Color;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

public class BoxLay extends JFrame
{
private static final long serialVersionUID = 1L;

public static void main(final String[] args)
{
    new BoxLay();

}

private BoxLay()
{
    setTitle("BoxLayout TestDummy");
    setSize(800, 450);
    setResizable(true);
    setVisible(true);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    this.getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));

    final JTextArea area1 = new JTextArea();
    area1.setText("First Text - Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... ");
    area1.setLineWrap(true);
    area1.setW开发者_Go百科rapStyleWord(true);
    area1.setEditable(false);
    area1.setBackground(Color.RED);
    this.add(area1);

    final JLabel label1 = new JLabel("DIRECTLY BELOW FIRST TEXT");
    this.add(label1);

    final JTextArea area2 = new JTextArea();
    area2.setText("Second Text - Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... ");
    area2.setLineWrap(true);
    area2.setWrapStyleWord(true);
    area2.setEditable(false);
    area2.setBackground(Color.RED);
    this.add(area2);

    final JLabel label2 = new JLabel("DIRECTLY BELOW SECOND TEXT");
    this.add(label2);

    this.add(Box.createVerticalGlue());

    this.getContentPane().invalidate();
    this.getContentPane().validate();

}
}


Finally wrote my own LayoutManager (dejavu!). It simply stacks components from top to bottom and does not try to make them fit the whole screen. It's a prototype, though, and X_AXIS is not supported. Probably a lot can be improved, but maybe it helps someone. I would also be very happy, if someone can improve it an share it with me, because I'm still a beginner regarding LayoutManagers.

Anyway, here is the code:

public class StackLayout implements LayoutManager

{

static final int  X_AXIS = 0;
static final int  Y_AXIS = 1;

private final int axis   = Y_AXIS;

public void addLayoutComponent(final String name, final Component comp)
{

}

public void removeLayoutComponent(final Component comp)
{

}

public Dimension preferredLayoutSize(final Container parent)
{

    return new Dimension(200, 200);

}

public Dimension minimumLayoutSize(final Container parent)
{

    return new Dimension(0, 0);

}

public void layoutContainer(final Container parent)
{
    //TODO implement x axis layout

    final Dimension size = parent.getSize();
    if (axis == Y_AXIS)
    {

        final int left = 0;

        int top = 0;

        for (int i = 0; i < parent.getComponentCount(); ++i)
        {
            final Component component = parent.getComponent(i);

            component.setSize(parent.getSize().width, 1);

            final Dimension preferredSize = component.getPreferredSize();

            component.setBounds(left, top, Math.max(preferredSize.width, parent.getSize().width), preferredSize.height);

            top += preferredSize.height;
        }
        parent.setPreferredSize(new Dimension(parent.getSize().width, top));
    }

}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消