Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #2577 > unrolled thread

JTextArea size problem.

Started by"RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this>
First post2011-04-27 15:39 +0000
Last post2011-04-27 15:40 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.java.gui


Contents

  JTextArea size problem. "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this> - 2011-04-27 15:39 +0000
    Re: JTextArea size proble "Larry Barowski" <larry.barowski@THRWHITE.remove-dii-this> - 2011-04-27 15:39 +0000
    Re: JTextArea size proble "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000

#2577 — JTextArea size problem.

From"RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this>
Date2011-04-27 15:39 +0000
SubjectJTextArea size problem.
Message-ID<470376e5$0$13927$fa0fcedb@news.zen.co.uk>
  To: comp.lang.java.gui
In a dialogue box, I want to display some text word-wrapped. I use a 
JTextArea and set word wrapping on.

However, the height of the dialogue seems to be incorrectly based on the 
Textarea being one line high, (it is two lines high when wrapped).

The println statements show
   Pref  java.awt.Dimension[width52,height]
   Wrap set
   Pref  java.awt.Dimension[width30,height]
Note that the width is reduced but the height is wrong.
   Packed
   Size  java.awt.Dimension[width30,height]
No change due to pack()
   Size  java.awt.Dimension[width30,height2]
But after closing, the height is correct (32)!

Qs:
Am I doing something wrong?
Is there a better way to word-wrap some text in a dialogue?

------------------------- 8< ----------------------
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class TestErrorDialog {

     public static void main(String[] args) {
         SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                 new TestErrorDialog();
             }
         });
     }

     private JFrame f;

     TestErrorDialog() {
         JPanel p = new JPanel();
         p.add(new JLabel("Testing ..."));

         f = new JFrame("Test Error Dialog");
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         f.add(p);
         f.pack();
         f.setVisible(true);
         triggerAnError();
     }

     public void triggerAnError() {
         System.out.println("Error triggered");
         new ErrorDialog(f, "Unable to unwind flanges!",
                 "Please contact the help desk "
                 + "and tell them absolutely everything.");
         System.exit(0);
     }
}

// =================================================================
class ErrorDialog extends JDialog implements ActionListener {

     JTextArea messageComponent = new JTextArea(1, 30);

     static final String CLOSE = "Close";

     public ErrorDialog(JFrame parent, String title, String message) {
         super(parent, "Error", true);

         System.out.println("Constructing dialog");

         messageComponent.setText(message);
         messageComponent.setEditable(false);
         System.out.println("Pref  "
                 + messageComponent.getPreferredSize());
         messageComponent.setLineWrap(true);
         messageComponent.setWrapStyleWord(true);
         System.out.println("Wrap set");
         System.out.println("Pref  "
                 + messageComponent.getPreferredSize());

         JButton closeButton = new JButton(CLOSE);
         closeButton.addActionListener(this);

         JLabel titleLabel = new JLabel("<html><body><h2>" + title
                 + "</h2></body></html>");

         JPanel innerPane = new JPanel();
         innerPane.setLayout(
                 new BoxLayout(innerPane, BoxLayout.PAGE_AXIS));
         innerPane.setBorder(
                 BorderFactory.createEmptyBorder(10,10,10,10));
         for (JComponent component : new JComponent[] { titleLabel,
                 messageComponent, closeButton })
             component.setAlignmentX(Component.LEFT_ALIGNMENT);
         innerPane.add(titleLabel);
         innerPane.add(Box.createRigidArea(new Dimension(0, 5)));
         innerPane.add(messageComponent);
         innerPane.add(Box.createRigidArea(new Dimension(0, 5)));
         innerPane.add(closeButton);
         add(innerPane);

         getRootPane().setDefaultButton(closeButton);
         setLocationRelativeTo(parent);
         pack();
         System.out.println("Packed");
         System.out.println("Size  " + messageComponent.getSize());
         setVisible(true);
         System.out.println("Size  " + messageComponent.getSize());
     }

     public void actionPerformed(ActionEvent e) {
             setVisible(false);
     }
} // ErrorDialog
---------------------------- 8< -------------------------

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#2582 — Re: JTextArea size proble

From"Larry Barowski" <larry.barowski@THRWHITE.remove-dii-this>
Date2011-04-27 15:39 +0000
SubjectRe: JTextArea size proble
Message-ID<ftKdnZ040MnVIZ7anZ2dnUVZ_o6knZ2d@comcast.com>
In reply to#2577
  To: comp.lang.java.gui

"RedGrittyBrick" <redgrittybrick@spamweary.foo> wrote in message 
news:470376e5$0$13927$fa0fcedb@news.zen.co.uk...
> In a dialogue box, I want to display some text word-wrapped. I use a 
> JTextArea and set word wrapping on.
>
> However, the height of the dialogue seems to be incorrectly based on the 
> Textarea being one line high, (it is two lines high when wrapped).

During layout, the text area doesn't know what its eventual width will
be, so it has no way to provide a useful preferred height. It's a problem
for any component for which the preferred size in one dimension
depends on the actual size in the other. This might be good enough to
solve your problem (replacement for pack()):

  pack();
  messageComponent.setSize(messageComponent.getPreferredSize());
  pack();

You could write your own word-wrapping label class from scratch,
but there is no way to solve the basic problem of height/width
dependence. A double pack(), at least, would still be required.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2590 — Re: JTextArea size proble

From"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
SubjectRe: JTextArea size proble
Message-ID<dve8g3t3iblcclse4rmrjvnfjkh7919mp8@4ax.com>
In reply to#2577
  To: comp.lang.java.gui
>However, the height of the dialogue seems to be incorrectly based on the
>Textarea being one line high, (it is two lines high when wrapped).

Use the row,columns feature in the TextArea constructor.  If that does
not work use the component-expanding features of the GridBagLayout.
Make sure the you have recently validated after changing the size of
the TextArea.
-- 
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web