Example usage for javax.swing.event CaretListener CaretListener

List of usage examples for javax.swing.event CaretListener CaretListener

Introduction

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

Prototype

CaretListener

Source Link

Usage

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);
        }/*  w w w .j  a v a  2 s.co  m*/
    });
}

From source file:CaretEeventListener.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField textField = new JTextField();

    textField.addCaretListener(new CaretListener() {

        public void caretUpdate(CaretEvent e) {
            System.out.println(e);

        }/*from ww  w.ja v a  2s  .  c om*/
    });

    frame.add(new JScrollPane(textField));

    frame.setSize(300, 200);
    frame.setVisible(true);
}

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   w  w w. j  av  a  2 s.c  o m*/
    };

    textArea.addCaretListener(listener);

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

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());
        }/* w w w . jav  a 2  s  .c o m*/
    };

    textArea.addCaretListener(listener);

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

From source file:Main.java

public static void main(String args[]) {
    final JTextField textField = new JTextField(15);
    JButton buttonCut = new JButton("Cut");
    JButton buttonPaste = new JButton("Paste");
    JButton buttonCopy = new JButton("Copy");

    JFrame jfrm = new JFrame("Cut, Copy, and Paste");
    jfrm.setLayout(new FlowLayout());
    jfrm.setSize(230, 150);/*from  w ww . jav a  2  s  .  c  o m*/
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    buttonCut.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent le) {
            textField.cut();
        }
    });

    buttonPaste.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent le) {
            textField.paste();
        }
    });

    buttonCopy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent le) {
            textField.copy();
        }
    });

    textField.addCaretListener(new CaretListener() {
        public void caretUpdate(CaretEvent ce) {
            System.out.println("All text: " + textField.getText());
            if (textField.getSelectedText() != null)
                System.out.println("Selected text: " + textField.getSelectedText());
            else
                System.out.println("Selected text: ");
        }
    });

    jfrm.add(textField);
    jfrm.add(buttonCut);
    jfrm.add(buttonPaste);
    jfrm.add(buttonCopy);
    jfrm.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);/*  w  w  w. java 2 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:Main.java

public Main() {
    setSize(300, 200);/*w  w  w  .j  av  a2s .co m*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    final JTextPane textPane = new JTextPane();
    add(textPane);
    textPane.addCaretListener(new CaretListener() {
        public void caretUpdate(CaretEvent e) {
            Highlight[] h = textPane.getHighlighter().getHighlights();
            for (int i = 0; i < h.length; i++) {
                System.out.println(h[i].getStartOffset());
                System.out.println(h[i].getEndOffset());
            }

        }
    });
}

From source file:Main.java

Main() {
    JFrame jfrm = new JFrame("Demonstrate a Text Field");
    jfrm.setLayout(new FlowLayout());
    jfrm.setSize(240, 140);/* w w w.  java  2s .  c om*/
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jtf.setActionCommand("TF");

    jtf.addActionListener(this);
    jbtnGetTextUpper.addActionListener(this);

    jtf.addCaretListener(new CaretListener() {
        public void caretUpdate(CaretEvent ce) {
            System.out.println("Text in real time: " + jtf.getText());
        }
    });

    jfrm.add(jtf);
    jfrm.add(jbtnGetTextUpper);
    jfrm.setVisible(true);
}

From source file:Main.java

public Main() {
    JFrame jfrm = new JFrame("Cut, Copy, and Paste");
    jfrm.setLayout(new FlowLayout());
    jfrm.setSize(230, 150);// w  w w .  ja va  2 s  .com
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    jbtnCut.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent le) {
            jtf.cut();
            update();
        }
    });

    jbtnPaste.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent le) {
            jtf.paste();
            update();
        }
    });

    jbtnCopy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent le) {
            jtf.copy();
            update();
        }
    });

    jtf.addCaretListener(new CaretListener() {
        public void caretUpdate(CaretEvent ce) {
            update();
        }
    });

    jfrm.add(jtf);
    jfrm.add(jbtnCut);
    jfrm.add(jbtnPaste);
    jfrm.add(jbtnCopy);
    jfrm.setVisible(true);
}

From source file:io.github.jeremgamer.editor.panels.GeneralSettings.java

