简体   繁体   中英

Dynamically changing Icons of tabs in Java JTabbedPane

UPDATED*** How do I dynamically change an icon of a tab in JTabbedPane? This is the code. I need the icon to change from initial-processing-final This code is just going initial-final

Code:

package tabs;

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JButton;


public class ChangeIconTest {

    private JFrame frame;
    private JTabbedPane tabbedPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ChangeIconTest window = new ChangeIconTest();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public ChangeIconTest() {
        initialize();
        addtabs();

    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        tabbedPane.setBounds(0, 0, 434, 262);
        frame.getContentPane().add(tabbedPane);


    }

    private void addtabs(){
        tabbedPane.insertTab("tab name", displayInitial(), addButton(), null, 0);
        tabbedPane.setSelectedIndex(0);

    }

    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = ChangeIconTest.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    public Icon displayInitial(){
        ImageIcon initialIcon = createImageIcon("first.gif");
        return initialIcon;
    }
    public Icon displayMid(){


        ImageIcon midIcon = createImageIcon("second.gif");
        return midIcon;

    }
    public Icon displayFinal(){

        ImageIcon finalIcon2 = createImageIcon("third.jpg");
        return finalIcon2;
    }


    public Component addButton(){
        JButton jb = new JButton("The Big Button to change the Icon");
        jb.addActionListener(new ChangeTabListener());
        return jb;
    }

    class ChangeTabListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            tabbedPane.setIconAt(0, displayMid());
            tabbedPane.revalidate();
            tabbedPane.repaint();
            tabbedPane.setIconAt(0, displayFinal());
            tabbedPane.revalidate();
            tabbedPane.repaint();
        }



    }
}

So what should I add or modify? Thanks!

Why are you using Thread.sleep()? You are probably causing the Event Dispatch Thread to sleep which means the GUI can't repaint itself.

If you want to dynamically change the Icon for a certain period of time then use a Swing Timer to schedule the changing of the icon.

If you need more help then update your question and post a proper SSCCE that demonstrates the problem.

Try add this

tabs.revalidate();
tabs.repaint();

Or

tabs.getParent().revalidate();
tabs.getParent().repaint();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM