问题
I have JPanel
with BoxLayout
.
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS){
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public Dimension minimumLayoutSize(Container target){
return new Dimension(100, 25);
}
@Override
public Dimension maximumLayoutSize(Container target){
return new Dimension(100,25);
}
});
I add some JTextPane
and JPanel
to it dynamically.
I call the method below to add new JTextPane
to my jpanel and pass the text to it.
public void display(final String text){
StyleContext sc = new StyleContext();
final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
final Style style = sc.addStyle("MainStyle", defaultStyle);
JTextPane pane = new JTextPane(){
/**
*
*/
private static final long serialVersionUID = 1L;
@Override // how should I write this:
public Dimension getMinimumSize(){
return new Dimension(text.length()*5, getContentHeight(text.length()*5,text));
}
@Override // how should I write this:
public Dimension getMaximumSize(){
return new Dimension(430, getContentHeight(text.length()*5,text));
}
};
receive.setStyledDocument(doc);
receive.setEditable(false);
// how should I write this:
receive.setPreferredSize(new Dimension(text.length()*5, getContentHeight(text.length()*5,text)));
try {
doc.insertString(doc.getLength(),text,style);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
panel.add(pane);
}
so the content won't change.
public int getContentHeight(int i, String content) {
JEditorPane dummyEditorPane=new JEditorPane();
dummyEditorPane.setSize(i,Short.MAX_VALUE);
dummyEditorPane.setText(content);
return dummyEditorPane.getPreferredSize().height;
}
The issue is in getMinimumSize()
and getMaximumSize()
and setPreferredSize()
method!
Also in width that I set for dummyEditorPane.setSize(i,Short.MAX_VALUE);
So How can I set this method to fix size for textpane?
回答1:
Use setPreferredSize
but maybe you might benefit from the normal usage of a JScrollPane:
panel.add(new JScrollPane(pane));
The JTextPane will adapt to its document size, and the JScrollPane takes further care of scrollbars when needed.
回答2:
getPreferredSize()
already returns the size of the content. So the solution is just to not use setPreferredSize
, but use
jtp.setSize(jtp.getWidth(),jtp.getPreferredSize().height);//For fixed width
or
jtp.setSize(jtp.getPreferredSize());//for unlimited width and no wrapping
This will set the height of the JTextPane to match its content.
To do this with a BoxLayout, you might want to
extends your JTextPane and override
getMinimumSize
andgetMaximumSize
to make them returngetPreferredSize
:final JTextPane myJTP = new JTextPane() { @Override public Dimension getMinimumSize() { return getPreferredSize(); } @Override public Dimension getMaximumSize() { return getPreferredSize(); } };
or extends BoxLayout to make the
layoutContainer
method set the correct size to its children.
来源:https://stackoverflow.com/questions/25181929/how-to-fix-size-of-jtextpane-to-its-content