How to disable the automatic HTML support of JLabel?

前端 未结 3 593
北海茫月
北海茫月 2021-01-02 13:09

A Swing JLabel automatically interprets any text as HTML content, if it starts with . If the content of this HTML is an image with invalid URL this will cause th

3条回答
  •  清酒与你
    2021-01-02 13:31

    There is a way if you create your own look and feel.
    I'm not sure how well this performs is this, but it works. Lets assume you will extend the "Classic Windows" L&F.You need at leas 2 classes One is the Look&Feel itself, lets call it WindowsClassicLookAndFeelExt. You only need to override method initClassDefaults.

    package testSwing;
    
    import javax.swing.UIDefaults;
    import com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel;
    
    public class WindowsClassicLookAndFeelExt extends WindowsClassicLookAndFeel    {
        @Override protected void initClassDefaults(UIDefaults table){
            super.initClassDefaults(table);
            Object[] uiDefaults = { "LabelUI", WindowsLabelExtUI.class.getCanonicalName()};
            table.putDefaults(uiDefaults);
        }
    }
    

    You also need a WindowsLabelExtUI class to manage all JLabels and set the property:

    package testSwing;
    import javax.swing.JComponent;
    import javax.swing.plaf.ComponentUI;
    import com.sun.java.swing.plaf.windows.WindowsLabelUI;
    
    public class WindowsLabelExtUI extends WindowsLabelUI{
        static WindowsLabelExtUI singleton = new WindowsLabelExtUI();
    
        public static ComponentUI createUI(JComponent c){
            c.putClientProperty("html.disable", Boolean.TRUE);    
            return singleton;
        }
    }
    

    And finally a test class when you set the theme as WindowsClassicLookAndFeelExt

    package testSwing;
    
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.UIManager;
    
    
    public class Main{
        public static void main(String[] args){
            try{                UIManager.setLookAndFeel(WindowsClassicLookAndFeelExt.class.getCanonicalName());
            }catch (Exception e){
                e.printStackTrace();
            }
    
            JFrame frame = new JFrame("JList Test");
            frame.setLayout(new FlowLayout());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            String[] selections = {"", "

    Hello

    ", "orange", "dark blue"}; JList list = new JList(selections); list.setSelectedIndex(1); System.out.println(list.getSelectedValue()); JLabel jLabel = new JLabel("

    standard Label

    "); frame.add(new JScrollPane(list)); frame.add(jLabel); frame.pack(); frame.setVisible(true); } }

    And you will see something like

    alt text

提交回复
热议问题