Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35656 > unrolled thread

JTextArea

Started byEric Douglas <e.d.programmer@gmail.com>
First post2016-09-27 05:11 -0700
Last post2016-09-27 14:00 -0700
Articles 6 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  JTextArea Eric Douglas <e.d.programmer@gmail.com> - 2016-09-27 05:11 -0700
    Re: JTextArea Eric Douglas <e.d.programmer@gmail.com> - 2016-09-27 05:19 -0700
      Re: JTextArea Martin Gregorie <martin@address-in-sig.invalid> - 2016-09-27 14:44 +0000
        Re: JTextArea Eric Douglas <e.d.programmer@gmail.com> - 2016-09-27 08:28 -0700
          Re: JTextArea Martin Gregorie <martin@address-in-sig.invalid> - 2016-09-27 17:44 +0000
            Re: JTextArea Eric Douglas <e.d.programmer@gmail.com> - 2016-09-27 14:00 -0700

#35656 — JTextArea

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-09-27 05:11 -0700
SubjectJTextArea
Message-ID<76597439-1bdd-4674-bbc2-771e593ab06e@googlegroups.com>
Am I missing something?
I'm trying to use this control but if I select text then I click in the middle of the text it un-highlights.  I should be able to click in the highlighted text to drag it elsewhere.  Why does it immediately un-highlight on mouse down and how do I fix it?

[toc] | [next] | [standalone]


#35657

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-09-27 05:19 -0700
Message-ID<a8e9f052-b06c-413b-886a-5ccb52089227@googlegroups.com>
In reply to#35656
On Tuesday, September 27, 2016 at 8:13:40 AM UTC-4, Eric Douglas wrote:
> Am I missing something?
> I'm trying to use this control but if I select text then I click in the middle of the text it un-highlights.  I should be able to click in the highlighted text to drag it elsewhere.  Why does it immediately un-highlight on mouse down and how do I fix it?

It seems odd behavior.  I've found if I call setDragEnabled(true) it does allow me to click in highlighted text to drag it, though it un-highlights it once I start dragging while the mouse is over the control.
As I type here in the Google Groups input box if I highlight and drag it leaves it highlighted and the drag cursor shows the text.  This seems more normal.

[toc] | [prev] | [next] | [standalone]


#35658

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2016-09-27 14:44 +0000
Message-ID<nse0ke$ts8$1@dont-email.me>
In reply to#35657
On Tue, 27 Sep 2016 05:19:37 -0700, Eric Douglas wrote:

> On Tuesday, September 27, 2016 at 8:13:40 AM UTC-4, Eric Douglas wrote:
>> Am I missing something?
>> I'm trying to use this control but if I select text then I click in the
>> middle of the text it un-highlights.  I should be able to click in the
>> highlighted text to drag it elsewhere.  Why does it immediately
>> un-highlight on mouse down and how do I fix it?
>
Probably because JTextArea is primarily intended for displaying text and 
allowing parts of it to be copied to other windows. Yes, a JTextArea can 
be used to enter text provided that the instance has been set editable 
(the default) but its not really intended for editing text: if that's 
what you want, you're better off using a JEditPane instance and doing 
clever things with the caret and the action listeners as needed.

FWIW some time back I built a 24x80 green screen terminal emulator round 
a JTextArea instance with its colours and font set appropriately. I 
handled both keyboard input and text display by setting editable and 
wordwrap to false, linewrap to true and using a KeyAdaptor together with 
the JTextArea text manipulation methods to accept and display keyboard 
input and sending it to the host computer. JTextArea text manipulation 
methods were also used to display host computer responses on the screen. 
That was fairly easy to implement and ran happily at 9600 baud, the 
normal linespeed of the host computer. This emulator was developed and 
tested on a 480 MHz Thinkpad laptop using Java 1.4 and running Linux. 

Before you ask, the terminal emulator used a standard network connection 
to talk to a serial port handler, which was written in C, and which 
handled the host connection.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [next] | [standalone]


#35660

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-09-27 08:28 -0700
Message-ID<4caa7450-ba80-4edd-8e05-3de4e668d350@googlegroups.com>
In reply to#35658
On Tuesday, September 27, 2016 at 10:44:51 AM UTC-4, Martin Gregorie wrote:
> >
> Probably because JTextArea is primarily intended for displaying text and 
> allowing parts of it to be copied to other windows. Yes, a JTextArea can 
> be used to enter text provided that the instance has been set editable 
> (the default) but its not really intended for editing text: if that's 
> what you want, you're better off using a JEditPane instance and doing 
> clever things with the caret and the action listeners as needed.

I'm not sure what you mean by not intended for editing text.  Editing text is exactly what I'm looking for here, with a control that handles multiple lines.
http://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html
"The JTextArea class provides a component that displays multiple lines of text and optionally allows the user to edit the text."

JTextArea extends JEditorPane, so since I am using the built in methods (ie insert(), setLineWrap(), setWrapStyleWord()) it seems to me it would be a lot more trouble to extend JEditorPane.

So, there are some hacks to make it work as expected.  Since I can call setDragEnabled(true) and it does allow me to drag highlighted text and it does stay highlighted until I start moving the mouse I should be able to write a hack to keep it highlighted and/or change the cursor to show the text being dragged.

