2012-11-08 16 views
5

JTextPaneで小さなHTML-wysiwygを作成しようとしていますが、BackgroundActionを動作させることができません。私はJTextPaneStyledDocumentsetCharacterAttributesを使用していますが、問題があるようです。ビューはOKですが、Documentはそうではありません。JTextPaneのテキストの背景色が機能しない

ここに、問題を示す小さなデモコードがあります。 2 JTextPaneがあります

  1. 私が最初JTextPaneのテキストを取得し、二番目の

     の上に設定された第1の1

  • に私のテキストの背景色を設定します  - >同じテキストを持っていても、同じものは表示されません。

    現在選択されているテキストに背景色を設定し、JTextPaneに更新されたHTMLテキストを報告する方法はありますか?

    import java.awt.Color; 
    import java.awt.GridBagConstraints; 
    import java.awt.GridBagLayout; 
    
    import javax.swing.JFrame; 
    import javax.swing.JPanel; 
    import javax.swing.JTextPane; 
    import javax.swing.SwingUtilities; 
    import javax.swing.text.SimpleAttributeSet; 
    import javax.swing.text.StyleConstants; 
    import javax.swing.text.StyledDocument; 
    
    public class TestDifferentStyles { 
    
        private void initUI() { 
         JFrame frame = new JFrame(TestDifferentStyles.class.getSimpleName()); 
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         final JTextPane textPane = new JTextPane(); 
         final JTextPane textPane2 = new JTextPane(); 
         textPane2.setEditable(false); 
         textPane.setContentType("text/html"); 
         textPane2.setContentType("text/html"); 
         textPane.setText("<html><head></head><body><p>Hello world</p></body></html>"); 
         SimpleAttributeSet set = new SimpleAttributeSet(); 
         StyleConstants.setForeground(set, Color.GREEN); 
         StyleConstants.setBackground(set, Color.BLACK); 
         ((StyledDocument) textPane.getDocument()).setCharacterAttributes(0, textPane.getDocument().getLength(), set, false); 
    
         JPanel panel = new JPanel(new GridBagLayout()); 
         GridBagConstraints gbc = new GridBagConstraints(); 
         gbc.fill = GridBagConstraints.BOTH; 
         gbc.weightx = 1.0; 
         gbc.weighty = 1.0; 
         panel.add(textPane, gbc); 
         panel.add(textPane2, gbc); 
         frame.add(panel); 
         frame.setSize(500, 400); 
         frame.setVisible(true); 
         SwingUtilities.invokeLater(new Runnable() { 
          @Override 
          public void run() { 
           System.err.println(textPane.getText()); 
           textPane2.setText(textPane.getText()); 
          } 
         }); 
        } 
    
        public static void main(String[] args) { 
    
         javax.swing.SwingUtilities.invokeLater(new Runnable() { 
          @Override 
          public void run() { 
           new TestDifferentStyles().initUI(); 
          } 
         }); 
        } 
    
    } 
    

    出力結果(黒枠がそれぞれJTextPaneの周りにある):ここでは output result

  • +0

    は@Stanislavを待つ必要が、彼はオーバーライド介護者のためのソリューションを持っている、選択とHightLighterは、私は、これはUIManagerを使っ及びそのXxxResources、 – mKorbel

    +0

    @mKorbel OKおかげについてだと思います。私はStanislavLを待つつもりです:-) –

    +0

    チャールズ・ベルの「HTMLDocumentEditor」、[here](http://stackoverflow.com/a/5899816/230513)も参照してください。 – trashgod

    答えて

    5

    は、背景色を設定することができますアクションのためのコードです:

    public class BackgroundColorAction extends StyledEditorKit.StyledTextAction { 
    
        private Color color; 
    
        public BackgroundColorAction(Color color) { 
         super(StyleConstants.Background.toString()); 
         this.color = color; 
        } 
    
        @Override 
        public void actionPerformed(ActionEvent ae) { 
         JEditorPane editor = getEditor(ae); 
         if (editor == null) { 
          return; 
         } 
         //Add span Tag 
         String htmlStyle = "background-color:" + Util.getHTMLColor(color); 
         SimpleAttributeSet attr = new SimpleAttributeSet(); 
         attr.addAttribute(HTML.Attribute.STYLE, htmlStyle); 
         MutableAttributeSet outerAttr = new SimpleAttributeSet(); 
         outerAttr.addAttribute(HTML.Tag.SPAN, attr); 
         //Next line is just an instruction to editor to change color 
         StyleConstants.setBackground(outerAttr, this.color); 
         setCharacterAttributes(editor, outerAttr, false); 
        } 
    } 
    

    私はたくさんありました背景色の設定に問題があります。しかし最後に、私はそれを解読することができました。 " 申し訳ありません、私はサブルーチンを投稿するのを忘れました。ここに行く:

    /** 
    * Convert a Java Color to equivalent HTML Color. 
    * 
    * @param color The Java Color 
    * @return The String containing HTML Color. 
    */ 
    public static String getHTMLColor(Color color) { 
        if (color == null) { 
         return "#000000"; 
        } 
        return "#" + Integer.toHexString(color.getRGB()).substring(2).toUpperCase(); 
    } 
    
    +0

    'Util'クラスに適切なパッケージが見つかりませんでした。それはフィールドではないようです。 'Util.getHTMLColor(color);' –

    +0

    @NickRippeコードを確認する時間がまだありませんでしたが、私は推測しています。それはthieのようなものです: ''# "+ String.format("%1 $ 02x%2 $ 02x%3 $ 02x "、color.getRed()、color.getGreen()、color.getBlue())' –

    +0

    ちょうどそれを確認し、それは魅力のように動作します。 +1と受け入れられた答え。 –

    関連する問題