2011-10-31 9 views
2

編集可能でないJTextPaneを使用して、一部のデータをHTML形式で表示しています。 contentTypeを "text/html"に設定しています。今度は、JTextPaneにHTMLチェックボックスを追加し、その変更を聞き、特定のチェックボックスが選択されているかどうかを取得できるようにしたいと考えました。これは可能ですか?jTextPane(または代替)のHTMLチェックボックスを聞く?

JTextPaneののテキストは、この形式になります。

<html><form> 
<input type="checkbox" name="checkbox1" value="value" /> checkbox1<br /> 
</form></html> 

私はすべて、この目的のためにJTextPaneのを使用すること、またはより良いコントロールがありますでしょうか?通常のチェックボックスはオプションではありません。簡単にスタイルを設定するにはHTML形式が必要です。

答えて

7

一般に、JEditorPaneを使用してHTMLを表示します。

あなたの条件に応じて、これについて移動する2つの方法があります。

  1. は、Swingコンポーネントは、実際には、エディタ・ペインに追加されます。だから、ドキュメントが解析され、エディタペインがrevalidated()されたら、エディタペインに追加されたすべてのコンポーネントのリストを取得するだけです。クラス名を確認して、必要なコンポーネントを見つけることができます。

  2. HTMLDocumentには、コンポーネントモデルを含む追加された各コンポーネントに関する属性が含まれています。したがって、すべてのチェックボックスのモデルを取得するためにドキュメントを検索することができます。ここで

はあなたを始めるためにいくつかの一般的なコードです:

import java.awt.*; 
import java.util.*; 
import java.io.*; 
import javax.swing.*; 
import javax.swing.text.*; 
import javax.swing.text.html.*; 

public class GetComponent extends JFrame 
{ 
    public GetComponent() 
     throws Exception 
    { 
     FileReader reader = new FileReader("form.html"); 

     JEditorPane editor = new JEditorPane(); 
     editor.setContentType("text/html"); 
     editor.setEditable(false); 
     editor.read(reader, null); 

     JScrollPane scrollPane = new JScrollPane(editor); 
     scrollPane.setPreferredSize(new Dimension(400, 300)); 
     add(scrollPane); 

     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     pack(); 
     setLocationRelativeTo(null); 
     setVisible(true); 

     // display the attributes of the document 

     HTMLDocument doc = (HTMLDocument)editor.getDocument(); 
     ElementIterator it = new ElementIterator(doc); 
     Element element; 

     while ((element = it.next()) != null) 
     { 
      System.out.println(); 

      AttributeSet as = element.getAttributes(); 
      Enumeration enumm = as.getAttributeNames(); 

      while(enumm.hasMoreElements()) 
      { 
       Object name = enumm.nextElement(); 
       Object value = as.getAttribute(name); 
       System.out.println("\t" + name + " : " + value); 

       if (value instanceof DefaultComboBoxModel) 
       { 
        DefaultComboBoxModel model = (DefaultComboBoxModel)value; 

        for (int j = 0; j < model.getSize(); j++) 
        { 
         Object o = model.getElementAt(j); 
         Object selected = model.getSelectedItem(); 
         System.out.print("\t\t"); 

         if (o.equals(selected)) 
          System.out.println(o + " : selected"); 
         else 
          System.out.println(o); 
        } 
       } 
      } 
     } 

     // display the components added to the editor pane 

     for (Component c: editor.getComponents()) 
     { 
      Container parent = (Container)c; 
      System.out.println(parent.getComponent(0).getClass()); 
     } 
    } 

    public static void main(String[] args) 
     throws Exception 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       try 
       { 
        GetComponent frame = new GetComponent(); 
       } 
       catch(Exception e) { System.out.println(e); } 
      } 
     }); 
    } 
} 
1

JTextPaneでjavascriptイベントを処理できないと思われるので、チェックボックスの切り替えはオプションではないと思います。

関連する問題