2012-11-08 16 views
5

मैं JTextPane के साथ एक छोटा HTML-wysiwyg बनाने की कोशिश कर रहा हूं लेकिन मुझे काम करने के लिए BackgroundAction नहीं मिल रहा है। मैं पर JTextPane पर उपयोग कर रहा हूं लेकिन यह समस्याग्रस्त लगता है। दृश्य ठीक है लेकिन Document नहीं है।JTextPane टेक्स्ट पृष्ठभूमि रंग काम नहीं करता

समस्या दिखाते हुए एक छोटा डेमो कोड यहां दिया गया है। वहाँ 2 JTextPane हैं:

  1. मैं पहली बार एक
  2. अपने पाठ की पृष्ठभूमि का रंग सेट मैं पहली बार JTextPane के पाठ को पुनः प्राप्त और दूसरा एक

      पर सेट   -> वे एक ही चीज़ नहीं दिखाते हैं हालांकि उनके पास एक ही पाठ है।

क्या मौजूदा चयनित पाठ पर पृष्ठभूमि रंग सेट करने का कोई तरीका है और JTextPane अद्यतन एचटीएमएल-पाठ की रिपोर्ट करें?

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 के आसपास रहे हैं):

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

मैं बहुत था: output result

+0

@Stanislav लिए इंतजार करना पड़, वह ओवरराइड देखभालकर्ता, चयन और HightLighter के लिए समाधान है, मुझे लगता है कि इस UImanager और उसके XxxResources, – mKorbel

+0

@mKorbel ठीक धन्यवाद के बारे में है। मैं तब स्टैनिस्लाव के लिए इंतजार करूंगा :-) –

+0

चार्ल्स बेल के 'HTMLDocumentEditor' को भी देखें, उद्धृत [यहां] (http://stackoverflow.com/a/5899816/230513)। – trashgod

उत्तर

5

यहाँ एक लड़ाई है कि पृष्ठभूमि रंग सेट कर सकते हैं के लिए कोड है पृष्ठभूमि रंग सेटिंग परेशानी का। लेकिन आखिरकार, मैंने इसे क्रैक करने में कामयाब रहा है। क्षमा करें मैं subroutine पोस्ट करना भूल गया। ये रहा:

/** 
* 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 (रंग); ' –

+0

@NickRippe मेरे पास कोड सत्यापित करने के लिए अभी तक समय नहीं था (Asap करेगा), लेकिन मैं अनुमान लगा रहा हूं कि यह थाई की तरह कुछ करता है: '" # "+ स्ट्रिंग.फॉर्मैट ("% 1 $ 02x% 2 $ 02x% 3 $ 02x ", color.getRed(), color.getGreen(), color.getBlue())' –

+0

बस इसे सत्यापित किया गया और यह एक आकर्षण की तरह काम करता है। +1 और स्वीकृत उत्तर। –

संबंधित मुद्दे