Example usage for javax.swing JTextArea setCaretPosition

List of usage examples for javax.swing JTextArea setCaretPosition

Introduction

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

Prototype

@BeanProperty(bound = false, description = "the caret position")
public void setCaretPosition(int position) 

Source Link

Document

Sets the position of the text insertion caret for the TextComponent.

Usage

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    final String START_STRING = "Start\n";
    final int START_STRING_LENGTH = START_STRING.length();

    JFrame frame = new JFrame("Navigation Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextArea textArea = new JTextArea(START_STRING);
    textArea.setCaretPosition(START_STRING_LENGTH);
    JScrollPane scrollPane = new JScrollPane(textArea);
    frame.add(scrollPane, BorderLayout.CENTER);

    NavigationFilter filter = new NavigationFilter() {
        public void setDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) {
            if (dot < START_STRING_LENGTH) {
                fb.setDot(START_STRING_LENGTH, bias);
            } else {
                fb.setDot(dot, bias);/*from w  w w .  java 2 s .  c o m*/
            }
        }

        public void moveDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) {
            if (dot < START_STRING_LENGTH) {
                fb.setDot(START_STRING_LENGTH, bias);
            } else {
                fb.setDot(dot, bias);
            }
        }
    };

    textArea.setNavigationFilter(filter);

    frame.setSize(250, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String argv[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String testStr = "Paste text here.";
    JTextArea wrapArea = new JTextArea(testStr, 20, 40);
    wrapArea.setLineWrap(true);// ww  w  . j a v  a2 s .c  om
    wrapArea.setWrapStyleWord(true);
    wrapArea.setCaretPosition(testStr.length());
    frame.add(new JScrollPane(wrapArea));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    String text = "one\ntwo\nthree\nfour\nfive";
    JFrame frame = new JFrame("title");
    JTextArea textArea = new JTextArea(text, 1, 30); // shows only one line
    JScrollPane scrollPane = new JScrollPane(textArea);
    frame.add(scrollPane);/*from  w w  w.  j  av a 2  s .c om*/
    frame.pack();
    frame.setVisible(true);

    final JViewport viewport = scrollPane.getViewport();

    textArea.addCaretListener(e -> {
        System.out.println("First : " + viewport.getViewPosition());
        System.out.println("Second: " + viewport.getViewPosition());
    });
    textArea.setCaretPosition(text.length());
}

From source file:com.mindcognition.mindraider.ui.swing.concept.annotation.renderer.RichTextAnnotationRenderer.java

private void insertStringToAnnotation(String stringToInsert) {
    JTextArea editor = richTextAnnotationRenderer.editor;
    try {//  w ww. j  av a 2s .co  m
        final int caretPosition = editor.getCaretPosition();
        // link & htmlize
        String linked = editor.getDocument().getText(0, caretPosition) + stringToInsert
                + editor.getDocument().getText(caretPosition, editor.getDocument().getLength() - caretPosition);
        linked = linked.trim();
        // set to UI again
        editor.setText(linked);
        editor.setCaretPosition(caretPosition);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:hermes.browser.actions.AbstractFIXBrowserDocumentComponent.java

protected Component createPrettyPrintPanel(FIXMessage m) {
    final JTextArea textArea = new JTextArea();

    textArea.setEditable(false);//from  w  w w  . j  a v a 2s  . c o  m
    textArea.setFont(Font.decode("Monospaced-PLAIN-12"));

    byte[] bytes = null;

    try {
        textArea.setText(FIXUtils.prettyPrint(m));
    } catch (Throwable e) {
        textArea.setText(e.getMessage());

        log.error("exception converting message to byte[]: ", e);
    }

    textArea.setCaretPosition(0);

    return SwingUtils.createJScrollPane(textArea);
}

From source file:hermes.browser.actions.AbstractFIXBrowserDocumentComponent.java

protected Component createHexPanel(FIXMessage m) {
    final JTextArea textArea = new JTextArea();

    textArea.setEditable(false);/*from w w w . ja  v  a2 s .  c o  m*/
    textArea.setFont(Font.decode("Monospaced-PLAIN-12"));

    byte[] bytes = null;

    try {
        textArea.setText(DumpUtils.dumpBinary(m.getBytes(), DumpUtils.DUMP_AS_HEX_AND_ALPHA));
    } catch (Throwable e) {
        textArea.setText(e.getMessage());

        log.error("exception converting message to byte[]: ", e);
    }

    textArea.setCaretPosition(0);

    final JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(textArea);

    return scrollPane;
}

From source file:com.github.lindenb.jvarkit.tools.bamviewgui.BamFileRef.java

BamInternalFrame(BamFileRef ref) {
    super(ref.bamFile.getName(), true, false, true, true);
    this.ref = ref;
    JPanel mainPane = new JPanel(new BorderLayout(5, 5));
    setContentPane(mainPane);/*from   w w w .j  a  va  2 s. c om*/
    JTabbedPane tabbedPane = new JTabbedPane();
    mainPane.add(tabbedPane, BorderLayout.CENTER);

    JPanel pane = new JPanel(new BorderLayout(5, 5));
    tabbedPane.addTab("BAM", pane);

    this.tableModel = new BamTableModel();
    this.jTable = createTable(tableModel);
    this.jTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    this.jTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    JScrollPane scroll1 = new JScrollPane(this.jTable);

    this.infoTableModel = new FlagTableModel();
    JTable tInfo = createTable(this.infoTableModel);

    this.genotypeTableModel = new SAMTagAndValueModel();
    JTable tGen = createTable(this.genotypeTableModel);

    this.groupTableModel = new ReadGroupTableModel();
    JTable tGrp = createTable(this.groupTableModel);

    JPanel splitH = new JPanel(new GridLayout(1, 0, 5, 5));
    splitH.add(new JScrollPane(tInfo));
    splitH.add(new JScrollPane(tGen));
    splitH.add(new JScrollPane(tGrp));

    JSplitPane splitVert = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scroll1, splitH);

    this.jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting())
                return;
            int row = jTable.getSelectedRow();
            SAMRecord ctx;
            if (row == -1 || (ctx = tableModel.getElementAt(row)) == null) {
                infoTableModel.setContext(null);
                genotypeTableModel.setContext(null);
                groupTableModel.setContext(null);
            } else {
                infoTableModel.setContext(ctx);
                genotypeTableModel.setContext(ctx);
                groupTableModel.setContext(ctx);
            }

        }
    });

    pane.add(splitVert);

    //header as text
    pane = new JPanel(new BorderLayout(5, 5));
    tabbedPane.addTab("Header", pane);
    JTextArea area = new JTextArea(String.valueOf(ref.header.getTextHeader()));
    area.setCaretPosition(0);
    area.setEditable(false);
    pane.add(new JScrollPane(area), BorderLayout.CENTER);

    //dict
    pane = new JPanel(new BorderLayout(5, 5));
    tabbedPane.addTab("Reference", pane);
    JTable dictTable = createTable(new SAMSequenceDictionaryTableModel(ref.header.getSequenceDictionary()));
    dictTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    pane.add(new JScrollPane(dictTable), BorderLayout.CENTER);

    this.selList = new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting())
                return;
            listSelectionChanged();
        }
    };

    this.addInternalFrameListener(new InternalFrameAdapter() {
        @Override
        public void internalFrameActivated(InternalFrameEvent e) {
            jTable.getSelectionModel().addListSelectionListener(selList);
        }

        @Override
        public void internalFrameDeactivated(InternalFrameEvent e) {
            jTable.getSelectionModel().removeListSelectionListener(selList);
        }
    });
}

