Originariamente inviato da zipangulu
a cosa mi servirebbe questa cosa?
Per avere un elenco dei L&F da mostrare es. come voci di un menù o di un combo box o una lista .... insomma permettere all'utente di scegliere lui un L&F in qualche modo.
Originariamente inviato da zipangulu
un esempio di codice?
Ecco il mio solito esempio completo.
codice:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LAFTestFrame extends JFrame implements ActionListener {
public LAFTestFrame() {
super("Look and feels test");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
UIManager.LookAndFeelInfo[] lafiArray = UIManager.getInstalledLookAndFeels();
for (int i = 0; i < lafiArray.length; i++) {
LAFSelectButton lafButton = new LAFSelectButton(lafiArray[i]);
contentPane.add(lafButton);
lafButton.addActionListener(this);
}
pack();
}
public void actionPerformed(ActionEvent e) {
LAFSelectButton lafButton = (LAFSelectButton) e.getSource();
final String className = lafButton.getLAFClassName();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(className);
SwingUtilities.updateComponentTreeUI(LAFTestFrame.this);
pack();
} catch (Exception e) {
JOptionPane.showMessageDialog(LAFTestFrame.this,
"Error setting Look&Feel", "ERROR",
JOptionPane.ERROR_MESSAGE);
}
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new LAFTestFrame().setVisible(true);
}
});
}
}
class LAFSelectButton extends JButton {
private UIManager.LookAndFeelInfo lafi;
public LAFSelectButton(UIManager.LookAndFeelInfo lafi) {
super(lafi.getName());
this.lafi = lafi;
}
public String getLAFClassName() {
return lafi.getClassName();
}
}