EditorPane and TextPane: fixed font and text color when using HTML content (#9)

This commit is contained in:
Karl Tauber
2019-10-20 18:02:20 +02:00
parent 5a952c187c
commit 41ecbccc76
5 changed files with 120 additions and 1 deletions

View File

@@ -6,6 +6,8 @@ FlatLaf Change Log
- ComboBox: Use small border if used as table editor.
- ToolBar: Disable focusability of buttons in toolbar.
- OptionPane: Fixed rendering of longer HTML text. (issue #12)
- EditorPane and TextPane: Fixed font and text color when using HTML content.
(issue #9)
- SwingX: Support `JXBusyLabel`, `JXDatePicker`, `JXHeader`, `JXHyperlink`,
`JXMonthView`, `JXTaskPaneContainer` and `JXTaskPane`. (issue #8)

View File

@@ -19,6 +19,7 @@ package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicEditorPaneUI;
@@ -37,6 +38,8 @@ public class FlatEditorPaneUI
{
protected int minimumWidth;
private Object oldHonorDisplayProperties;
public static ComponentUI createUI( JComponent c ) {
return new FlatEditorPaneUI();
}
@@ -46,6 +49,17 @@ public class FlatEditorPaneUI
super.installDefaults();
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
// use component font and foreground for HTML text
oldHonorDisplayProperties = getComponent().getClientProperty( JEditorPane.HONOR_DISPLAY_PROPERTIES );
getComponent().putClientProperty( JEditorPane.HONOR_DISPLAY_PROPERTIES, true );
}
@Override
protected void uninstallDefaults() {
super.uninstallDefaults();
getComponent().putClientProperty( JEditorPane.HONOR_DISPLAY_PROPERTIES, oldHonorDisplayProperties );
}
@Override

View File

@@ -19,6 +19,7 @@ package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTextPaneUI;
@@ -37,6 +38,8 @@ public class FlatTextPaneUI
{
protected int minimumWidth;
private Object oldHonorDisplayProperties;
public static ComponentUI createUI( JComponent c ) {
return new FlatTextPaneUI();
}
@@ -46,6 +49,17 @@ public class FlatTextPaneUI
super.installDefaults();
minimumWidth = UIManager.getInt( "Component.minimumWidth" );
// use component font and foreground for HTML text
oldHonorDisplayProperties = getComponent().getClientProperty( JEditorPane.HONOR_DISPLAY_PROPERTIES );
getComponent().putClientProperty( JEditorPane.HONOR_DISPLAY_PROPERTIES, true );
}
@Override
protected void uninstallDefaults() {
super.uninstallDefaults();
getComponent().putClientProperty( JEditorPane.HONOR_DISPLAY_PROPERTIES, oldHonorDisplayProperties );
}
@Override

View File

@@ -143,7 +143,14 @@ public class FlatComponentsTest
JToggleButton toggleButton7 = new JToggleButton();
JLabel scrollBarLabel = new JLabel();
JScrollBar scrollBar1 = new JScrollBar();
JLabel label4 = new JLabel();
JScrollBar scrollBar4 = new JScrollBar();
JPanel panel3 = new JPanel();
JLabel label3 = new JLabel();
JScrollPane scrollPane15 = new JScrollPane();
JEditorPane editorPane6 = new JEditorPane();
JScrollPane scrollPane16 = new JScrollPane();
JTextPane textPane6 = new JTextPane();
JLabel separatorLabel = new JLabel();
JSeparator separator1 = new JSeparator();
JPanel panel2 = new JPanel();
@@ -719,11 +726,52 @@ public class FlatComponentsTest
scrollBar1.setOrientation(Adjustable.HORIZONTAL);
add(scrollBar1, "cell 1 14,growx");
//---- label4 ----
label4.setText("HTML:");
add(label4, "cell 5 14");
//---- scrollBar4 ----
scrollBar4.setOrientation(Adjustable.HORIZONTAL);
scrollBar4.setEnabled(false);
add(scrollBar4, "cell 1 15,growx");
//======== panel3 ========
{
panel3.setLayout(new MigLayout(
"insets 0,hidemode 3,gap 5 5,ltr",
// columns
"[]",
// rows
"[]" +
"[]" +
"[]"));
//---- label3 ----
label3.setText("<html>JLabel HTML<br>Sample <b>content</b><br> <u>text</u></html>");
panel3.add(label3, "cell 0 0");
//======== scrollPane15 ========
{
//---- editorPane6 ----
editorPane6.setContentType("text/html");
editorPane6.setText("JEditorPane HTML<br>Sample <b>content</b><br> <u>text</u>");
scrollPane15.setViewportView(editorPane6);
}
panel3.add(scrollPane15, "cell 0 1,grow");
//======== scrollPane16 ========
{
//---- textPane6 ----
textPane6.setContentType("text/html");
textPane6.setText("JTextPane HTML<br>Sample <b>content</b><br> <u>text</u>");
scrollPane16.setViewportView(textPane6);
}
panel3.add(scrollPane16, "cell 0 2,grow");
}
add(panel3, "cell 5 15 1 7,aligny top,grow 100 0");
//---- separatorLabel ----
separatorLabel.setText("JSeparator:");
add(separatorLabel, "cell 0 16");

View File

@@ -678,6 +678,12 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 14,growx"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label4"
"text": "HTML:"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 5 14"
} )
add( new FormComponent( "javax.swing.JScrollBar" ) {
name: "scrollBar4"
"orientation": 0
@@ -685,6 +691,41 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 15,growx"
} )
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$columnConstraints": "[]"
"$rowConstraints": "[][][]"
"$layoutConstraints": "insets 0,hidemode 3,gap 5 5,ltr"
} ) {
name: "panel3"
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label3"
"text": "<html>JLabel HTML<br>Sample <b>content</b><br> <u>text</u></html>"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 0"
} )
add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) {
name: "scrollPane15"
add( new FormComponent( "javax.swing.JEditorPane" ) {
name: "editorPane6"
"contentType": "text/html"
"text": "JEditorPane HTML<br>Sample <b>content</b><br> <u>text</u>"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 1,grow"
} )
add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) {
name: "scrollPane16"
add( new FormComponent( "javax.swing.JTextPane" ) {
name: "textPane6"
"contentType": "text/html"
"text": "JTextPane HTML<br>Sample <b>content</b><br> <u>text</u>"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 2,grow"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 5 15 1 7,aligny top,grow 100 0"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "separatorLabel"
"text": "JSeparator:"
@@ -837,7 +878,7 @@ new FormModel {
} )
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 )
"size": new java.awt.Dimension( 790, 715 )
"size": new java.awt.Dimension( 790, 750 )
} )
}
}