Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select and retrieve a String of a whole line of text in a JtextArea?

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.

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()).

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.

An example: http://i47.tinypic.com/wvol6a.png


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

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

like image 336
Maxenboy Avatar asked Jan 12 '13 20:01

Maxenboy


3 Answers

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);
   }
}
like image 128
Hovercraft Full Of Eels Avatar answered Oct 31 '22 23:10

Hovercraft Full Of Eels


Consider using a JList instead of JTextArea. JList allows you to select an element out of some set. So you just fill this set with whatever strings you need abd let the user choose.

like image 28
Jakub Zaverka Avatar answered Nov 01 '22 00:11

Jakub Zaverka


i think a JList would be more appropriate for your needs.

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

Or is there a need to use a JTextArea?

like image 45
duffy356 Avatar answered Nov 01 '22 00:11

duffy356