List of usage examples for javax.swing.text Document remove
public void remove(int offs, int len) throws BadLocationException;
From source file:Main.java
public static void main(String[] argv) throws Exception { JTextComponent textComp = new JTextField("Initial Text"); Document doc = textComp.getDocument(); // Delete the first 5 characters int pos = 0;/*from www. j a v a 2 s. c om*/ int len = 5; doc.remove(pos, len); }
From source file:Main.java
public static void main(String[] argv) throws Exception { JTextComponent textComp = new JTextField("Initial Text"); Document doc = textComp.getDocument(); // Replace the first 3 characters with some text int pos = 0;/*from w ww. j av a2 s .co m*/ int len = 3; doc.remove(pos, len); doc.insertString(pos, "new text", null); }
From source file:Main.java
public static void main(String[] argv) throws Exception { JTextArea comp = new JTextArea(); String actionName = "Lowercase"; JFrame f = new JFrame(); f.add(new JScrollPane(comp)); f.setSize(300, 300);// w w w .j a v a 2s . co m f.setVisible(true); comp.getInputMap().put(KeyStroke.getKeyStroke("F2"), actionName); comp.getActionMap().put(actionName, new TextAction(actionName) { public void actionPerformed(ActionEvent evt) { JTextComponent comp = getTextComponent(evt); if (comp.getSelectionStart() == comp.getSelectionEnd()) { if (comp.getCaretPosition() < comp.getDocument().getLength()) { try { int pos = comp.getCaretPosition(); Document doc = comp.getDocument(); String str = doc.getText(pos, 1).toLowerCase(); doc.remove(pos, 1); doc.insertString(pos, str, null); comp.moveCaretPosition(pos + 1); } catch (Exception e) { System.out.println(); } } } else { int s = comp.getSelectionStart(); int e = comp.getSelectionEnd(); comp.replaceSelection(comp.getSelectedText().toLowerCase()); comp.select(s, e); } } }); }
From source file:de.unentscheidbar.validation.swing.trigger.DocumentChangeTriggerTest.java
@Override protected void provokeTrigger(Iterable<JTextComponent> components) { for (final JTextComponent c : components) { runInEdt(new Callable<Void>() { @Override//w ww. j a va 2 s . c o m public Void call() throws Exception { Document doc = c.getDocument(); switch (rnd.nextInt(3)) { case 0: doc.remove(doc.getStartPosition().getOffset(), doc.getLength() / 2); break; case 1: doc.insertString(rnd.nextInt(Math.max(1, doc.getStartPosition().getOffset())), RandomStringUtils.randomAlphanumeric(1 + rnd.nextInt(10)), null); break; case 2: c.setText(UUID.randomUUID().toString()); break; default: Assert.fail(); } return null; } }); } drainEventQueue(); }
From source file:dmh.swing.huxley.action.InsertHeadingAction.java
@Override public int manipulateText(Document document, int start, int end) { try {/*ww w . j a va2s .c om*/ final int offset = start; final int length = end - start; // Extract the selected text. String title = StringUtils.trimToEmpty(document.getText(offset, length)); document.remove(offset, length); // Add the header. String bar = StringUtils.repeat(token, 40); String insertText = "\n " + title + "\n" + bar + "\n"; document.insertString(offset, insertText, null); // Return the caret position. return start + ("".equals(title) ? 2 : insertText.length()); } catch (BadLocationException e) { // This indicates a programming error. throw new RuntimeException(e); } }
From source file:dmh.swing.huxley.action.WrapTextAction.java
@Override public int manipulateText(Document document, int start, int end) { try {/*from ww w. java 2s .co m*/ final int offset = start; final int length = end - start; // Extract the selected text. String selectedText = StringUtils.trimToEmpty(document.getText(offset, length)); document.remove(offset, length); // Re-insert the text, wrapped in tokens. String insertText = prefixToken + selectedText + suffixToken; document.insertString(offset, insertText, null); // Return the caret position. return start + ("".equals(selectedText) ? 1 : insertText.length()); } catch (BadLocationException e) { // This indicates a programming error. throw new RuntimeException(e); } }
From source file:Main.java
public void actionPerformed(ActionEvent e) { JTextComponent comp = getTextComponent(e); if (comp == null) return;/* w w w.j a v a 2 s .com*/ Document doc = comp.getDocument(); int start = comp.getSelectionStart(); int end = comp.getSelectionEnd(); try { int left = Utilities.getWordStart(comp, start); int right = Utilities.getWordEnd(comp, end); String word = doc.getText(left, right - left); doc.remove(left, right - left); doc.insertString(left, word.toUpperCase(), null); comp.setSelectionStart(start); comp.setSelectionEnd(end); } catch (Exception ble) { return; } }
From source file:Main.java
public void actionPerformed(ActionEvent e) { JTextComponent comp = getTextComponent(e); if (comp == null) return;// w w w . j a v a2s. co m Document doc = comp.getDocument(); int start = comp.getSelectionStart(); int end = comp.getSelectionEnd(); try { int left = Utilities.getWordStart(comp, start); int right = Utilities.getWordEnd(comp, end); String word = doc.getText(left, right - left); doc.remove(left, right - left); doc.insertString(left, word.toUpperCase(), null); comp.setSelectionStart(start); comp.setSelectionEnd(end); } catch (BadLocationException ble) { return; } }
From source file:KeymapExample.java
public void actionPerformed(ActionEvent e) { JTextComponent comp = getTextComponent(e); if (comp == null) return;/*from ww w. j a v a2 s .c o m*/ Document doc = comp.getDocument(); int start = comp.getSelectionStart(); int end = comp.getSelectionEnd(); try { int left = javax.swing.text.Utilities.getWordStart(comp, start); int right = javax.swing.text.Utilities.getWordEnd(comp, end); String word = doc.getText(left, right - left); doc.remove(left, right - left); doc.insertString(left, word.toUpperCase(), null); comp.setSelectionStart(start); comp.setSelectionEnd(end); } catch (BadLocationException ble) { return; } }
From source file:de.huxhorn.lilith.swing.preferences.PreferencesDialog.java
public void editDetailsFormatter() { Console console = new Console(); File messageViewRoot = applicationPreferences.getDetailsViewRoot(); File messageViewGroovyFile = new File(messageViewRoot, ApplicationPreferences.DETAILS_VIEW_GROOVY_FILENAME); EventWrapper<LoggingEvent> eventWrapper = new EventWrapper<>( new SourceIdentifier("identifier", "secondaryIdentifier"), 17, new LoggingEvent()); console.setVariable("eventWrapper", eventWrapper); console.setCurrentFileChooserDir(messageViewRoot); String text = ""; if (!messageViewGroovyFile.isFile()) { applicationPreferences.initDetailsViewRoot(true); }/* w ww . j a v a 2s. co m*/ if (messageViewGroovyFile.isFile()) { InputStream is; try { is = new FileInputStream(messageViewGroovyFile); List lines = IOUtils.readLines(is, StandardCharsets.UTF_8); boolean isFirst = true; StringBuilder textBuffer = new StringBuilder(); for (Object o : lines) { String s = (String) o; if (isFirst) { isFirst = false; } else { textBuffer.append("\n"); } textBuffer.append(s); } text = textBuffer.toString(); } catch (IOException e) { if (logger.isInfoEnabled()) { logger.info("Exception while reading '" + messageViewGroovyFile.getAbsolutePath() + "'.", e); } } } else { if (logger.isWarnEnabled()) logger.warn("Failed to initialize detailsView file '{}'!", messageViewGroovyFile.getAbsolutePath()); } console.run(); // initializes everything console.setScriptFile(messageViewGroovyFile); JTextPane inputArea = console.getInputArea(); //inputArea.setText(text); Document doc = inputArea.getDocument(); try { doc.remove(0, doc.getLength()); doc.insertString(0, text, null); } catch (BadLocationException e) { if (logger.isWarnEnabled()) logger.warn("Exception while setting source!", e); } console.setDirty(false); inputArea.setCaretPosition(0); inputArea.requestFocusInWindow(); }