見た目で分かるとおり、JEditorPaneにhtmlファイルを表示したときフォームの部品として使用されるのはswingコンポーネントです。 これらのコンポーネントはJEditorPaneの子をリストアップすればアクセスできます。
ただし、タグとコンポーネントの結びつきを調べる方法は限定されます。 submitボタンなどはテキストを確認すれば判別できます。 しかしその他のコンポーネントでこの方法は使えません。 表示される順番で特定することもできますが、あまり賢い方法ではないでしょう。 一応こんな事もできるよってことで紹介しますが、実際にコーディングするときは別の項目を参考にしてください。
子コンポーネントが追加されるタイミングについて、詳しくは調べていません。 JEditorPaneにsetPageした直後にはまだ子コンポーネントが構築されていません。 サンプルコードではJFrameが初めて表示されるときにリストアップしていますが、より適切なイベントがあるかもしれません。 興味のある方は調べてみてください。 ちなみにHTMLDocumentのDocumentEvent中にJEditorPaneに対してgetComponentsを使うと、そのスレッドは固まってしまいます。 別のイベントを探す必要がありそうです。
以下はサンプルコードです。 読み込まれたhtmlに配置されたボタンを押すと、ボタンのテキストをダイアログで表示します。 htmlファイルなどはネタのトップからダウンロードしてください。 手抜きコードなのでJListに付いているスクロールバーのボタンなど、JButtonなら全て反応してしまいます。
// ChildrenTest.java
import java.io.File;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;
public class ChildrenTest extends JFrame implements ActionListener
{
public static final String TEST_HTML = "jeditorpane_test.html";
public static final String TEST_CSS = "jeditorpane_test.css";
private JEditorPane _editorPane;
private WindowAdapter _windowAdapter = new WindowAdapter()
{
public void windowOpened(WindowEvent event)
{
checkChildComponents(_editorPane);
}
};
public ChildrenTest()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
try
{
_editorPane = new JEditorPane();
_editorPane.setEditable(false);
_editorPane.setContentType("text/html");
HTMLEditorKit editorKit = (HTMLEditorKit)_editorPane.getEditorKit();
editorKit.setAutoFormSubmission(false);
File cssFile = new File(TEST_CSS);
editorKit.getStyleSheet().importStyleSheet(cssFile.toURI().toURL() );
File htmlFile = new File(TEST_HTML);
_editorPane.setPage(htmlFile.toURI().toURL() );
JScrollPane scrollPane = new JScrollPane(_editorPane);
getContentPane().add(scrollPane);
addWindowListener(_windowAdapter);
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
public static void main(String[] args)
{
ChildrenTest app = new ChildrenTest();
app.setSize(640, 480);
app.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(this, event.getActionCommand() );
}
private void checkChildComponents(Container container)
{
Component[] comps = container.getComponents();
for(Component child : comps)
{
if(child instanceof JButton)
{
( (JButton)child).addActionListener(this);
}
else if(child instanceof Container)
{
checkChildComponents( (Container)child);
}
}
}
}