Look and feel is not updating in Swing JTabbedPane
Asked Answered
S

1

4

I have created an application in Java Swing. I offer the option to change the look and feel of the application from a menu, but after adding a new tab in JTabbedPane, it is not getting updated with the new look and feel.

I have already used this code:

Window windows[] = Frame.getWindows();
for(Window window : windows) {
    SwingUtilities.updateComponentTreeUI(window);
}
Sybarite answered 14/8, 2012 at 6:4 Comment(4)
For better help sooner, post an SSCCE.Normally
can you show us some more code, perhaps the one adding your Jtabbedpane?Exteriorize
Did you set the new look and feel? Sounds obvious, but sometimes the obvious things get lost :D UIManager.setLookAndFeel(lnfName);Gielgud
have you gone through thisSapphism
C
7

Leveraging @Andrew's example and this old thing, it seems to work for me.

enter image description here

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://mcmap.net/q/1020196/-look-and-feel-is-not-updating-in-swing-jtabbedpane
* @see https://mcmap.net/q/1021493/-jtabbedpane-jlabel-jtextfield
*/
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<String> names = new ArrayList<String>();
        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 actionPerformed(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;
    }
}
Claudieclaudina answered 14/8, 2012 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.