How to select and get a string of an entire text string in JtextArea?

I have a JFrame that displays the current movies that are stored on my computer. It maps file names as String in JTextArea .

I want to double-click on a specific String (which is the actual file on my computer) and this file will open.

The initial part and the double-click part are already resolved, but when I double-click on a String in my JTextArea , only part of that String will be selected. (I am using JTextArea.getSelectedText() ).

I want all String be selected and I can get String . I need to do this because some of my movie files have similar names and the file does not open.

Is there an already implemented method that can expand the selection to a whole line? I tried to solve the Google problem, but nothing will display the entire line of text.

Example: http://i47.tinypic.com/wvol6a.png


Thanks to everyone for their contribution, and I'm sorry that I was unclear regarding JTextArea, JTextArea was a must.

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

+6
source share
5 answers

It is best to use a JList, as recommended by many above. If you need to use JTextArea, then this can be done, but you will need to use the JTextArea viewToModel(Point p) method to translate the location of the mouse click. Indicate the location of the offset in the text. Then you can use the static methods of the utility class javax.swing.text.Utilities, getRowStart(...) and getRowEnd(...) to find the beginning and end of the selected line. 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); } } 
+4
source

Consider using JList instead of JTextArea. JList allows you to select an item from some set. This way, you simply fill this set with any lines you need, and let the user select.

+3
source

I think JList will be more suitable for your needs.

simple example: http://www.cs.cf.ac.uk/Dave/HCI/HCI_Handout_CALLER/node143.html

Or do I need to use JTextArea?

+2
source

Basically, you need to extract a string from this JTextArea. But I recommend you switch to a component that is more suitable for your use case: http://docs.oracle.com/javase/6/docs/api/javax/swing/JList.html

In any case, you can try a quick and dirty hack like this:

 public static String getLineOfSelectionStart(JTextArea textArea) { String contents = textArea.getText(); int selStart = textArea.getSelectionStart(); if (selStart >= 0) { int selEnd = selStart; // don't use getSelectionEnd(), since one // could select multiple lines; while (selStart > 0) { switch (contents.charAt(selStart)) { case '\r': case '\n': break; default: --selStart; continue; } break; } while (selEnd < contents.length()) { switch (contents.charAt(selEnd)) { case '\r': case '\n': break; default: ++selEnd; continue; } break; } return contents.substring(selStart, selEnd); } return null; } 

But in fact, if you list a large number of files, it will not be so good.

+1
source

Use a JList and to get the selected option, use getSelectedIndex() to retrieve the index or use getSelectedValue() to get the value.

See here: see getSelectedIndex () method

0
source

All Articles