Example usage for javax.swing JTextArea getText

List of usage examples for javax.swing JTextArea getText

Introduction

In this page you can find the example usage for javax.swing JTextArea getText.

Prototype

public String getText() 

Source Link

Document

Returns the text contained in this TextComponent.

Usage

From source file:Main.java

public static void main(String args[]) {
    JTextArea area = new JTextArea(5, 20);
    area.setText("this is a test.");
    String charsToHighlight = "aeiouAEIOU";
    Highlighter h = area.getHighlighter();
    h.removeAllHighlights();/*  ww  w. j  a  va 2 s. com*/
    String text = area.getText().toUpperCase();
    for (int i = 0; i < text.length(); i += 1) {
        char ch = text.charAt(i);
        if (charsToHighlight.indexOf(ch) >= 0)
            try {
                h.addHighlight(i, i + 1, DefaultHighlighter.DefaultPainter);
            } catch (Exception ble) {
            }
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("MultiHighlight");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextArea comp = new JTextArea(5, 20);
    comp.setText("this is a test");
    frame.getContentPane().add(new JScrollPane(comp), BorderLayout.CENTER);

    String charsToHighlight = "a";
    Highlighter h = comp.getHighlighter();
    h.removeAllHighlights();/*from w  ww  .jav  a2s . com*/
    String text = comp.getText().toUpperCase();

    for (int j = 0; j < text.length(); j += 1) {
        char ch = text.charAt(j);
        if (charsToHighlight.indexOf(ch) >= 0)
            h.addHighlight(j, j + 1, DefaultHighlighter.DefaultPainter);
    }
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Copy a JTextArea text (jTextArea.getText()) to clipborad
 *
 * @param jTextArea//from w w w.j av a  2 s .  co m
 */
public static void copyToClipboard(JTextArea jTextArea) {
    copyToClipboard(jTextArea.getText());
}

From source file:Main.java

/**
 * Select all text in a {@link JComponent} if it is a {@link JTextField} or {@link JTextArea}.
 *
 * @param comp the component//from  www .  j  a v a  2  s. c  om
 */
public static void selectAllText(JComponent comp) {
    if (comp instanceof JTextField) {
        JTextField tf = (JTextField) comp;
        tf.setSelectionStart(0);
        tf.setSelectionEnd(tf.getText().length());
    } else if (comp instanceof JTextArea) {
        JTextArea ta = (JTextArea) comp;
        ta.setSelectionStart(0);
        ta.setSelectionEnd(ta.getText().length());
    }
}

From source file:com.SCI.centraltoko.utility.UtilityTools.java

private static void convertToUpperCase(final JTextArea textArea) {
    textArea.setText(textArea.getText().toUpperCase());
}

From source file:Main.java

private static List<String> getLines(JTextArea textArea) {
    int lineHeight = getLineHeight(textArea);
    List<String> list = new ArrayList<String>();
    for (int num = 0;; num++) {
        int i = textArea.viewToModel(new Point(0, num * lineHeight));
        int j = textArea.viewToModel(new Point(0, (num + 1) * lineHeight));
        if (i == 0 && j == 0) {
            continue;
        }/*  ww  w . j  ava  2 s  . com*/
        if (textArea.getDocument().getLength() == i && i == j) {
            break;
        }
        String s = removeTrailingNewLine(textArea.getText().substring(i, j));
        list.add(s);
    }
    return list;
}

From source file:com.prodigy4440.view.MainJFrame.java

public static String[] getTextLineByLine(JTextArea area) {
    int numberOfLine = area.getLineCount();
    String resultString[] = new String[numberOfLine];
    String text = area.getText();
    for (int i = 0; i < numberOfLine; i++) {
        try {/*from www  .j  ava2  s .  c  o m*/
            int start = area.getLineStartOffset(i);
            int end = area.getLineEndOffset(i);

            String line = text.substring(start, end);
            resultString[i] = line;
        } catch (BadLocationException ex) {
            Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    return resultString;
}

From source file:com.sander.verhagen.frame.LineWrapCellRenderer.java

private int getLineCount(JTextArea textArea) {
    AttributedString string = new AttributedString(textArea.getText());
    FontRenderContext fontRenderContext = textArea.getFontMetrics(textArea.getFont()).getFontRenderContext();
    AttributedCharacterIterator characterIterator = string.getIterator();
    LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(characterIterator, fontRenderContext);
    lineBreakMeasurer.setPosition(characterIterator.getBeginIndex());

    int lineCount = 0;
    while (lineBreakMeasurer.getPosition() < characterIterator.getEndIndex()) {
        lineBreakMeasurer.nextLayout(textArea.getSize().width);
        lineCount++;//from www  .  java  2  s  .  co  m
    }
    return lineCount;
}

From source file:Main.java

public Main() {
    JTextArea area = new JTextArea(5, 20);
    area.setText("this is a test.");
    String charsToHighlight = "aeiouAEIOU";
    Highlighter h = area.getHighlighter();
    h.removeAllHighlights();//from   w  w w  . jav a  2  s. c  o  m
    String text = area.getText().toUpperCase();
    for (int i = 0; i < text.length(); i += 1) {
        char ch = text.charAt(i);
        if (charsToHighlight.indexOf(ch) >= 0)
            try {
                h.addHighlight(i, i + 1, DefaultHighlighter.DefaultPainter);
            } catch (Exception ble) {
            }
    }
    this.getContentPane().add(area);

}

From source file:testFileHandler.java

public void writeFile(String path, javax.swing.JTextArea textField) throws IOException {

    FileWriter fw = new FileWriter(path); //create a file writer to write to the path
    try (PrintWriter pw = new PrintWriter(fw)) {

        String s[] = textField.getText().split("\\r?\\n"); //get the text fileds characters line by line and put them into an arry so that we can write those text fields as it is.
        ArrayList<String> arrList = new ArrayList<>(Arrays.asList(s));
        for (int i = 0; i < textField.getLineCount(); i++) {
            pw.println(arrList.get(i)); //get the string in that array one by one and write them into the file that opened.
        }/*w w  w. j av  a2 s .c  o  m*/
        pw.close(); // close the file when writting is done.
    } catch (Exception e) {
        System.out.println(e);
    }

}