Getting the caret row and column positions on my JTextPane

Main.java:

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
 
public class Main extends JFrame {
   public Main() {
      setTitle("CaretPosition demo");
 
      final JEditorPane ep = new JEditorPane();
  
      JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
 
      panel.add(new JLabel("Row:"));
      final JTextField rowTextField = new JTextField(5);
      rowTextField.setEditable(false);
      panel.add(rowTextField);
 
      panel.add(new JLabel("Col:"));
      final JTextField colTextField = new JTextField(5);
      colTextField.setEditable(false);
      panel.add(colTextField);
 
      getContentPane().add(BorderLayout.NORTH, panel);
      getContentPane().add(BorderLayout.CENTER, ep);
 
      ep.addCaretListener(new CaretListener() {
         public void caretUpdate(CaretEvent ce) {
            int pos = ep.getCaretPosition();
            Element map = ep.getDocument().getDefaultRootElement();
            int row = map.getElementIndex(pos);
            Element lineElem = map.getElement(row);
            int col = pos - lineElem.getStartOffset();
 
            rowTextField.setText(""+row);
            colTextField.setText(""+col);
         }
      });
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(0);
         }
      });
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(300, 300);
      main.setVisible(true);        
   }
}