2012-11-08 15 views
5

Próbuję zrobić mały HTML-wysiwyg z JTextPane, ale nie mogę uzyskać BackgroundAction do pracy. Używam setCharacterAttributes na StyledDocument z JTextPane, ale wydaje się problematyczne. Widok jest w porządku, ale Document nie jest.Kolor tła tekstu JTextPane nie działa

Oto mały kod demonstracyjny pokazujący problem. Istnieją 2 JTextPane:

  1. ustawić kolor tła mojego tekstu w pierwszym
  2. odzyskać tekst pierwszej JTextPane i ustawić go na drugim

        -> Nie pokazują tego samego, chociaż mają ten sam tekst.

Czy istnieje sposób ustawienia koloru tła dla bieżącego zaznaczonego tekstu i czy raport JTextPane jest zaktualizowany w tekście 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(); 
      } 
     }); 
    } 

} 

Wynik wyjściowe (czarne obramowanie są wokół każdego JTextPane): output result

+0

trzeba czekać na @Stanislav, ma rozwiązania dla nadpisania opiekuna, selekcje i HightLighter, myślę, że to jest o UIManager i jego XxxResources, – mKorbel

+0

@mKorbel ok dzięki. Zaczekam na StanislavL, a następnie :-) –

+0

Zobacz także "HTMLDocumentEditor" Charlesa Bella, cytowany [tutaj] (http://stackoverflow.com/a/5899816/230513). – trashgod

Odpowiedz

5

Oto kod na działanie, które można ustawić kolor tła:

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); 
    } 
} 

miałem dużo problemu z ustawianiem koloru tła. Ale w końcu udało mi się go złamać. " Przepraszam, zapomniałem opublikować podprogram. Proszę bardzo:

/** 
* 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

Nie można znaleźć odpowiedniego pakietu dla klasy 'Util' i nie wydaje się on być polem. Czy możesz rzucić trochę światła na to, o czym mówi "Util" w tym wierszu: 'Util.getHTMLColor (color);' –

+0

@NickRippe Nie miałem jeszcze czasu, aby zweryfikować kod (zrobi to jak najszybciej), ale zgaduję że robi coś takiego jak thie: '" # "+ String.format ("% 1 $ 02x% 2 $ 02x% 3 $ 02x ", color.getRed(), color.getGreen(), color.getBlue())' –

+0

Właśnie to zweryfikowałem i działa jak czar. +1 i zaakceptowana odpowiedź. –