如何在java中的textArea中新添加的文本末尾自动显示插入符?

发布于 2024-12-29 09:33:10 字数 702 浏览 4 评论 0原文

可能的重复:
自动滚动到文本区域的底部 < /p>

I有 TextArea 组件。在不同的情况下,我应该向其附加文本。我希望插入符出现在新附加文本的末尾,如果文本太大,则自动向下滚动。

textAreaStatus = new WebTextArea(
            "1- Click on the refresh icon to get newest file.\n" +
                    "2- Select destination if needed.\n" +
                    "3- Click download button to start downloading.\n");
    textAreaStatus.setBackground(Color.black);
    textAreaStatus.setCaretPosition(textAreaStatus.getText().length());
    textAreaStatus.getCaret().setVisible(true);

Possible Duplicate:
Automatically scroll to the bottom of a text area

I have TextArea component. In different situation i should append text to it.I want Caret to be appears at the end of new appended text and if text is to large, automatically scrolling down.

textAreaStatus = new WebTextArea(
            "1- Click on the refresh icon to get newest file.\n" +
                    "2- Select destination if needed.\n" +
                    "3- Click download button to start downloading.\n");
    textAreaStatus.setBackground(Color.black);
    textAreaStatus.setCaretPosition(textAreaStatus.getText().length());
    textAreaStatus.getCaret().setVisible(true);

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

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

发布评论

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

评论(1

那支青花 2025-01-05 09:33:10

希望这段代码可以在某种程度上帮助您。您只需执行此操作

int len = textArea.getDocument().getLength();
textArea.setCaretPosition(len);

并换行文本,以便它向下滚动,因为长度超过实际视图使用

textArea.setLineWrap(true);

这里是一个示例程序供您理解

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class CarotPosition extends JFrame
{
    private JPanel panel;
    private JTextArea textArea;
    private JScrollPane scrollPane;
    private JButton button;

    public CarotPosition()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        panel = new JPanel();
        panel.setLayout(new BorderLayout());

        textArea = new JTextArea();
        scrollPane = new JScrollPane(textArea);
        textArea.setLineWrap(true);

        button = new JButton("Click to add Text");
        button.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    textArea.append("Some NEW TEXT is here...");
                    int len = textArea.getDocument().getLength();
                    textArea.setCaretPosition(len);
                    textArea.requestFocusInWindow();
                }
            });

        setContentPane(panel);
        panel.add(scrollPane, BorderLayout.CENTER);
        panel.add(button, BorderLayout.PAGE_END);

        pack();
        setVisible(true);   
    }

    public static void main(String... args)
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new CarotPosition();
                }
            });
    }
}

希望这对您有一些帮助。

问候

Hopefully this code might help you in some way. You just have to do this

int len = textArea.getDocument().getLength();
textArea.setCaretPosition(len);

and for wraping the text, so that it scrolls down, as the length is more than the actual view use

textArea.setLineWrap(true);

Here is a sample Program for your understanding

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class CarotPosition extends JFrame
{
    private JPanel panel;
    private JTextArea textArea;
    private JScrollPane scrollPane;
    private JButton button;

    public CarotPosition()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        panel = new JPanel();
        panel.setLayout(new BorderLayout());

        textArea = new JTextArea();
        scrollPane = new JScrollPane(textArea);
        textArea.setLineWrap(true);

        button = new JButton("Click to add Text");
        button.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    textArea.append("Some NEW TEXT is here...");
                    int len = textArea.getDocument().getLength();
                    textArea.setCaretPosition(len);
                    textArea.requestFocusInWindow();
                }
            });

        setContentPane(panel);
        panel.add(scrollPane, BorderLayout.CENTER);
        panel.add(button, BorderLayout.PAGE_END);

        pack();
        setVisible(true);   
    }

    public static void main(String... args)
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new CarotPosition();
                }
            });
    }
}

Hope this be of some help to you.

Regards

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