JTextArea y ajuste de línea (palabra)

I have a JPanel(a) with box layout, and all the components are stacked on it vertically. Among them, I have a JPanel(b) with horizontal box layout. To that JPanel(b) I have added rigid area, JPanel and JTextArea.

What I want is that JPanel(b) increases its height every time JTextArea expands due to word wrap. However, since my JPanel in beginning has the height of a single row. JTextArea doesn't expand because all space is filled.

Is there a way to fix this, an alternative?

It doesn't have to be JPanel and JTextArea, just something that can contain components and a JTextComponent that suporrts multi line.

class Question extends JPanel
public JPanel questionArea;
public JTextArea number, question;

public Question(Page page)
    setSize(new Dimension(556, 100));
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

    Border in = BorderFactory.createDashedBorder(Color.BLACK);
    Border out = BorderFactory.createMatteBorder(0, 0, 10, 0, Color.WHITE);
    setBorder(BorderFactory.createCompoundBorder(out, in));

    questionArea = new JPanel();
    questionArea.setPreferredSize(new Dimension(556, 32));
    questionArea.setLayout(new BoxLayout(questionArea, BoxLayout.LINE_AXIS));

    out = BorderFactory.createMatteBorder(0, 0, 8, 0, Color.WHITE);
    setBorder(BorderFactory.createCompoundBorder(out, in));

    number = new JTextArea();
    number.setPreferredSize(new Dimension(25, 32));
    number.setFont(new Font("Arial", Font.BOLD, 15));

    question = new JTextArea();
    question.setPreferredSize(new Dimension(494, 32));
    question.setFont(new Font("Arial", Font.BOLD, 15));
    question.setText("What is the first question?");

    questionArea.add(Box.createRigidArea(new Dimension(35, 32)));


    page.editArea.add(this, page.content);


class Page extends JPanel
public JPanel editArea;
public Box.Filler blank;

public Page(JPanel panel)
    setBounds(92, panel.getPreferredSize().height+40, 794, 1123);


    editArea = new JPanel();
    editArea.setLayout(new BoxLayout(editArea, BoxLayout.PAGE_AXIS));
    editArea.setBounds(119, 96, 556, 931);

    blank = new Box.Filler(new Dimension(556, -1), new Dimension(556, 931), new Dimension(556, 931));



Page class is itself on a JPanel with null layout, no need for code, right?

preguntado el 28 de agosto de 12 a las 10:08

Have you set LineWrap & WrapStyleWord to true? -

hmm .. looks like don't get it, so repeating: show an SSCCE that demonstrates what you are doing and how it doesn't live up to your expectations -

use null-layout and suffer ... ;-) Seriously: null layout is a nononeverever in Swing, there's a good probability that it alone already is the reason for the perceived misbehaviour. Didn't check further, though, because the code is a mere snippet instead of a SSCCE (google is your friend if you don't know the abbr) -

and another nononever is to call setXXSize: doing so short-circuits cualquier internal calculation of sizing hints. -

usted puede siempre create an SSCCE - but shrugs, if you don't want help why ask? -

1 Respuestas

I think horizontal BoxLayout is doing you in.

It will display components at their preferred size (using getPreferredSize() upon instantiation) and I don't believe it will readily resize it for you. You might want to change Panel(b) to a BorderLayout, and add the components to the EAST, WEST, y la JTextArea in CENTER.

Respondido 28 ago 12, 10:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.