JTextPane - fit size to its content

Iaroslav Baranov

What I have -

What I need -

Code:

JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextPane pane = new JTextPane();
pane.setText("Long string Long string Long string Long string Long string Long string");
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(pane);
panel.add(new JButton("Button"));
frame.add(panel);
frame.show();

If I do frame.pack(), I get very long window without word wrap!

Andrew Thompson

For any HTML aware Swing component, width can be set for the body using HTML styles (CSS). This in turn will determine the number of lines to render and, from that, the preferred height of the text component. Setting the width in CSS avoids the need to compute where line breaks should occur in (or the best size of) the component.

More generally, see How to Use HTML in Swing Components.

E.G.

import javax.swing.*;

public class FixedWidthLabel {

    public static void main(String[] srgs) {
        final String s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eu nulla urna. Donec sit amet risus nisl, a porta enim. Quisque luctus, ligula eu scelerisque gravida, tellus quam vestibulum urna, ut aliquet sapien purus sed erat. Pellentesque consequat vehicula magna, eu aliquam magna interdum porttitor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed sollicitudin sapien non leo tempus lobortis. Morbi semper auctor ipsum, a semper quam elementum a. Aliquam eget sem metus.";
        final String html1 = "<html><body style='width: ";
        final String html2 = "px'>";

        Runnable r = new Runnable() {

            @Override
            public void run() {
                JOptionPane.showMessageDialog(
                        null, new JLabel(html1 + "200" + html2 + s));
                JOptionPane.showMessageDialog(
                        null, new JLabel(html1 + "300" + html2 + s));
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

enter image description here enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

fit width of <td> to its content

From Dev

UITableView to fit its dynamic content?

From Dev

How do you get a container (ie grid) to fit the rotated size of its content?

From Dev

How to have a UIScrollView fit its content size, up until a max before scrolling in autolayout

From Dev

How do you get a container (ie grid) to fit the rotated size of its content?

From Dev

Javafx - Fit ScrollPane Size to content

From Dev

Makingthe columns of a tableview fit the size of its contents

From Dev

Makingthe columns of a tableview fit the size of its contents

From Dev

How to resize UITableView width to fit its content?

From Dev

Scale div with its content to fit window

From Dev

Fit UIView height to its content with AutoLayout

From Dev

How to resize UITableViewCell to fit its content?

From Dev

How to fit DataGridView width and height to its content?

From Dev

Resizing UIScrollView to fit its content… not working

From Dev

HTML <object> fit height of its content

From Dev

JavaScript table to shrink to fit its content

From Dev

How to fit label according to its content in UItableView

From Dev

card-content on materialize not fit in its class

From Java

Get the size of a directory (not its content)

From Dev

Make a div the size of its content

From Dev

A TableView that "fits" (ala "sizeToFit") its dynamic cell's content size. Then nested in a parent UIView that is also "sized to fit"

From Dev

How to make Surface size to fit text content

From Dev

Stretch a div to fit content in all screen size

From Dev

DataGrid column size width fit to cell content

From Dev

Java JTextpane Tab Size

From Dev

Java JTextpane Tab Size

From Dev

Adjust size of an SVG-element to its content

From Java

How do I size a UITextView to its content?

From Dev

How to make the tabs of QTabWidget fit the size of the container - Adjust its lenght?

Related Related

  1. 1

    fit width of <td> to its content

  2. 2

    UITableView to fit its dynamic content?

  3. 3

    How do you get a container (ie grid) to fit the rotated size of its content?

  4. 4

    How to have a UIScrollView fit its content size, up until a max before scrolling in autolayout

  5. 5

    How do you get a container (ie grid) to fit the rotated size of its content?

  6. 6

    Javafx - Fit ScrollPane Size to content

  7. 7

    Makingthe columns of a tableview fit the size of its contents

  8. 8

    Makingthe columns of a tableview fit the size of its contents

  9. 9

    How to resize UITableView width to fit its content?

  10. 10

    Scale div with its content to fit window

  11. 11

    Fit UIView height to its content with AutoLayout

  12. 12

    How to resize UITableViewCell to fit its content?

  13. 13

    How to fit DataGridView width and height to its content?

  14. 14

    Resizing UIScrollView to fit its content… not working

  15. 15

    HTML <object> fit height of its content

  16. 16

    JavaScript table to shrink to fit its content

  17. 17

    How to fit label according to its content in UItableView

  18. 18

    card-content on materialize not fit in its class

  19. 19

    Get the size of a directory (not its content)

  20. 20

    Make a div the size of its content

  21. 21

    A TableView that "fits" (ala "sizeToFit") its dynamic cell's content size. Then nested in a parent UIView that is also "sized to fit"

  22. 22

    How to make Surface size to fit text content

  23. 23

    Stretch a div to fit content in all screen size

  24. 24

    DataGrid column size width fit to cell content

  25. 25

    Java JTextpane Tab Size

  26. 26

    Java JTextpane Tab Size

  27. 27

    Adjust size of an SVG-element to its content

  28. 28

    How do I size a UITextView to its content?

  29. 29

    How to make the tabs of QTabWidget fit the size of the container - Adjust its lenght?

HotTag

Archive