Path: csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!news.glorb.com!news-out.readnews.com!transit3.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!86597e80!not-for-mail From: "Larry Barowski" Subject: Re: JTextArea size proble Message-ID: X-Comment-To: comp.lang.java.gui Newsgroups: comp.lang.java.gui In-Reply-To: References: Content-Type: text/plain; charset=IBM437 Content-Transfer-Encoding: 8bit X-Gateway: time.synchro.net [Synchronet 3.15a-Win32 NewsLink 1.92] Lines: 77 Date: Wed, 27 Apr 2011 15:40:09 GMT NNTP-Posting-Host: 96.60.20.240 X-Complaints-To: news@tds.net X-Trace: newsreading01.news.tds.net 1303918809 96.60.20.240 (Wed, 27 Apr 2011 10:40:09 CDT) NNTP-Posting-Date: Wed, 27 Apr 2011 10:40:09 CDT Organization: TDS.net Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.gui:2616 To: comp.lang.java.gui "Knute Johnson" wrote in message news:HohPi.47$Bu.20@newsfe17.lga... > Larry Barowski wrote: >> "Knute Johnson" wrote in message >> news:J0hOi.159$TF.70@newsfe13.lga... >>> I also included an example that works just fine with a JTextArea. >> >> Of course you can get it to work if you know ahead of time >> how many lines the wrapped text will require. That is unlikely >> in a real-world situation, and was not the case in the example >> in the OP. >> >> > > He said it was two lines. The error message was passed in as a parameter to the error dialog constructor, so it would be reasonable to assume he wanted a solution that works for any length of text. Besides, unless the number of rows are fixed and the width flexible or the text is one character long, the number of wrapped lines for any text in any component that wraps text in any gui layout is always system dependent in principle. > If you want it to do any number of lines then you have to call pack() > again after you know how many lines it will take. I have already posted two solutions that are equivalent to the one below. > import java.awt.*; > import java.awt.event.*; > import javax.swing.*; > > public class test10 { > public static void main(String[] args) { > Runnable r = new Runnable() { > public void run() { > final JFrame f = new JFrame(); > f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); > JButton b = new JButton("press me one more time"); > b.addActionListener(new ActionListener() { > public void actionPerformed(ActionEvent ae) { > JDialog d = new JDialog(f,true); > JTextArea ta = new JTextArea( > "Enough text to wrap and be visible on two > lines"); > ta.setColumns(15); > ta.setLineWrap(true); > d.add(ta); > d.pack(); > ta.setRows(ta.getLineCount()); > d.pack(); > d.setLocationRelativeTo(f); > d.setVisible(true); > d.dispose(); > } > }); > f.add(b); > f.pack(); > f.setVisible(true); > } > }; > EventQueue.invokeLater(r); > } > } > > -- > > Knute Johnson > email s/nospam/knute/ --- * 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