List of usage examples for javax.swing.text JTextComponent getText
public String getText()
TextComponent
. From source file:Main.java
/** * Cuts via the fake clipboard.//from w w w . j a va2 s . c om */ static void fakeCut(JTextComponent tc) { String selection = tc.getSelectedText(); if ((selection != null) && (selection.length() > 0)) { clipboard = selection; String text = tc.getText(); int selectionStart = tc.getSelectionStart(); int selectionEnd = tc.getSelectionEnd(); tc.setText(text.substring(0, selectionStart) + text.substring(selectionEnd)); tc.setCaretPosition(selectionStart); } }
From source file:Main.java
/** * Set the text component in which only numerics (positive decimal integers * with character 0-9)./*w ww .ja va 2s .c om*/ * * @param c * the specified text component */ public static void setTextComponetIntegerValid(final JTextComponent c) { c.addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent e) { if (e.getKeyChar() != KeyEvent.VK_BACK_SPACE) try { Integer.valueOf(c.getText() + e.getKeyChar()); } catch (NumberFormatException nfe) { e.consume(); Toolkit.getDefaultToolkit().beep(); } } }); }
From source file:Main.java
/** * Returns the {@link Dimension} for the given {@link JTextComponent} * subclass that will show the whole word wrapped text in the given width. * It won't work for styled text of varied size or style, it's assumed that * the whole text is rendered with the {@link JTextComponent}s font. * * @param textComponent the {@link JTextComponent} to calculate the {@link Dimension} for * @param width the width of the resulting {@link Dimension} * @param text the {@link String} which should be word wrapped * @return The calculated {@link Dimension} *//* ww w .j a v a 2 s . c o m*/ public static Dimension getWordWrappedTextDimension(JTextComponent textComponent, int width, String text) { if (textComponent == null) { throw new IllegalArgumentException("textComponent cannot be null"); } if (width < 1) { throw new IllegalArgumentException("width must be 1 or greater"); } if (text == null) { text = textComponent.getText(); } if (text.isEmpty()) { return new Dimension(width, 0); } FontMetrics metrics = textComponent.getFontMetrics(textComponent.getFont()); FontRenderContext rendererContext = metrics.getFontRenderContext(); float formatWidth = width - textComponent.getInsets().left - textComponent.getInsets().right; int lines = 0; String[] paragraphs = text.split("\n"); for (String paragraph : paragraphs) { if (paragraph.isEmpty()) { lines++; } else { AttributedString attributedText = new AttributedString(paragraph); attributedText.addAttribute(TextAttribute.FONT, textComponent.getFont()); AttributedCharacterIterator charIterator = attributedText.getIterator(); LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(charIterator, rendererContext); lineMeasurer.setPosition(charIterator.getBeginIndex()); while (lineMeasurer.getPosition() < charIterator.getEndIndex()) { lineMeasurer.nextLayout(formatWidth); lines++; } } } return new Dimension(width, metrics.getHeight() * lines + textComponent.getInsets().top + textComponent.getInsets().bottom); }
From source file:com.prodigy4440.view.MainJFrame.java
public static void highlightText(JTextComponent component, String s) { try {/*from w w w .j a v a2s .c om*/ Highlighter highlighter = component.getHighlighter(); String text = component.getText(); String line = null; int start = 0; int end; int totalLines = ((JTextArea) component).getLineCount(); for (int i = 0; i < totalLines; i++) { if (i == 5) { start = ((JTextArea) component).getLineOfOffset(i); end = ((JTextArea) component).getLineEndOffset(i); line = text.substring(start, end); } } int pos = start; if ((pos = text.indexOf(s, pos)) >= start) { DefaultHighlighter.DefaultHighlightPainter highlightPainter = new DefaultHighlighter.DefaultHighlightPainter( Color.BLUE); highlighter.addHighlight(pos, pos + s.length(), highlightPainter); } } catch (BadLocationException ble) { } }
From source file:net.sf.jabref.gui.keyboard.EmacsKeyBindings.java
private static int getWordEnd(JTextComponent jtc, int start) throws BadLocationException { try {/*from ww w .jav a2 s .c o m*/ return Utilities.getNextWord(jtc, start); } catch (BadLocationException ble) { int end = jtc.getText().length(); if (start < end) { return end; } else { throw ble; } } }
From source file:Main.java
public void focusGained(FocusEvent evt) { final JTextComponent c = (JTextComponent) evt.getSource(); String s = c.getText(); for (int i = 0; i < s.length(); i++) { if (!Character.isDigit(s.charAt(i))) { c.setSelectionStart(i);/*w w w . j a v a 2s.com*/ c.setSelectionEnd(i); break; } } }
From source file:Main.java
public void focusLost(FocusEvent evt) { final JTextComponent c = (JTextComponent) evt.getSource(); String s = c.getText(); if (evt.isTemporary()) { return;/* ww w . j a v a 2 s .co m*/ } for (int i = 0; i < s.length(); i++) { if (!Character.isDigit(s.charAt(i))) { System.out.println("must only contain digits"); c.requestFocus(); break; } } }
From source file:com.intel.stl.ui.common.view.ComponentFactory.java
public static SafeNumberField<Integer> createNumericTextField(Integer max, DocumentListener... docListeners) { final SafeNumberField<Integer> field = new SafeNumberField<Integer>(new DecimalFormat("###"), 0, false, max, false);/*from w w w. j a v a 2s . com*/ // only positive integer field.setValidChars(UIConstants.DIGITS); if (docListeners != null) { for (DocumentListener docListener : docListeners) { field.getDocument().addDocumentListener(docListener); } } // Add a mouse listener to clear out the field before the user types field.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { try { JTextComponent tc = (JTextComponent) e.getSource(); String txt = tc.getText(); if (!txt.isEmpty()) { field.getFormatter().stringToValue(txt); } } catch (Exception ex) { field.setText(""); } } }); return field; }
From source file:de.codesourcery.eve.skills.ui.utils.IntTextFieldValidator.java
protected void attachFilter(JTextComponent comp) { // Casting is bad although seems to be // the 'official way' as it's in the Swing tutorials as well.... final AbstractDocument doc = (AbstractDocument) comp.getDocument(); final String val = comp.getText(); if (!isInteger(val) || !isWithinRange(val)) { comp.setText(Integer.toString(minValue)); }//from w w w . jav a 2 s.c o m doc.setDocumentFilter(filter); }
From source file:net.sf.jabref.gui.AutoCompleteListener.java
public void clearCurrentSuggestion(JTextComponent comp) { if (toSetIn != null) { int selStart = comp.getSelectionStart(); String text = comp.getText(); comp.setText(text.substring(0, selStart) + text.substring(comp.getSelectionEnd())); comp.setCaretPosition(selStart); lastCompletions = null;//from w ww . ja v a 2s .c o m lastShownCompletion = 0; lastCaretPosition = -1; toSetIn = null; } }