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 wou
Use a JScrollPane
with no horizontal scroll.
JFrame frame = new JFrame();
JPanel pnel = new JPanel();
frame.setContentPane(pnel);
JTextPane txtpane = new JTextPane();
txtpane.setPreferredSize(new Dimension(200, 200));
JScrollPane jsp = new JScrollPane(txtpane,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
pnel.add(jsp);
frame.pack();
frame.setVisible(true);
This will not wrap text. To wrap text take a look at these: http://java-sl.com/wrap.html , http://java-sl.com/tip_html_letter_wrap.html
I will suggest to use JTextArea
not JTextPane
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);
}
});
}
}