1

I am looking for a way to do the following:

Have a JDialog with a fixed width.

In it is a JTextArea (or whatever you suggest is a better component...) which receives a text of varying length (somewhere between 0 and 30 line)

Below that text is a button.

The dialog is automatically sized in height to make sure all the Text AND the button is being displayed.

The closest I have come to a solution is this, but the JTextArea does not seem to know how large it is after it did the automatic line breaks!

    public PleaseResize(){
    super();

    Container cp = this.getContentPane();
    cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));

    JTextArea area = new JTextArea();

    area.setColumns(20);
    area.setLineWrap(true);
    area.setEditable(false);
    area.setWrapStyleWord(true);
    area.setText("Once upon a midnight dreary, while I pondered, weak and weary, over many a quaint an curious volume of forgotten lore.");


    cp.add(area);

    cp.add(new JButton("Hallo"));


    this.pack();

}

Scrolling the Text is unfortunately not an option.

I have asked this is a slightly different way before here: Resize Dialog properly after automatic LineWrap, but perhaps the JTextArea is the wrong component after all? Am I using the wrong LayoutManager? All of them seem unable to determine how large the dialog should be, though. Why does the JTextArea fail to communicate it's height after adding line-breaks to the text to the outside?


Here is the working code:

    public PleaseResize() throws BadLocationException {
    super();

    Container cp = this.getContentPane();
    cp.setLayout(new BorderLayout());

    JTextArea area = new JTextArea();
    area.setColumns(20);
    area.setLineWrap(true);
    area.setEditable(false);
    area.setWrapStyleWord(true);
    area.setText("Once upon a midnight dreary, while I pondered, weak and weary, over many a quaint an curious volume of forgotten lore.");
    System.out.println(area.getLineCount());

    JScrollPane pane = new JScrollPane(area);
    cp.add(pane, BorderLayout.CENTER);
    cp.add(new JButton("Hallo"), BorderLayout.SOUTH);

    this.pack();
    this.pack();


}

Packing twice still seems a bit weird to me, but it does solve the resizing problems :).

Community
  • 1
  • 1
Layna
  • 457
  • 5
  • 19
  • I'm miss there JScrollPane, rest in Oracle trails how to use JTextArea/JScrollPane – mKorbel Nov 03 '14 at 09:26
  • Use an HTML aware component and CSS to limit the width. The component will be as tall as it needs to be. See [this answer](http://stackoverflow.com/a/5767825/418556) for demo. – Andrew Thompson Nov 03 '14 at 09:34
  • I am just now seriously doubting my sanity: as the fixed code above shows, Ezequiel's code does not seem to do anything, as it works with just calling pack() TWICE instead of once... is this a Java-bug, something I should technically not be doing, or just how it works? – Layna Nov 03 '14 at 11:27
  • The code DOES seem necessary for the actual case I needed this form though! (a slightly more complex dialog with some more JPanels and JLabels pushed into it). I love having a solution, but this still confuses me to no end... -_- – Layna Nov 03 '14 at 11:39

1 Answers1

3

Try this aproach to resize JTextArea based in its model.

        Rectangle modelToView = area.modelToView(area.getDocument().getLength());
        if (modelToView != null) {
            area.setSize(area.getWidth(), Math.max(area.getHeight(), modelToView.height + modelToView.y));
            area.doLayout();
        }
Ezequiel
  • 3,477
  • 1
  • 20
  • 28
  • This does look promising, but I am getting a null for the modelToView method. The Javadoc states "layout cannot be computed until the component has been sized". I suspect that might be the problem, but doesn't that toss me back to the original problem: figuring out what size the JTextArea is? – Layna Nov 03 '14 at 09:35
  • Try to use these lines after you pack the jframe. – Ezequiel Nov 03 '14 at 09:37
  • Now it works... the key was to to pack(), THEN use the code, and then pack AGAIN. So much packing, but it works now! Thanks :) – Layna Nov 03 '14 at 09:42