From source file:hermes.renderers.DefaultMessageRenderer.java

/**
 * Show a BytesMessage either as a java object or just a size.
 * /*  ww w . ja va2  s  . com*/
 * @param parent
 * 
 * @param bytesMessage
 * @return
 * @throws JMSException
 * @throws IOException
 * @throws ClassNotFoundException
 */
protected JComponent handleBytesMessage(JScrollPane parent, BytesMessage bytesMessage)
        throws JMSException, IOException, ClassNotFoundException {
    final MyConfig currentConfig = (MyConfig) getConfig();

    JTextArea textPane = new MyTextArea();

    textPane.setEditable(false);
    textPane.setWrapStyleWord(true);
    textPane.setLineWrap(true);
    bytesMessage.reset();

    if (currentConfig.isBytesIsObject()) {
        final byte[] bytes = MessageUtils.asBytes(bytesMessage);
        final ByteArrayInputStream bistream = new ByteArrayInputStream(bytes);
        final ObjectInputStream oistream = new ObjectInputStream(bistream);
        final Object o = oistream.readObject();

        textPane.setText(o.toString());
    } else if (currentConfig.isBytesIsString()) {
        try {
            String text = new String(MessageUtils.asBytes(bytesMessage), currentConfig.getBytesEncoding());
            textPane.setText(text);
            return textPane;
        } catch (JMSException e) {
            textPane.setText(e.getMessage());
        }
    } else {
        HexMessageRenderer renderer = new HexMessageRenderer();
        textPane = (JTextArea) renderer.render(parent, bytesMessage); // Hack.
    }

    textPane.setCaretPosition(0);

    return textPane;
}

