Catching an event when a form SUBMIT button has been clicked on a JTextPane

Set a custom HTMLEditorKit on your JTextPane. In the example below, this is MyHTMLEditorKit. It contains a factory that is responsible for returning a custom FormView. In MyFormView, you can override the method submitData that is called when the submit button is pressed. The form data is passed to this method in a escaped form.

Main.java:

import javax.swing.text.html.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.awt.*;
import java.io.*;
 
public class Main extends JFrame
{
   final JTextField urlTextField = new JTextField();
   final JEditorPane htmlPane = new JEditorPane();
   JButton urlButton = new JButton("Go!");
 
   public Main() {
      htmlPane.setEditable(false);
      urlTextField.setText("file:c:\test.html");
 
      urlButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            loadPage(urlTextField.getText());
         }
      });
 
      htmlPane.setEditorKit(new MyHTMLEditorKit());
 
      getContentPane().setLayout(new BorderLayout());
      JPanel topPanel = new JPanel(new BorderLayout());
      topPanel.add(BorderLayout.CENTER, urlTextField);
      topPanel.add(BorderLayout.EAST, urlButton);
 
      getContentPane().add(BorderLayout.NORTH, topPanel);
      getContentPane().add(BorderLayout.CENTER, new JScrollPane(htmlPane));
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
 
      setSize(400, 400);
   }
 
   public void loadPage(String url) {
      try {
         htmlPane.setPage(new URL(urlTextField.getText()));
      }
      catch(Exception e) {
         System.out.println(e);
      }
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setVisible(true);
   }
}

MyHTMLEditorKit.java:

import javax.swing.text.html.*;
import javax.swing.text.*;
 
public class MyHTMLEditorKit extends HTMLEditorKit {
   private static final ViewFactory customFactory = new MyHTMLFactory();
 
   public ViewFactory getViewFactory() {
      return customFactory;
   }
 
   public static class MyHTMLFactory extends HTMLEditorKit.HTMLFactory
   {
      public View create(Element elem) {
         Object o = elem.getAttributes().getAttribute(StyleConstants.NameAttribute);
 
         if (o instanceof HTML.Tag) {
            HTML.Tag kind = (HTML.Tag) o;
            if (kind == HTML.Tag.INPUT || kind == HTML.Tag.SELECT || kind == HTML.Tag.TEXTAREA) {
               return new MyFormView(elem);
            }
            else return super.create(elem);
         }
 
         return super.create(elem);
      }
   }
}

MyFormView.java:

import javax.swing.text.html.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.util.*;
 
public class MyFormView extends FormView
{
   public MyFormView(Element elem) {
      super(elem);
   }
 
   public void actionPerformed(ActionEvent event) {
      super.actionPerformed(event);
   }
 
   public void submitData(String data) {
      System.out.println("Escaped data: " + data);
   }
}

A simple HTML file to test it with: test.html:

<html>
<body>
 
<form METHOD=POST ACTION="performyourownaction">
 
   <i>Subcategory:</i><br>
   <SELECT NAME="subcat">
      <OPTION> J2SE </OPTION>
      <OPTION> J2EE </OPTION>
      <OPTION> J2ME </OPTION>
      <OPTION> Other </OPTION>
   </SELECT>
   <p>
   <i>Subject:</i><br>
      <input type=text size=80 maxsize=200 name="subject"><p>
   <i>Question:</i><br>
      <textarea name="question" cols=60 rows=4></textarea><p>
 
   <br><br>
 
   <input TYPE=SUBMIT VALUE=" Submit Question! ">
</form>
 
</body>
</html>