Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

java - How to set fixed width but dynamic height on JTextPane?

I want to use a JTextpane with fixed width but dynamic height which should also allow wrapping. The height should change as the user adds or removes text. I would've used a JTextArea but the text has to be styled differently. Is there an easy way to go about this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I already wrote an answer for something similar. Check here.

You have to understand that the preferredSize returned by the JTextPane is:

  • if the width is not set, getPreferredSize returns Dimension(width,height) where width would be the width of the longest line if there where no wrapping, and height, the height needed to display all the lines in a pane with infinite width.

  • if the width is set, getPrefferedSize returns Dimension(width,height) where width would be the width of the longest line if there where no wrapping, and height is the height needed to display the whole text if it was wrapped at the current width.

Why this behaviour ?

This is very simple. You can't calculate the height needed to represent a text if you don't know the width you can use.

How to do it dynamically?

The best would be to use add DocumentListener to your JTextPane's Document. Then, on every change, just call

myJTextPane.setSize(myJTextPane.getWidth(),myJtextPane.getPreferredSize().height);

To illustrate

Have fun with this code:

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;

public class HelloWorld {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final JFrame mainFrame = new JFrame("test");
                mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                final JTextPane field = new JTextPane();
                field.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse accumsan magna vel libero bibendum, quis hendrerit nisi rutrum. Cras placerat erat eget dictum ornare. Sed eget nisl quis nibh vehicula mollis. Vestibulum non iaculis erat, quis pulvinar magna. Suspendisse ac rhoncus purus. Quisque finibus, dolor varius tincidunt aliquet, mauris felis condimentum neque, at viverra felis nulla at justo. Duis ut dui velit. Integer vitae mollis leo. Cras quis urna odio. Suspendisse tempus, urna sed maximus fringilla, ante velit finibus massa, id commodo libero quam non ipsum. Sed id augue vitae sapien sagittis imperdiet in eget nibh. Nam semper posuere nisl, dictum efficitur ipsum aliquet ac. Phasellus eros massa, fringilla et neque maximus, pretium tempor magna.");

                mainFrame.getContentPane().setLayout(null);
                mainFrame.getContentPane().add(field,BorderLayout.CENTER);
                field.setLocation(0, 0);
                field.setSize(200,40);
                field.setSize(200, field.getPreferredSize().height);
                mainFrame.setSize(300,500);
                mainFrame.setVisible(true);
                System.out.println(field.getPreferredSize().width+" , "+field.getPreferredSize().height);
            }
        });
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...