如何在JtextArea中选择和检索整行文本的String? [英] How to select and retrieve a String of a whole line of text in a JtextArea?

查看:186
本文介绍了如何在JtextArea中选择和检索整行文本的String?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JFrame,显示存储在我的计算机上的当前电影。它在 JTextArea 中将文件名称显示为 String s。

I have a JFrame that displays the current movies that are stored on my computer. It displays the names of the files as Strings in a JTextArea.

我想要做的是双击特定的字符串(表示我计算机上的实际文件) )并且该文件将被打开。

What I want to do is to double-click a particular String (which represents an actual file on my computer) and that file would be opened.

开头部分和双击部分已经解决,但是当我双击字符串时在我的 JTextArea 中,只会选择 String 的一部分。 (我正在使用 JTextArea.getSelectedText())。

The opening part and double-click part is already solved, but when I double-click on the String in my JTextArea only a part of that String will be selected. (I'm using JTextArea.getSelectedText()).

我想要的是整个<$ c选择了$ c> String ,我可以检索 String 。我需要这样做,因为我的一些电影文件具有相似的名称,并且将打开错误的文件。

What I want is that the whole String is selected and that I can retrieve the String. I need to do this since some of my movie files have similar names and the wrong file would be opened.

是否有任何已经实现的方法可以将选择范围扩展到全线?我试图谷歌解决这个问题,但没有什么会选择整行文字。

Is there any already implemented method that can extend the selection to a whole line? I've tried to Google the problem but nothing will select the whole line of text.

一个例子:
http://i47.tinypic.com/wvol6a.png

感谢大家的输入,对不起我对JTextArea不清楚,JTextArea是强制性的。

Thank you all for the input and I'm sorry that i was unclear regarding the JTextArea, the JTextArea was mandatory.

我现在有了解决我的问题,我感谢Hovercraft Full Of Eels。

I have now a solution to my problem and I thank Hovercraft Full Of Eels for this.

推荐答案

你最好的选择是使用JList上面有很多人推荐过。如果你必须使用JTextArea,那么这可以完成,但你需要使用JTextArea的 viewToModel(Point p)方法将鼠标按下位置的Point转换为偏移量在您的文字中的位置。然后,您可以使用javax.swing.text.Utilities类的静态实用程序方法, getRowStart(...) getRowEnd(...)查找所选行的开头和结尾。例如,我的SSCCE:

Your best bet is to use a JList as has been recommended by many above. If you have to use a JTextArea, then this can be done, but you will need to use JTextArea's viewToModel(Point p) method to translate the mouse press location's Point to the offset location in your text. You can then use the javax.swing.text.Utilities class's static utility methods, getRowStart(...) and getRowEnd(...) to find the start and end of the selected row. For example, my SSCCE:

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
import javax.swing.text.Utilities;

public class GetLineFromTextArea {
   private static final int MIN_CHARS = 4;
   private static final int MAX_CHARS = 8;
   private static final int WORDS_PER_LINE = 10;
   private static final int ROWS = 30;

   public static void main(String[] args) {
      Random random = new Random();
      final JTextArea textArea = new JTextArea(20, 50);
      JScrollPane scrollpane = new JScrollPane(textArea);
      StringBuilder sb = new StringBuilder();

      for (int row = 0; row < ROWS ; row++) {
         sb = new StringBuilder();
         for (int words = 0; words < WORDS_PER_LINE; words++) {
            int maxChars = random.nextInt(MAX_CHARS - MIN_CHARS) + MIN_CHARS;
            for (int charsPerWord = 0; charsPerWord < maxChars; charsPerWord++) {
               char c = (char) (random.nextInt('z' - 'a' + 1) + 'a');
               sb.append(c);
            }
            sb.append(" ");
         }
         textArea.append(sb.toString() + "\n");
      }

      textArea.addMouseListener(new MouseAdapter() {
         @Override
         public void mouseClicked(MouseEvent e) {
            if (e.getButton() != MouseEvent.BUTTON1) {
               return;
            }
            if (e.getClickCount() != 2) {
               return;
            }

            int offset = textArea.viewToModel(e.getPoint());

            try {
               int rowStart = Utilities.getRowStart(textArea, offset);
               int rowEnd = Utilities.getRowEnd(textArea, offset);
               String selectedLine = textArea.getText().substring(rowStart, rowEnd);
               System.out.println(selectedLine);

            } catch (BadLocationException e1) {
               e1.printStackTrace();
            }

         }
      });


      JOptionPane.showMessageDialog(null, scrollpane);
   }
}

这篇关于如何在JtextArea中选择和检索整行文本的String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