JTextArea question

  • Thread starter Ryan Tan via JavaKB.com
  • Start date
R

Ryan Tan via JavaKB.com

Hi, I have a question regarding JTextArea.

I need to place a JTextArea on my frame to display application messages. Is there a way to make it non-editable but users still can select text in it and copy to clipboard using the keyboard shortcut?

I tried .setEditable(false) but this disables the "Copy to clipboard" functionality of JTextArea.
 
B

Babu Kalakrishnan

Ryan said:
Hi, I have a question regarding JTextArea.

I need to place a JTextArea on my frame to display application messages. Is there a way to make it non-editable but users still can select text in it and copy to clipboard using the keyboard shortcut?

I tried .setEditable(false) but this disables the "Copy to clipboard" functionality of JTextArea.

Are you sure ? Which JDK version is this ? I find that I can select as
well as copy text on JDK 1.4.2 / Linux even on a TextArea that isn't
editable.

BK
 
R

Ryan Tan via JavaKB.com

Yes I am sure. I am using JDK 1.4.2_04 on windows.

Any ideas are appreciated :)
 
B

Babu Kalakrishnan

Ryan said:
Yes I am sure. I am using JDK 1.4.2_04 on windows.

Any ideas are appreciated :)

Even the source code doesn't show them being disabled. So I suspect it
is something else in your code that's doing it. Can you post a small
code sample that exhibits this behaviour ?

Are you by any chance calling setEnabled(false) as well ?

BK
 
D

David Segall

Ryan Tan via JavaKB.com said:
Yes I am sure. I am using JDK 1.4.2_04 on windows.
That's odd. I can copy to the clipboard using JDK 1.5 and Windows XP.
Of course, the process is different from copying from an editable
document because a single mouse click does not insert a cursor.
 
R

Ryan Tan via JavaKB.com

Hi Mr Kalakrishnan, yes I am setting it as non-editable using the .setEditable(false) method.

Here is how I create my JTextArea:

protected JTextArea txtMyText;

.....

txtMyText = new JTextArea(7, 40);
txtMyText.setEditable(false);
txtMyText.setLineWrap(true);
txtMyText.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(txtMyText,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

....

myPanel.add(listScrollPane);

Am I doing something wrong here? As far as I can see, I am not explicitly doing anything to interrupt the normal functionality of the JTextArea.

Thanks for the help guys.
 
R

Ryan Tan via JavaKB.com

Sorry, the code where it says myPanel.add(listScrollPane);
should say myPanel.add(scrollPane); instead.
 
B

Babu Kalakrishnan

Ryan said:
Hi Mr Kalakrishnan, yes I am setting it as non-editable using the .setEditable(false) method.

Here is how I create my JTextArea:

protected JTextArea txtMyText;

....

txtMyText = new JTextArea(7, 40);
txtMyText.setEditable(false);
txtMyText.setLineWrap(true);
txtMyText.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(txtMyText,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

...

myPanel.add(listScrollPane);

Am I doing something wrong here? As far as I can see, I am not explicitly doing anything to interrupt the normal functionality of the JTextArea.

Thanks for the help guys.

With code almost identical to that, the selection / copying seems to be
working on my machine (And I assume it must be similar code which worked
on some one elses machine on WinXP). So it would need some more input to
see if you really have a problem.

How are you selecting text ? I do it by clicking on the start character
and dragging the mouse to the ending character - and the selection shows
up fine. In your case are you able to see a selection when you do the
same ? Secondly, how are you trying to invoke the "Copy" ? (I do so by
pressing the Ctrl-C key combination).

BK
 
R

Ryan Tan via JavaKB.com

Yes, I am selecting and copying like you describe. Now I am getting worried... I tried to make a small app with a disabled JTextArea and it the copy functionality still works (you're right!). So now I am pretty sure it's something to do with my large program. I will try and see if I can find the reason for this bug...

Thanks for the help Mr Kalakrishnan
 
S

Sherman Wang via JavaKB.com

I have the same problem:
1. My code has a non-editable JTextArea. My computer is XP professional with SP2. I compile my code using JDK 1.3.2.
2. When I run it using JRE 1.3.2, I can select text and copy it.
3. But If I run it using JRE 1.4.1_07, JRE 1.4.2_06 and JRE 1.5.0, the text is not selectable anymore.

Because I use the same code and the same classes, I think there is something dependent on JRE.
 
Joined
Jan 30, 2009
Messages
1
Reaction score
0
Same problem

I have same problem on java 1.6. I have resoulved that problem by this code:
public static void setNonEditableButSelectable(final JTextPane jtp){
jtp.setEditable(false);
jtp.setHighlighter(new DefaultHighlighter());
jtp.addCaretListener(new CaretListener(){
Code:
   @Override
   public void caretUpdate(CaretEvent e) {
     int dot = e.getDot();
     int mark = e.getMark();
				   			    
     if (dot == mark) {
       return;
     } else if (dot < mark) {
        jtp.getHighlighter().removeAllHighlights();
        try {
          jtp.getHighlighter().addHighlight(dot, mark,
                    new DefaultHighlighter.DefaultHighlightPainter(jtp.getSelectionColor()));
        } catch (BadLocationException e1) {
           e1.printStackTrace();
        }
      } else {
        jtp.getHighlighter().removeAllHighlights();
			    		
      try {
        jtp.getHighlighter().addHighlight(mark, dot, new DefaultHighlighter.DefaultHighlightPainter(jtp.getSelectionColor()));
      } catch (BadLocationException e1) {
         e1.printStackTrace();
      }
    }
  }});
}
the methods getSelectionEnd, getSelectionStart and getSelectionText are working even if it's not highlighted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top