List of usage examples for javax.swing.text JTextComponent getDocument
public Document getDocument()
From source file:Main.java
public static void main(String[] argv) { JTextComponent textComp1 = new JTextArea(); JTextComponent textComp2 = new JTextArea(); textComp2.setDocument(textComp1.getDocument()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { JTextComponent textcomp = new JTextPane(); textcomp.setText("Initial Text"); textcomp.getDocument().addDocumentListener(new DocumentListener() { public void insertUpdate(DocumentEvent evt) { int off = evt.getOffset(); System.out.println("off:" + off); int len = evt.getLength(); System.out.println("len:" + len); try { String str = evt.getDocument().getText(off, len); System.out.println(str); } catch (BadLocationException e) { }//from w w w .ja v a 2s .com } public void removeUpdate(DocumentEvent evt) { int off = evt.getOffset(); System.out.println("off:" + off); int len = evt.getLength(); System.out.println("len:" + len); } public void changedUpdate(DocumentEvent evt) { int off = evt.getOffset(); System.out.println("off:" + off); int len = evt.getLength(); System.out.println("len:" + len); } }); }
From source file:Main.java
public static void main(String[] argv) { JTextComponent textcomp = new JTextArea(); final UndoManager undo = new UndoManager(); Document doc = textcomp.getDocument(); doc.addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); }//from www . ja v a 2s. c om }); textcomp.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JScrollPane(textcomp)); frame.setSize(380, 320); frame.setLocationRelativeTo(null); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] argv) { JTextComponent textcomp = new JTextArea(); final UndoManager undo = new UndoManager(); Document doc = textcomp.getDocument(); doc.addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); }//w w w.j ava 2 s. c o m }); textcomp.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JScrollPane(textcomp)); frame.setSize(380, 320); frame.setLocationRelativeTo(null); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] argv) throws Exception { JTextComponent textcomp = new JTextArea(); final UndoManager undo = new UndoManager(); Document doc = textcomp.getDocument(); doc.addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); }//from ww w . j av a 2s . c om }); textcomp.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); textcomp.getActionMap().put("Redo", new AbstractAction("Redo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canRedo()) { undo.redo(); } } catch (CannotRedoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo"); }
From source file:Main.java
public static void main(String[] argv) throws Exception { JTextComponent textcomp = new JTextArea(); final UndoManager undo = new UndoManager(); Document doc = textcomp.getDocument(); JFrame f = new JFrame(); f.add(new JScrollPane(textcomp)); f.setSize(330, 300);//from ww w . j a v a 2s. c o m f.setVisible(true); doc.addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); } }); textcomp.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); textcomp.getActionMap().put("Redo", new AbstractAction("Redo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canRedo()) { undo.redo(); } } catch (CannotRedoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo"); }
From source file:Main.java
public static void main(String[] argv) { JTextComponent textcomp = new JTextArea(); final UndoManager undo = new UndoManager(); Document doc = textcomp.getDocument(); doc.addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); }/*from ww w .j a va 2 s . c o m*/ }); textcomp.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); textcomp.getActionMap().put("Redo", new AbstractAction("Redo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canRedo()) { undo.redo(); } } catch (CannotRedoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo"); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JScrollPane(textcomp)); frame.setSize(380, 320); frame.setLocationRelativeTo(null); frame.setVisible(true); }
From source file:Main.java
/** * Get the string from the passed in textComponent representing the text displayed by the * component with all the soft new lines (added in from text wrapping) added in. * /* w w w.j ava 2 s.c o m*/ * @param c * @return * Returns null if comp does not have a size */ public static String getWrappedText(JTextComponent c) { int len = c.getDocument().getLength(); int offset = 0; // Increase 10% for extra newlines StringBuffer buf = new StringBuffer((int) (len * 1.10)); try { while (offset < len) { int end = Utilities.getRowEnd(c, offset); if (end < 0) { break; } // Include the last character on the line end = Math.min(end + 1, len); String s = c.getDocument().getText(offset, end - offset); buf.append(s); // Add a newline if s does not have one if (!s.endsWith("\n")) { buf.append('\n'); } offset = end; } } catch (BadLocationException e) { } return buf.toString(); }
From source file:TextComponentDisplay.java
public static void displayModel(JTextComponent comp, PrintStream out) { Document doc = comp.getDocument(); if (doc instanceof AbstractDocument) { ((AbstractDocument) doc).dump(out); }//w w w .j av a2s . c o m }
From source file:Main.java
public static void highlight(JTextComponent textComp) { try {/*from ww w .j av a2 s . c o m*/ Highlighter hilite = textComp.getHighlighter(); Document doc = textComp.getDocument(); hilite.addHighlight(3, 5, new MyHighlightPainter(Color.red)); } catch (BadLocationException e) { e.printStackTrace(); } }