2011-10-31 51 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。

根據您的需求有兩種方式去了解這一點:

  1. Swing組件實際上是添加到編輯窗格。因此,一旦解析了docuemnt並且編輯器窗格已被重新驗證(),您應該能夠獲取添加到編輯器窗格的所有組件的列表。您可以檢查課程名稱以查找您想要的組件。

  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事件,所以我不認爲切換複選框是一個選項。

相關問題