From source file:edu.ku.brc.specify.tasks.subpane.wb.DataImportDialog.java

/**
* Takes the list of data import errors and displays then to the user
* 
* void/*from w ww .  j av a 2s . c o  m*/
*/
protected void showErrors() {
    JList listOfErrors = genListOfErrorWhereTableDataDefiesSizeConstraints(model.getColumnNames(),
            model.getData());

    if ((model.getColumnNames() == null) || (model.getData() == null) || (listOfErrors == null)
            || (listOfErrors.getModel().getSize() == 0)) {
        JTextArea textArea = new JTextArea();
        textArea.setRows(25);
        textArea.setColumns(60);
        //String newline = "\n";
        //for (int i = 0; i < listOfErrors.getModel().getSize(); i++)
        //{
        textArea.append(getResourceString("WB_PARSE_FILE_ERROR2"));
        //}
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setEditable(false);
        textArea.setCaretPosition(0);
        JScrollPane pane = new JScrollPane(textArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        JOptionPane.showMessageDialog(UIRegistry.getTopWindow(), pane, getResourceString("DATA_IMPORT_ISSUES"),
                JOptionPane.WARNING_MESSAGE);
        okBtn.setEnabled(false);
    } else if (listOfErrors.getModel().getSize() > 0) {
        JTextArea textArea = new JTextArea();
        textArea.setRows(25);
        textArea.setColumns(60);
        String newline = "\n";
        for (int i = 0; i < listOfErrors.getModel().getSize(); i++) {
            textArea.append((String) listOfErrors.getModel().getElementAt(i) + newline + newline);
        }
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setEditable(false);
        textArea.setCaretPosition(0);
        JScrollPane pane = new JScrollPane(textArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        JOptionPane.showMessageDialog(UIRegistry.getTopWindow(), pane, getResourceString("DATA_IMPORT_ISSUES"),
                JOptionPane.WARNING_MESSAGE);
    }
}

From source file:hermes.renderers.DefaultMessageRenderer.java

/**
 * Show the TextMessage in a JTextArea./* www. j  ava 2 s  . co  m*/
 * 
 * @param textMessage
 * @return
 * @throws JMSException
 */
protected JComponent handleTextMessage(final TextMessage textMessage) throws JMSException {
    //
    // Show the text in a JTextArea, you can edit the message in place and
    // then drop it onto another queue/topic.

    final String text = textMessage.getText();
    final JTextArea textPane = new JTextArea();

    // final CharBuffer bytes = CharBuffer.wrap(text.subSequence(0,
    // text.length())) ;
    // final JTextArea textPane = new MyTextArea(new PlainDocument(new
    // MappedStringContent(bytes))) ;

    textPane.setEditable(false);
    textPane.setFont(Font.decode("Monospaced-PLAIN-12"));
    textPane.setLineWrap(true);
    textPane.setWrapStyleWord(true);

    textPane.append(text);

    textPane.getDocument().addDocumentListener(new DocumentListener() {
        public void doChange() {
            try {
                textMessage.setText(textPane.getText());
            } catch (JMSException e) {
                JOptionPane.showMessageDialog(textPane, "Unable to update the TextMessage: " + e.getMessage(),
                        "Error modifying message content", JOptionPane.ERROR_MESSAGE);

                try {
                    textPane.setText(textMessage.getText());
                } catch (JMSException e1) {
                    log.error(e1.getMessage(), e1);
                }

                textPane.setEditable(false);
                textPane.getDocument().removeDocumentListener(this);
            }
        }

        @Override
        public void changedUpdate(DocumentEvent arg0) {
            doChange();
        }

        @Override
        public void insertUpdate(DocumentEvent arg0) {
            doChange();
        }

        @Override
        public void removeUpdate(DocumentEvent arg0) {
            doChange();
        }
    });

    textPane.setCaretPosition(0);

    return textPane;
}