如何禁用JLabel的自动HTML支持? [英] How to disable the automatic HTML support of JLabel?

查看:216
本文介绍了如何禁用JLabel的自动HTML支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swing JLabel自动将任何文本解释为HTML内容,如果它以< html>开头。如果这个HTML的内容是一个带有无效URL的图片,这将导致整个GUI挂起,因为应该加载这个图片的ImageFetche会被一个NPE退出。



为了重现这个问题,只需创建一个JLabel,如下所示:

  new JLabel(< html>< img src ='http:\\\\\\\\\\\\\')

我知道有一个客户端属性来防止JLabel解释HTML。但JLabel是许多Swing组件(如JTree,JTable等)的默认渲染器实现,这使得几乎任何允许用户输入的Swing应用程序都成为问题。因此,我没有实现大量的自定义渲染器,而是寻找一个全局解决方案来禁用HTML解释。解析方案

有一个方式,如果你创建自己的外观和感觉。

我不确定这是如何执行的,但它的工作原理。让我们假设你将扩展经典WindowsL& F.你需要leas 2 classes
一个是Look& Feel本身,我们称它为WindowsClassicLookAndFeelExt。您只需要重写initClassDefaults方法。

  package testSwing; 

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






$ b你还需要一个WindowsLabelExtUI类来管理所有的JLabel和设置属性:

  package testSwing; 
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import com.sun.java.swing.plaf.windows.WindowsLabelUI;

公共类WindowsLabelExtUI扩展WindowsLabelUI {
静态WindowsLabelExtUI singleton = new WindowsLabelExtUI();

public static ComponentUI createUI(JComponent c){
c.putClientProperty(html.disable,Boolean.TRUE);
返回单身人士;


最后,当您将主题设置为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;

$ b $ 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 = {< html>< img src ='http:\\\\\\\\\\\\\' ;< H1> Hello< / h1>< / html>,orange,dark blue};

JList list = new JList(selections);

list.setSelectedIndex(1);
System.out.println(list.getSelectedValue());

JLabel jLabel =新的JLabel(< html>< h2>标准标签< / h2>< / html>);
frame.add(new JScrollPane(list));
frame.add(jLabel);
frame.pack();

frame.setVisible(true);


你会看到类似于




A Swing JLabel automatically interprets any text as HTML content, if it starts with <html>. If the content of this HTML is an image with invalid URL this will cause the whole GUI to hang since the ImageFetche which should load this image will quit by an NPE.

To reproduce this problem simply create a JLabel as follows

new JLabel("<html><img src='http:\\\\invalid\\url'>")

I know there is a client property to prevent the JLabel from interpreting HTML. But JLabel is the default renderer implementation for many Swing components(like JTree, JTable and so on) which makes this a problem for nearly any Swing application which allows user input. So instead of implementing tons of custom renderer I'm searching for a global solution to disable the HTML interpretation.

解决方案

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 = {"<html><img src='http:\\\\invalid\\url'>", "<html><H1>Hello</h1></html>", "orange", "dark blue"};

        JList list = new JList(selections);

        list.setSelectedIndex(1);
        System.out.println(list.getSelectedValue());

        JLabel jLabel = new JLabel("<html><h2>standard Label</h2></html>");
        frame.add(new JScrollPane(list));
        frame.add(jLabel);
        frame.pack();

        frame.setVisible(true);
    }
}

And you will see something like

这篇关于如何禁用JLabel的自动HTML支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