Example usage for javax.swing.event CaretEvent getMark

List of usage examples for javax.swing.event CaretEvent getMark

Introduction

In this page you can find the example usage for javax.swing.event CaretEvent getMark.

Prototype

public abstract int getMark();

Source Link

Document

Fetches the location of other end of a logical selection.

Usage

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Caret Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

    CaretListener listener = new CaretListener() {
        public void caretUpdate(CaretEvent caretEvent) {
            System.out.println("dot:" + caretEvent.getDot());
            System.out.println("mark" + caretEvent.getMark());
        }//from   ww  w.java  2s.co m
    };

    textArea.addCaretListener(listener);

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

From source file:Main.java

public static void main(String[] argv) {
    JTextComponent textComp = new JTextArea();
    textComp.addCaretListener(new CaretListener() {
        public void caretUpdate(CaretEvent e) {

            int dot = e.getDot();
            System.out.println("dot is the caret position:" + dot);

            int mark = e.getMark();
            System.out.println("mark is the non-caret end of the selection: " + mark);
        }//from  ww  w  .  ja  v  a 2 s . co  m
    });
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Caret Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

    CaretListener listener = new CaretListener() {
        public void caretUpdate(CaretEvent caretEvent) {
            System.out.println("Dot: " + caretEvent.getDot());
            System.out.println("Mark: " + caretEvent.getMark());
        }/*from  ww  w .  j a  v a 2s .  c o  m*/
    };

    textArea.addCaretListener(listener);

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

From source file:CaretSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Caret Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = frame.getContentPane();
    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    content.add(scrollPane, BorderLayout.CENTER);

    final JTextField dot = new JTextField();
    dot.setEditable(false);//from   w w  w . j  av  a2  s.  c o  m
    JPanel dotPanel = new JPanel(new BorderLayout());
    dotPanel.add(new JLabel("Dot: "), BorderLayout.WEST);
    dotPanel.add(dot, BorderLayout.CENTER);
    content.add(dotPanel, BorderLayout.NORTH);

    final JTextField mark = new JTextField();
    mark.setEditable(false);
    JPanel markPanel = new JPanel(new BorderLayout());
    markPanel.add(new JLabel("Mark: "), BorderLayout.WEST);
    markPanel.add(mark, BorderLayout.CENTER);
    content.add(markPanel, BorderLayout.SOUTH);

    CaretListener listener = new CaretListener() {
        public void caretUpdate(CaretEvent caretEvent) {
            dot.setText("" + caretEvent.getDot());
            mark.setText("" + caretEvent.getMark());
        }
    };

    textArea.addCaretListener(listener);

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

From source file:net.team2xh.crt.gui.editor.EditorTextPane.java

public EditorTextPane() {

    // Initialize highlighters
    lh = new LineHighlighter(Theme.getTheme().COLOR_13);
    ll = new LineHighlighter(Theme.getTheme().COLOR_14);
    wh = new WordHighlighter(Theme.getTheme().COLOR_11);
    eh = new ErrorHighlighter(Color.RED);

    // Initialise colors
    initAttributeSets();//  w w w.j  av a2  s . c om

    setOpaque(false); // Background will be drawn later on
    setFont(font);

    doc = (DefaultStyledDocument) getDocument();

    // Replace all tabs with four spaces
    // TODO: tab to next multiple of 4 column
    // TODO: tab whole selection
    // TODO: insert matching brace
    doc.setDocumentFilter(new DocumentFilter() {
        private String process(int offset, String text) {
            return text.replaceAll("\t", "    ").replaceAll("\r", "");
        }

        @Override
        public void insertString(FilterBypass fb, int offset, String text, AttributeSet attr)
                throws BadLocationException {
            super.insertString(fb, offset, process(offset, text), attr);
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attr)
                throws BadLocationException {
            super.replace(fb, offset, length, process(offset, text), attr);
        }
    });

    // Highlight text when text changes
    doc.addDocumentListener(new DocumentListener() {

        @Override
        public void removeUpdate(DocumentEvent e) {
            SwingUtilities.invokeLater(() -> highlightText());
            changed = true;
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            SwingUtilities.invokeLater(() -> highlightText());
            changed = true;
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
        }
    });

    addCaretListener(new CaretListener() {
        private boolean isAlphanum(char c) {
            return Character.isDigit(c) || Character.isLetter(c);
        }

        private boolean isAlphanum(String s) {
            for (char c : s.toCharArray()) // Allow dots in middle of words for floats
            {
                if (c != '.' && !isAlphanum(c)) {
                    return false;
                }
            }

            return true;
        }

        @Override
        public void caretUpdate(CaretEvent ce) {
            try {

                // Highlight current line
                highlightCurrentLine();

                // Clear previously highlighted occurrences
                for (Object o : occurrences) {
                    getHighlighter().removeHighlight(o);
                }
                repaint();
                occurrences.clear();

                // Get start and end offsets, swap them if necessary
                int s = ce.getDot();
                int e = ce.getMark();

                if (s > e) {
                    s = s + e;
                    e = s - e;
                    s = s - e;
                }

                // If there is a selection,
                if (s != e) {
                    // Check if the char on the left and on the right are not alphanums
                    char f = s == 0 ? ' ' : doc.getText(s - 1, 1).charAt(0);
                    char l = s == doc.getLength() - 1 ? ' ' : doc.getText(e, 1).charAt(0);
                    if (!isAlphanum(f) && !isAlphanum(l)) {
                        String word = doc.getText(s, e - s);
                        if (isAlphanum(word)) {
                            highlightOccurrences(word, s, e);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    setCaretColor(Theme.getTheme().COLOR_11);

    highlightText();
}