JTextArea y ajuste de línea (palabra)
Frecuentes
Visto 382 veces
-2
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));
setBackground(Color.PINK);
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.setBackground(Color.YELLOW);
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.setBackground(Color.GREEN);
number.setFont(new Font("Arial", Font.BOLD, 15));
number.setText("10.");
number.setEditable(false);
question = new JTextArea();
question.setPreferredSize(new Dimension(494, 32));
question.setBackground(Color.PINK);
question.setFont(new Font("Arial", Font.BOLD, 15));
question.setText("What is the first question?");
questionArea.add(Box.createRigidArea(new Dimension(35, 32)));
questionArea.add(number);
questionArea.add(question);
add(questionArea);
page.editArea.add(this, page.content);
}
}
romper
class Page extends JPanel
{
public JPanel editArea;
public Box.Filler blank;
public Page(JPanel panel)
{
setLayout(null);
setBounds(92, panel.getPreferredSize().height+40, 794, 1123);
setBackground(Color.WHITE);
editArea = new JPanel();
editArea.setLayout(new BoxLayout(editArea, BoxLayout.PAGE_AXIS));
editArea.setBounds(119, 96, 556, 931);
editArea.setBackground(Color.LIGHT_GRAY);
blank = new Box.Filler(new Dimension(556, -1), new Dimension(556, 931), new Dimension(556, 931));
editArea.add(blank);
add(editArea);
}
}
Page class is itself on a JPanel with null layout, no need for code, right?
1 Respuestas
0
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
Have you set LineWrap & WrapStyleWord to true? - MadProgrammer
hmm .. looks like tí 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 - kleopatra
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) - kleopatra
and another nononever is to call setXXSize: doing so short-circuits cualquier internal calculation of sizing hints. - kleopatra
usted puede siempre create an SSCCE - but shrugs, if you don't want help why ask? - kleopatra