C'è un modo se si crea il proprio look and feel.
Non sono sicuro di quanto funzioni questo risultato, ma funziona. Supponiamo che estendiate le "finestre classiche" L & F.È necessario alle classi 2 di leas 2 Uno è il look & Sentirsi, chiamiamolo WindowsClassicLookAndFeelExt. Devi solo eseguire l'override del metodo 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);
}
}
È inoltre necessario un classe WindowsLabelExtUI per gestire tutte le JLabels e impostare la proprietà:
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;
}
}
E infine una classe di test quando si imposta il tema come 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);
}
}
E voi vedrà qualcosa come
fonte
2010-08-27 19:24:25
Cosa impedisce la soluzione 'putClientProperty'? –
putClientProperty funziona - ma ci sono molti JLabels di cui uno potrebbe non essere a conoscenza. Ad esempio: creare un elenco come nuovo JList (new String [] {"entry"}). La 'voce' sarà resa da una JLabel (vedi javax.swing.DefaultListCellRenderer). Non conosco nessuna soluzione generica per impedire a JLabels come questo di interpretare l'HTML. Se si sostituisce 'entry' con l'HTML come menzionato sopra, la GUI si bloccherà. – tigger