java - Get height of multi line text with fixed width to make dialog resize properly -


I want to create a dialog that has some kind of text element (JLabel / JTextArea etc.) which is multi-queued and wrapped word. I want the dialog to be of a certain width, but the height should be adjusted how big is the text I have this code:

  import static javax.swing.GroupLayout.DEFAULT_SIZE; Import java.awt.event.ActionEvent; Import java.awt.event.ActionListener; Import javax.swing *; Public class text size problem increases JFrame {public text size problem (string) {string dummies = ""; For (int i = 0; i <100; i ++) {dmestress + = "word" + i; // Create a longer text JLabel text = new jlabel (); Text.setText ("& lt; html>" + Dummy Staster + ""); Jebton PackMetton = New Pocket ("Pack"); PackMeButton.addActionListener (New Executioner) {Public Zero Action Action (Action Event e) {Pack ();}}); Group Layout Layout = New Group Layout (this.getContentPane ()); GetContentPane () setLayout (layout). Layout.setVerticalGroup (layout.createParallelGroup () .addComponent (packMeButton) .addComponent (text)); Layout.setHorizontalGroup (layout.createSequentialGroup) .addComponent (packMeButton) .addComponent (text, DEFAULT_SIZE, 400, 400) // width width 400); Pack (); } Public Static Zero Main (String Exams []) {Swing-Utilities.Vaculator (New Runnabal) (Public Zero Free) {Jeffre Frame = New Text Size Problem; Frame.set DefaultCallionation (JFrame.EXIT_ON_CLOSE); Frames.Sable (true);}}); }}  

While running the program it looks like this:

But I would like to see this kind of dialogue (as you press the pack-button):

I am feeling that the problem is that Layout Manager was not able to determine the proper height of the text before it was displayed on the screen. I have tried many assumptions (), invalid (), valid () etc. but have not succeeded.

This is the optimization of your code, which you want but calculate the size of the label and its favorites To set the size it needs a little trick.

  Import static javax.swing.GroupLayout.DEFAULT_SIZE; Import java.awt.Dimension; Import java.awt.event.ActionEvent; Import java.awt.event.ActionListener; Import javax.swing *; Import javax.swing.text.View; Public class text size problem increases JFrame {public text size problem (string) {string dummies = ""; For (int i = 0; i <100; i ++) {dmestress + = "word" + i; // Create a longer text JLabel text = new jlabel (); Text.setText ("& lt; html>" + Dummy Staster + ""); Dimension prefSize = getPreferredSize (text.getText (), true, 400); Jebton PackMetton = New Pocket ("Pack"); PackMeButton.addActionListener (New Executioner) {Public Zero Action Action (Action Event e) {Pack ();}}); Group Layout Layout = New Group Layout (this.getContentPane ()); GetContentPane () setLayout (layout). Layout.Cetetic group (layout.cotateparlegup (). AddComponent (packet button) .addComponent (text, default T_SIZE, prefix .high, prefixes hiight)); Layout.Convertory group (layout.create time group). AddComponent (packet button) .addComponent (text, default T_SIZE, prefaces, width, prefix, width) // width width 400); Pack (); } Public Static Zero Main (String Exams []) {Swing-Utilities.Vaculator (New Runnabal) (Public Zero Free) {Jeffre Frame = New Text Size Problem; Frame.set DefaultCallionation (JFrame.EXIT_ON_CLOSE); Frames.Sable (true);}}); } Private stable final JLabel resizer = new JLab (); / ** * Returns the preferred size to set a component to render HTML Strander. You can specify the size of a dimension * / public static java.awt.Demension getPreferredSize (string HTML, boolean width, int pulse) {resizer.setText (html); View View = (see) resizer.getClientProperty (javax.swing.plaf.basic.BasicHTML.propertyKey); View.setSize (width? PrefSize: 0, width? 0: prefixes); Float w = see.jetprovedspain (see.x_xis); Float h = view.jetprofedspain (see.ਵਾਈ_adis); Return new Java.Wave.Emment ((int) Math CCL (W), (Int.) Math. Cecil (H)); }}  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -