2012-11-08 28 views
5

Tôi đang cố gắng tạo một HTML-wysiwyg nhỏ với JTextPane nhưng tôi không thể nhận được BackgroundAction để hoạt động. Tôi đang sử dụng setCharacterAttributes trên StyledDocument của JTextPane nhưng có vẻ như có vấn đề. Chế độ xem là ok nhưng Document thì không.Màu nền văn bản JTextPane không hoạt động

Đây là một mã trình diễn nhỏ hiển thị sự cố. Có 2 JTextPane:

  1. tôi đặt màu nền của văn bản của tôi trong lần đầu tiên một
  2. Tôi lấy nội dung của JTextPane đầu tiên và đặt nó vào thứ hai

        -> Chúng không hiển thị cùng một thứ mặc dù chúng có cùng văn bản.

Có cách nào để đặt màu nền cho văn bản được chọn hiện tại và có báo cáo JTextPane một văn bản HTML được cập nhật không?

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

} 

Kết quả đầu ra (các viền đen là xung quanh mỗi JTextPane): output result

+0

phải chờ @Stanislav, ông có giải pháp cho người chăm sóc override, Selections và HightLighter, tôi nghĩ rằng đây là khoảng UImanager và XxxResources của nó, – mKorbel

+0

@mKorbel ok cảm ơn. Tôi sẽ đợi StanislavL sau đó :-) –

+0

Xem thêm 'HTMLDocumentEditor' của Charles Bell, trích dẫn [ở đây] (http://stackoverflow.com/a/5899816/230513). – trashgod

Trả lời

5

Đây là mã cho một hành động có thể thiết lập màu nền:

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

Tôi có rất nhiều gặp khó khăn khi thiết lập màu nền. Nhưng cuối cùng, tôi đã cố gắng giải quyết nó.` Xin lỗi tôi đã quên đăng chương trình con. Ở đây bạn đi:

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

Không thể tìm thấy một gói thích hợp cho lớp 'Util' và nó không có vẻ là một trường. Bạn có thể làm sáng tỏ những gì 'Util' đang đề cập đến trong dòng này: 'Util.getHTMLColor (color);' –

+0

@NickRippe Tôi chưa có thời gian để xác minh mã (sẽ làm càng sớm càng tốt), nhưng tôi đoán rằng nó làm một cái gì đó như thie: '" # "+ String.format ("% 1 $ 02x% 2 $ 02x% 3 $ 02x ", color.getRed(), color.getGreen(), color.getBlue())' –

+0

Chỉ cần xác minh nó và nó hoạt động như một sự quyến rũ. Câu trả lời +1 và được chấp nhận. –