How to fix size of JTextPane to its content?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 05:19:34

问题


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 and getMaximumSize to make them return getPreferredSize:

            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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!