3

Is there a possible way to get the height of an entered text in a JTextPane? Pixels would be great but any value that can represent it would do. For example I have entered in the JTextPane this:

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Is there a way to get a value for how long it is(not how many lines it is)? I need this because if I enter a picture in one of the lines, the total height changes and I want to be able to know how much it changes exactly. The JTextPane, I use has a StyledDocument and doesn't have html. I made an SSCCE but instead of an image I used two different font sizes to show the difference between the height of the 2 JTextPanes.

public class Fonts extends JFrame {

    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Fonts frame = new Fonts();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Fonts() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JTextPane textPane = new JTextPane();
        textPane.setEditable(false);
        textPane.setFont(new Font("Tahoma", Font.PLAIN, 11));
        StyledDocument doc = textPane.getStyledDocument();
        JTextPane textPane_1 = new JTextPane();
        textPane_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
        textPane_1.setEditable(false);

        StyledDocument doc1 = textPane_1.getStyledDocument();
        String s = "Lorem Ipsum is simply dummy text of the printing and typesetting industry." +
                " Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown" +
                " printer took a galley of type and scrambled it to make a type specimen book. It has survived not" +
                " only five centuries, but also the ";
        try {
            doc.insertString(0, s, null);
            doc1.insertString(0, s, null);
        } catch (BadLocationException e) {
            e.printStackTrace();
        }

        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addComponent(textPane, GroupLayout.PREFERRED_SIZE, 214, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addComponent(textPane_1, GroupLayout.PREFERRED_SIZE, 208, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        gl_contentPane.setVerticalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
                        .addComponent(textPane_1, Alignment.LEADING)
                        .addComponent(textPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE))
                    .addContainerGap())
        );
        contentPane.setLayout(gl_contentPane);
    }
}

So is there a way to get the height of the 2 JTextPanes?

4

1 回答 1

5

Use the approach described here to measure the JEditorPane/JTextPane's height of content for fixed width.

于 2012-12-26T13:03:32.927 回答