Now, my next big problem is I'm trying to override the drag and drop behavior to accept text files, and currently if I drag and drop text within the JTextArea control the text just disappears if the drop location is in the range of the drag text.
What am I missing there?
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

          final String LF = System.getProperty("line.separator");
          final JDialog myWin = new JDialog();
          myWin.setSize(1024,800);
          JTextArea myDoc1 = new JTextArea();
          myDoc1.setDragEnabled(true);
          myDoc1.setLineWrap(true);
          myDoc1.setWrapStyleWord(true);
          JScrollPane sp = new JScrollPane(myDoc1);
          myWin.add(sp);
          sp.setLocation(5, 55);
          sp.setSize(800, 160);
          new DropTarget(myDoc1, new DropTargetListener() {

               @Override
               public void dragEnter(final DropTargetDragEvent e) {
                    myDoc1.getCaret().setVisible(true);
               }

               @Override
               public void dragExit(final DropTargetEvent e) {
                    if (!myDoc1.hasFocus()) {
                         myDoc1.getCaret().setVisible(false);
                    }
               }

               @Override
               public void dragOver(final DropTargetDragEvent e) {
                    myDoc1.setCaretPosition(myDoc1.viewToModel(e.getLocation()));
               }

               @Override
               public void dropActionChanged(final DropTargetDragEvent e) {

               }

               @Override
               public void drop(final DropTargetDropEvent e) {
                    try {

                         boolean success = false;
                         // Accept the drop first, important!
                         e.acceptDrop(e.getSourceActions());

                         final Transferable t = e.getTransferable();
                         if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                              myDoc1.insert((String) t.getTransferData(DataFlavor.stringFlavor), myDoc1.getCaretPosition());
                              EventQueue.invokeLater(new Runnable() {

                                   @Override
                                   public void run() {
                                        myDoc1.requestFocusInWindow();
                                   }
                              });
                              success = true;
                         } else {
                              // Get the files that are dropped as java.util.List
                              final java.util.List list = (java.util.List) e.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);

                              // Now get the first file from the list,
                              final File fi = (File) list.get(0);
                              final String fileName = fi.getCanonicalPath();
                              try {
                                   final List<String> lines = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
                                   myDoc1.insert(lines.stream().map(i -> i.toString()).collect(Collectors.joining(LF)) + LF, myDoc1.getCaretPosition());
                                   EventQueue.invokeLater(new Runnable() {

                                        @Override
                                        public void run() {
                                             myDoc1.requestFocusInWindow();
                                        }
                                   });
                                   success = true;
                              } catch (final IOException ei) {
                                   System.out.println("Error reading file " + fileName);
                              }
                         }
                         e.dropComplete(success);

                    } catch (final Exception ex) {
                    }
               }
          });

[toc] | [prev] | [next] | [standalone]


#35661

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2016-09-27 17:44 +0000
Message-ID<nseb5v$apn$1@dont-email.me>
In reply to#35660
On Tue, 27 Sep 2016 08:28:31 -0700, Eric Douglas wrote:

> JTextArea extends JEditorPane,
>
No sir. According to the Java 8 documentation it extends JTextComponent, 
which subclasses  JEditorPane, JTextArea and JTextField.

> so since I am using the built in methods
> (ie insert(), setLineWrap(), setWrapStyleWord())
>
and also copy(), cut(), paste()... But these are really there for 
programmatic use, not for handling keyboard input. 

IIRC I built my terminal emulator with JTextArea rather than JEditorPane 
was because I needed access to keyboard input so I could send it to the 
host machine and EditorKit didn't provide that access. Its also possible 
that it insisted on scrolling, which would be inappropriate for a 
greenscreen emulator, but isn't for a text editor.


> also  it seems to me it would
> be a lot more trouble to extend JEditorPane.
> 
There's a lot of stuff I'd expect an editor to do, but would have to be 
handcoded into JTextArea, such as changing the cursor (referred to in 
JTextComponent docs as a 'caret') to indicate the current editor mode,
e.g. whether its in insert or overtype mode.


> Now, my next big problem is I'm trying to override the drag and drop
> behavior to accept text files, and currently if I drag and drop text
> within the JTextArea control the text just disappears if the drop
> location is in the range of the drag text.
> What am I missing there?
>
Well, speaking for myself, I'd first try using JEditorPane to see how 
close it is to my requirements it is 'out of the box'. 

Insertion of text files should be possible with either JTextArea or 
JEditPane by using the JTextComponent.replaceSelection() method. Both 
probably need a Menu action or a JButton to trigger the file insert (use 
JFileChooser to select the file, read it into a String, call 
replaceSelection() to either replace the selected text or do an insert at 
the caret).

IOW, whether you decide to use a JTextArea or JEditorPane will depend on 
the other stuff you need and whether *that* is already in JEditorPane 
already.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [next] | [standalone]


#35662

FromEric Douglas <e.d.programmer@gmail.com>
Date2016-09-27 14:00 -0700
Message-ID<85e29c56-1513-4202-b417-5c22817adac1@googlegroups.com>
In reply to#35661
On Tuesday, September 27, 2016 at 1:44:52 PM UTC-4, Martin Gregorie wrote:
> On Tue, 27 Sep 2016 08:28:31 -0700, Eric Douglas wrote:
> 
> > JTextArea extends JEditorPane,
> >
> No sir. According to the Java 8 documentation it extends JTextComponent, 
> which subclasses  JEditorPane, JTextArea and JTextField.

Right I got mixed up, I was looking at the JTextPane extends JEditorPane.

http://docs.oracle.com/javase/tutorial/uiswing/components/text.html

So I think for now I'll stick with JTextPane.  A couple things I wanted it to do seemed tricky but I like the built in functionality with the line/word wrap, and at this time I don't need anything fancy like multiple fonts.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web