Look and feel non si sta aggiornando in Swing JTabbedPane

Ho creato un’applicazione in Java Swing. Offro la possibilità di cambiare l’aspetto e l’aspetto dell’applicazione da un menu, ma dopo aver aggiunto una nuova scheda in JTabbedPane , non viene aggiornata con il nuovo aspetto.

Ho già usato questo codice:

 Window windows[] = Frame.getWindows(); for(Window window : windows) { SwingUtilities.updateComponentTreeUI(window); } 

Facendo leva sull’esempio di Andrew e su questa vecchia cosa , sembra funzionare per me.

inserisci la descrizione dell'immagine qui

 import java.awt.BorderLayout; import java.awt.Component; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTabbedPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JToolBar; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.UIManager.LookAndFeelInfo; /** * @see https://stackoverflow.com/a/11949899/230513 * @see https://stackoverflow.com/a/5773956/230513 */ public class JTabbedText { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { private final JTabbedPane jtp = new JTabbedPane(); @Override public void run() { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jtp.addTab("Model", createPanel()); jtp.addTab("View", createPanel()); jtp.addTab("Control", createPanel()); f.add(createToolBar(f), BorderLayout.NORTH); f.add(jtp, BorderLayout.CENTER); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } }); } private static JToolBar createToolBar(final Component parent) { final UIManager.LookAndFeelInfo[] available = UIManager.getInstalledLookAndFeels(); List names = new ArrayList(); for (LookAndFeelInfo info : available) { names.add(info.getName()); } final JComboBox combo = new JComboBox(names.toArray()); String current = UIManager.getLookAndFeel().getName(); combo.setSelectedItem(current); combo.addActionListener(new ActionListener() { @Override public void actionPerformsd(ActionEvent ae) { int index = combo.getSelectedIndex(); try { UIManager.setLookAndFeel( available[index].getClassName()); SwingUtilities.updateComponentTreeUI(parent); } catch (Exception e) { e.printStackTrace(System.err); } } }); JToolBar bar = new JToolBar("L&F"); bar.add(combo); return bar; } private static Box createPanel() { Box panel = new Box(BoxLayout.X_AXIS); JLabel label = new JLabel("Code: ", JLabel.LEFT); label.setAlignmentY(JLabel.TOP_ALIGNMENT); JTextArea text = new JTextArea(4, 16); text.setAlignmentY(JTextField.TOP_ALIGNMENT); text.append("#" + panel.hashCode()); text.append("\n#" + label.hashCode()); text.append("\n#" + label.hashCode()); panel.add(label); panel.add(text); return panel; } }