public GeneralSettings() {
    this.setBorder(BorderFactory.createTitledBorder(""));

    JPanel namePanel = new JPanel();
    JLabel nameLabel = new JLabel("Nom :");
    namePanel.add(nameLabel);/* ww  w.  ja  v  a  2  s  .c o  m*/
    name.setPreferredSize(new Dimension(220, 30));
    namePanel.add(name);
    CaretListener caretUpdateName = new CaretListener() {
        public void caretUpdate(javax.swing.event.CaretEvent e) {
            JTextField text = (JTextField) e.getSource();
            gs.set("name", text.getText());
        }
    };
    name.addCaretListener(caretUpdateName);
    this.add(namePanel);

    adress.setEditable(false);
    CaretListener caretUpdateAdress = new CaretListener() {
        public void caretUpdate(javax.swing.event.CaretEvent e) {
            JTextField text = (JTextField) e.getSource();
            gs.set("adress", text.getText());
        }
    };
    adress.addCaretListener(caretUpdateAdress);
    JPanel subTypePanel = new JPanel();
    JLabel typeLabel = new JLabel("Type :");
    subTypePanel.add(typeLabel);
    type.setPreferredSize(new Dimension(220, 30));
    type.addItem("Minecraft classique");
    type.addItem("Minecraft personnalis");
    if (new File("projects/" + Editor.getProjectName() + "/data.zip").exists()) {
        type.setSelectedIndex(1);
        browse.setEnabled(true);
        browse.setText("Supprimer l'import");
    } else {
        browse.setEnabled(false);
    }
    type.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            @SuppressWarnings("unchecked")
            JComboBox<String> combo = (JComboBox<String>) event.getSource();
            if (combo.getSelectedIndex() == 1) {
                browse.setEnabled(true);
                adress.setEnabled(true);
                adress.setEditable(true);
            } else {
                browse.setEnabled(false);
                adress.setEnabled(false);
                adress.setEditable(false);
            }
            gs.set("type", combo.getSelectedIndex());
        }

    });
    subTypePanel.add(type);
    JPanel typePanel = new JPanel();
    typePanel.setLayout(new BoxLayout(typePanel, BoxLayout.PAGE_AXIS));

    typePanel.add(subTypePanel);
    JPanel browsePanel = new JPanel();
    browsePanel.add(browse);
    JPanel adressPanel = new JPanel();
    adressPanel.setLayout(new BoxLayout(adressPanel, BoxLayout.PAGE_AXIS));
    JLabel adressLabel = new JLabel("Adresse de tlchargement :");
    adressPanel.setPreferredSize(new Dimension(0, 47));
    adress.setPreferredSize(new Dimension(0, 30));
    adressPanel.add(adressLabel);
    adressPanel.add(adress);
    typePanel.add(adressPanel);

    this.add(typePanel);
    closeOnStart.setSelected(true);
    closeOnStart.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            if (closeOnStart.isSelected()) {
                gs.set("close", true);
            } else {
                gs.set("close", false);
            }
        }

    });
    this.add(closeOnStart);

    JPanel look = new JPanel();
    look.setBorder(BorderFactory.createTitledBorder("Apparence"));
    look.setPreferredSize(new Dimension(290, 340));
    JPanel colors = new JPanel();
    cDark.setSelected(true);
    bg.add(cLight);
    bg.add(cDark);
    colors.add(cLight);
    colors.add(cDark);
    cLight.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            gs.set("color", 0);
        }

    });
    cDark.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            gs.set("color", 1);
        }

    });
    look.add(colors);
    JPanel checks = new JPanel();
    checks.setLayout(new BoxLayout(checks, BoxLayout.PAGE_AXIS));
    borders.setSelected(true);
    borders.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            if (borders.isSelected()) {
                gs.set("borders", true);
            } else {
                gs.set("borders", false);
            }
        }

    });
    resize.setSelected(true);
    resize.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            if (resize.isSelected()) {
                gs.set("resizable", true);
            } else {
                gs.set("resizable", false);
            }
        }

    });
    alwaysOnTop.setSelected(false);
    alwaysOnTop.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            if (alwaysOnTop.isSelected()) {
                gs.set("top", true);
            } else {
                gs.set("top", false);
            }
        }

    });
    checks.add(borders);
    checks.add(resize);
    checks.add(alwaysOnTop);
    checks.setPreferredSize(new Dimension(270, 60));

    JPanel size = new JPanel();
    width.setPreferredSize(new Dimension(57, 30));
    widthMin.setPreferredSize(new Dimension(57, 30));
    widthMax.setPreferredSize(new Dimension(57, 30));
    height.setPreferredSize(new Dimension(57, 30));
    heightMin.setPreferredSize(new Dimension(57, 30));
    heightMax.setPreferredSize(new Dimension(57, 30));
    JPanel widthPanel = new JPanel();
    widthPanel.setPreferredSize(new Dimension(130, 150));
    widthPanel.setBorder(BorderFactory.createTitledBorder("Largeur"));
    widthPanel.setLayout(new BoxLayout(widthPanel, BoxLayout.PAGE_AXIS));
    JPanel widthPanelBase = new JPanel();
    widthPanelBase.add(new JLabel("Base :"));
    widthPanelBase.add(width);
    JPanel widthPanelMin = new JPanel();
    widthPanelMin.add(new JLabel("Min :"));
    widthPanelMin.add(Box.createRigidArea(new Dimension(5, 1)));
    widthPanelMin.add(widthMin);
    JPanel widthPanelMax = new JPanel();
    widthPanelMax.add(new JLabel("Max :"));
    widthPanelMax.add(Box.createRigidArea(new Dimension(3, 1)));
    widthPanelMax.add(widthMax);
    widthPanel.add(widthPanelBase);
    widthPanel.add(widthPanelMin);
    widthPanel.add(widthPanelMax);

    JPanel heightPanel = new JPanel();
    heightPanel.setPreferredSize(new Dimension(130, 150));
    heightPanel.setBorder(BorderFactory.createTitledBorder("Hauteur"));
    heightPanel.setLayout(new BoxLayout(heightPanel, BoxLayout.PAGE_AXIS));
    JPanel heightPanelBase = new JPanel();
    heightPanelBase.add(new JLabel("Base :"));
    heightPanelBase.add(height);
    JPanel heightPanelMin = new JPanel();
    heightPanelMin.add(new JLabel("Min :"));
    heightPanelMin.add(Box.createRigidArea(new Dimension(5, 1)));
    heightPanelMin.add(heightMin);
    JPanel heightPanelMax = new JPanel();
    heightPanelMax.add(new JLabel("Max :"));
    heightPanelMax.add(Box.createRigidArea(new Dimension(3, 1)));
    heightPanelMax.add(heightMax);
    heightPanel.add(heightPanelBase);
    heightPanel.add(heightPanelMin);
    heightPanel.add(heightPanelMax);
    size.add(widthPanel);
    size.add(heightPanel);

    width.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            JSpinner spinner = (JSpinner) e.getSource();
            gs.set("width", spinner.getValue());
        }
    });
    widthMin.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            JSpinner spinner = (JSpinner) e.getSource();
            gs.set("widthMin", spinner.getValue());
        }
    });
    widthMax.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            JSpinner spinner = (JSpinner) e.getSource();
            ;
            gs.set("widthMax", spinner.getValue());
        }
    });
    height.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            JSpinner spinner = (JSpinner) e.getSource();
            gs.set("height", spinner.getValue());
        }
    });
    heightMin.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            JSpinner spinner = (JSpinner) e.getSource();
            gs.set("heightMin", spinner.getValue());
        }
    });
    heightMax.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            JSpinner spinner = (JSpinner) e.getSource();
            ;
            gs.set("heightMax", spinner.getValue());
        }
    });

    look.add(checks);
    look.add(size);

    JPanel bottom = new JPanel();
    bottom.setLayout(new BoxLayout(bottom, BoxLayout.LINE_AXIS));
    JButton music = new JButton("Ajouter de la musique");
    music.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            new MusicFrame((JFrame) SwingUtilities.windowForComponent(adress), gs);
        }

    });
    bottom.add(music);
    JButton icons = new JButton("Icnes");
    icons.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            new IconFrame((JFrame) SwingUtilities.windowForComponent(adress));
        }

    });
    bottom.add(icons);

    look.add(bottom);

    this.add(look);

    load();
}