简体   繁体   English

如何将光标移到JScrollPane内JTextArea的TOP

[英]How do I get the cursor to the TOP of a JTextArea inside a JScrollPane

For a long time I've been living with the cursor being at the bottom of my JTextArea named txaOutput after being populated in a JScrollPane even though I WANT it positioned at the TOP. 很长一段时间以来,我一直希望光标位于我的JTextArea的底部,名为txaOutput之后将其填充在JScrollPane即使我希望将其放置在顶部。

Tthe only help I've found has referred to setting the caret position to 0 (eg, txaOutput.setCaretPosition(0); ), which I'd never figured out how to make work properly. 我发现的唯一帮助是将插入符号位置设置为0(例如txaOutput.setCaretPosition(0); ),我从没想过如何使工作正常。

Today I just went through every conceivable method for JTextArea and finally found that, BEFORE populating it, this line seems to do what I need: 今天,我遍历了JTextArea所有可能方法,最后发现,在填充它之前,此行似乎可以满足我的要求:

txaOutput.insert(" ", 1 );

SURELY this isn't the best or only way. 当然,这不是最佳或唯一的方法。

Here's the class for the text area: 这是文本区域的类:

package masterwords;
import gbl.GBConstraints;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;

public class HelpOutput extends JFrame {

  private JScrollPane scrPnl;
  private JTextArea txaOutput;  

  public     HelpOutput() /* constructor */ {

    scrPnl = new JScrollPane();

    txaOutput    = new JTextArea();

    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

    scrPnl.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrPnl.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scrPnl.setViewportBorder(BorderFactory.createEtchedBorder());
    scrPnl.setAutoscrolls(false);
    scrPnl.setPreferredSize(new Dimension(500, 555));

    txaOutput.setFont(new Font("Courier New", 0, 14)); 
    scrPnl.setViewportView(txaOutput);

    setLayout(new GridBagLayout());

    add(scrPnl, new GBConstraints(0,0).ipad(200, 300).spanX(100).spanY(90));

    txaOutput.insert(" ", 1 );  // ********** WITHOUT THIS CURSOR IS AT BOTTOM
    setVisible(true);
    pack();
  }

  public void appendHelp(String s){
    txaOutput.append(s);
  }
}

Here's how I've always called it but it NEVER worked until adding line with ************ above: 这就是我一直称呼它的方式,但是直到添加上面的************行,它才起作用:

  private void btnHelpActionPerformed (ActionEvent evt) {                                        

    HelpOutput helpOutput = new HelpOutput();

    Scanner sc = openHelp();
    while(sc.hasNext())
      helpOutput.appendHelp(sc.next());

   // txaOutput.setCaretPosition(0); // THIS DOES NOTHING so commented out!!!
  }   

Prior to adding the line with all the **************s, NOTHING ELSE I tried put the cursor at the TOP of the text area--always the bottom. 在添加带有所有**************的行之前,我尝试过将光标放在文本区域的顶部(始终在底部)。

WHAT SHOULD I BE DOING? 我应该怎么做? What I AM doing seems a kludge. 我正在做的事情似乎很矛盾。

* EDIT, THANKS TO JAVANATOR * *编辑,感谢JAVANATOR *

Rename variable txaOutput to txaHelpOutput ; 将变量txaOutput重命名为txaHelpOutput ; problem solved. 问题解决了。 New key lines: 新的关键线:

  private void btnHelpActionPerformed (ActionEvent evt) {                                        

    HelpOutput helpOutput = new HelpOutput();

    Scanner sc = openHelp();
    while(sc.hasNext())
      helpOutput.appendHelp(sc.next());

   txaHelpOutput.setCaretPosition(0);
   // ^^^^
  }   


public class HelpOutput extends JFrame {

  private JTextArea txaHelpOutput;  
  //                   ^^^^

  public     HelpOutput() /* constructor */ {

    txaHelpOutput    = new JTextArea();
    // ^^^^

    scrPnl.setViewportView(txaHelpOutput);
    //                        ^^^^

    // LOSE THIS LINE!! txaHelpOutput.insert(" ", 1 ); 
  }

First, txaOutput.insert(" ", 1 ); 首先, txaOutput.insert(" ", 1 ); inserts a space into your help text, which is probably not what you want. 在您的帮助文本中插入一个空格,这可能不是您想要的。

Second, you create a HelpOutput object, append text to it, but then you call setCaretPosition on some other object referenced by txaOutput . 其次,创建一个HelpOutput对象, HelpOutput添加文本,然后在txaOutput引用的其他对象上调用setCaretPosition You need to call setCaretPosition on the HelpOutput object's JTextArea . 您需要在HelpOutput对象的JTextArea上调用setCaretPosition This is easily done by creating a method in HelpOutput which calls setCaretPosition(0) . 这可以通过在HelpOutput创建一个调用setCaretPosition(0)的方法来轻松完成。

The following code will produce a text area with the carrot at the top, using setCaretPosition(0) . 下面的代码将使用setCaretPosition(0)生成一个文本区域,顶部是胡萝卜。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class HelpOutput extends JFrame
{
    private static final long   serialVersionUID    = -1323914827861467580L;
    private JScrollPane         scrPnl;
    private JTextArea           txaOutput;

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                btnHelpActionPerformed(null);
            }
        });
    }

    public HelpOutput()
    {

        scrPnl = new JScrollPane();
        txaOutput = new JTextArea();

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        scrPnl.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrPnl.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrPnl.setViewportBorder(BorderFactory.createEtchedBorder());
        scrPnl.setAutoscrolls(false);
        scrPnl.setPreferredSize(new Dimension(500, 555));

        txaOutput.setFont(new Font("Courier New", 0, 14));
        scrPnl.setViewportView(txaOutput);

        setLayout(new BorderLayout());

        add(scrPnl, BorderLayout.CENTER);

        setVisible(true);
        pack();
    }

    public void appendHelp(String s)
    {
        txaOutput.append(s);
    }

    public void putCarrotAtTop()
    {
        txaOutput.setCaretPosition(0);
    }

    private static void btnHelpActionPerformed(ActionEvent evt)
    {
        HelpOutput helpOutput = new HelpOutput();

        helpOutput
                .appendHelp("Lots of help\nLots of help\nLots of help\nLots of help\nLots of help\n");
        helpOutput.putCarrotAtTop();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM