JTextPane 中的插入符位置不正确?!错误或预期行为?

发布于 2024-12-27 08:32:15 字数 2503 浏览 3 评论 0原文

我偶然发现了以下问题:

我想读取 JTextComponent 文档中组件插入符位置的字符。当我使用 JTextPane 时,插入符号位置返回的字符不是正确的。更详细地说,返回的字符是插入符的位置减去行号! (插入符号位置 - 当前行号)。另一方面,当我使用 JTextArea 时,结果是正确的...为了演示这一点,我实现了一个您可以使用的示例程序。

所以最大的问题是,在 JTextPane 的情况下如何获得插入符位置的字符?

为什么 JTextPane 不返回与 JTextArea 相同的插入符位置,并且为什么 JTextPane 返回的字符不是我们在屏幕上看到的字符? 所描述的行为是一个错误吗?

下面您可以找到示例程序的代码以及非常有趣且意想不到的结果的屏幕截图

使用 JTextPane。 CARET 位置 17 中的字母是 e。不...

“替代文本”

使用 JTextArea。这里我的插入符号位置与之前相同,但现在我得到插入符号位置 20 并且返回字母是 \r\n (其中是预期的)。

替代文本

您可以使用以下代码来观察这种奇怪的行为:

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;

public class Example extends JFrame {
//  use this instead of JTextPane to see the difference

//  JTextComponent testingArea = new JTextArea(5,10);  
    JTextComponent testingArea = new JTextPane();
JButton button = new JButton("test");
JTextComponent resultArea = new JTextField(20);


public Example() {
    initialise();
    testingArea.setText("line1\r\nline2\r\nline3\r\nline4");
}


private void initialise() {
    testingArea.setPreferredSize(new Dimension(100,100));
    setLayout(new FlowLayout());
    getContentPane().add(testingArea);
    getContentPane().add(new JLabel("answer"));
    getContentPane().add(resultArea);
    getContentPane().add(button);
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try { 
                int caretPosition = testingArea.getCaretPosition();
                char result = testingArea.getText().charAt(caretPosition);
                resultArea.setText("Char at caretPosition " + caretPosition + " is " + result);
            }catch (Exception e2) {
                e2.printStackTrace();
                resultArea.setText("ERROR");
            }

        }
    });
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    final Example ex = new Example();
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            ex.pack();
            ex.setVisible(true);

        }
    });
}
}

感谢您的帮助!

PS我使用的是java 6。

i have stumbled into the following problem:

I want to read the character in a JTextComponent's document in the location of the component's caret. When i am using a JTextPane the character returned at the caret's position is not the correct one. In more detail the character returned is the character is the position of the caret minus the number of the line!!! (caret Position- number of current line). In the other hand when i use a JTextArea the result is correct... To demonstrate this i have implemented a sample program that you can play with.

So the big question is , how can i get the character of the caret's position in case of JTextPane?

Why JTextPane does not returns the same caret position as JTextArea, and further more why the character returned by JTextPane is not the one that we can see in the screen?
Is the described behavior a bug?

Below you can find the code of the sample program as well as screenshots of the very interesting and unexpected results

Using a JTextPane. The letter in the CARET position 17 is e. nope...

alt text

Usign a JTextArea. Here i have the caret in the same position as before, but now i get caret position 20 and the return letter is \r\n (with is the one expected).

alt text

Here is the code that you can play with to see this strange behavior :

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;

public class Example extends JFrame {
//  use this instead of JTextPane to see the difference

//  JTextComponent testingArea = new JTextArea(5,10);  
    JTextComponent testingArea = new JTextPane();
JButton button = new JButton("test");
JTextComponent resultArea = new JTextField(20);


public Example() {
    initialise();
    testingArea.setText("line1\r\nline2\r\nline3\r\nline4");
}


private void initialise() {
    testingArea.setPreferredSize(new Dimension(100,100));
    setLayout(new FlowLayout());
    getContentPane().add(testingArea);
    getContentPane().add(new JLabel("answer"));
    getContentPane().add(resultArea);
    getContentPane().add(button);
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try { 
                int caretPosition = testingArea.getCaretPosition();
                char result = testingArea.getText().charAt(caretPosition);
                resultArea.setText("Char at caretPosition " + caretPosition + " is " + result);
            }catch (Exception e2) {
                e2.printStackTrace();
                resultArea.setText("ERROR");
            }

        }
    });
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    final Example ex = new Example();
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            ex.pack();
            ex.setVisible(true);

        }
    });
}
}

Thanks for the help!

PS i am using java 6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏日浅笑〃 2025-01-03 08:32:15

使用

char result = testingArea.getDocument().getText(caretPosition,1).charAt(0);

而不是

char result = testingArea.getText().charAt(caretPosition);

Use

char result = testingArea.getDocument().getText(caretPosition,1).charAt(0);

rather than

char result = testingArea.getText().charAt(caretPosition);
神妖 2025-01-03 08:32:15

我认为在 JTextPane 中 EOL 被计为一个字符(\n 我想),而在 JTextArea 中它被计为两个字符(\r\n)。

Oracle 文档说:

JEditorPane 类是 Swing 样式化文本组件的基础,并提供了一种机制,您可以通过该机制添加对自定义文本格式的支持。如果您想要无样式的文本,请改用文本区域。

因此文本区域仅基于给定文本,因此所有输入字符都会计入。 JEdi​​torPane 使用 StyledDocument,因此可以解释 EOL。

I think in JTextPane EOL is counted as one character (\n I suppose) whereas in JTextArea it's counted as two (\r\n).

Oracle documentation says :

The JEditorPane class is the foundation for Swing's styled text components and provides a mechanism through which you can add support for custom text formats. If you want unstyled text, use a text area instead.

So text area are only base on given text so all entry chars are count. JEditorPane used StyledDocument so the EOL might be interpreted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文