Java Swing UIManager fixHtmlDisplay(JComponent component)

Here you can find the source of fixHtmlDisplay(JComponent component)

Description

This method fixes the display of HTML enabled components (like JEditorPanes and JTextPanes).

License

Apache License

Declaration

public static void fixHtmlDisplay(JComponent component) 

Method Source Code

//package com.java2s;
/*//from w  ww  . j  a  va2s.co m
 * Copyright 2005 Patrick Gotthardt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.swing.*;

import javax.swing.text.*;
import javax.swing.text.html.*;
import java.awt.*;

public class Main {
    /**
     * This method fixes the display of HTML enabled components (like JEditorPanes
     * and JTextPanes).
     */
    public static void fixHtmlDisplay(JComponent component) {
        Font defaultFont = UIManager.getFont("Button.font");

        String stylesheet = "body { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0; font-family: "
                + defaultFont.getName()
                + "; font-size: "
                + defaultFont.getSize()
                + "pt;   }"
                + "a, p, li { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0; font-family: "
                + defaultFont.getName()
                + "; font-size: "
                + defaultFont.getSize() + "pt;   }";

        try {
            HTMLDocument doc = null;
            if (component instanceof JEditorPane) {
                if (((JEditorPane) component).getDocument() instanceof HTMLDocument) {
                    doc = (HTMLDocument) ((JEditorPane) component)
                            .getDocument();
                }
            } else {
                View v = (View) component
                        .getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
                if (v != null && v.getDocument() instanceof HTMLDocument) {
                    doc = (HTMLDocument) v.getDocument();
                }
            }
            if (doc != null) {
                doc.getStyleSheet().loadRules(
                        new java.io.StringReader(stylesheet), null);
            } // end of if (doc != null)
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. destylize(T comp)
  2. disabledIcon(JComponent parent, Icon icon)
  3. disablePaintSliderValue()
  4. drawVistaBackground(Graphics g, Component b, String prefix)
  5. fixGtkPopupStyle()
  6. getAnimationSpeed()
  7. getClassLoaderForResources()
  8. getClassLoaderForResources()
  9. getDark()