Search code examples
javaswingjtextarea

How to switch between lines in JTextArea in swing


Am trying to set the goto line number for switching between lines in my swing application. I got the number of lines in the JTextArea instance using getLineCount() but don't know how to switch between lines?

Could any one suggest me?


Solution

  • You can set the caret position with textArea.setCaretPosition. Something like this would work

    textArea.setCaretPosition(textArea.getDocument().getDefaultRootElement()
                             .getElement(index).getStartOffset());
    

    In the example below, I just use a JComboBox that I populate with the indexes using the line numbers of the JTextArea. When you select on an number in combo box, the caret will move to that line of the JTextArea.

    The program is not a great program though. You would probably want to populate the ComboBoxModel dynamically with the addition and removal of lines. But this should give you the answer you're looking for.

    Disclaimer

    I haven't added any functionality to bring the scrollpane focus the the current line. You may want to look at @Balder comment below for help with that.

    enter image description here

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class TestCaret {
    
        public TestCaret() {
            JTextArea textArea = createTextArea();
            JComboBox cBox = createComboBox(textArea);
    
            JFrame frame = new JFrame();
            frame.add(new JScrollPane(textArea));
            frame.add(cBox, BorderLayout.SOUTH);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        private JComboBox createComboBox(final JTextArea textArea) {
            DefaultComboBoxModel<Integer> model  = new DefaultComboBoxModel<>();
            int lines = textArea.getLineCount();
            for (int i = 0; i < lines; i++) {
                model.addElement(i);
            }
            final JComboBox cBox = new JComboBox(model);
            cBox.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    int index = (Integer)cBox.getSelectedItem();
                    textArea.setCaretPosition(
                            textArea.getDocument().getDefaultRootElement().getElement(index).getStartOffset());
                    textArea.requestFocusInWindow();
                }
            });
            return cBox;
        }
    
        private JTextArea createTextArea() {
            JTextArea textArea = new JTextArea(10, 50);
            textArea.setMargin(new Insets(15, 15, 15, 15));
            textArea.setLineWrap(true);
            textArea.setWrapStyleWord(true);
            String text 
                    = "0 Hello World\n" + 
                    "1 Hello World\n" + 
                    "2 Hello World\n" + 
                    "3 Hello World\n" + 
                    "4 Hello World\n" + 
                    "5 Hello World\n" + 
                    "6 Hello World\n" +
                    "7 Hello World\n" + 
                    "8 Hello World\n" + 
                    "9 Hello World\n"; 
            textArea.setText(text);
            return textArea;
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                    new TestCaret();
                }
            });
        }
    }