Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #35660
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2016-09-27 08:28 -0700 |
| References | <76597439-1bdd-4674-bbc2-771e593ab06e@googlegroups.com> <a8e9f052-b06c-413b-886a-5ccb52089227@googlegroups.com> <nse0ke$ts8$1@dont-email.me> |
| Message-ID | <4caa7450-ba80-4edd-8e05-3de4e668d350@googlegroups.com> (permalink) |
| Subject | Re: JTextArea |
| From | Eric Douglas <e.d.programmer@gmail.com> |
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) {
}
}
});
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web