JTabbedPane: show task progress in a tab

后端 未结 3 1801
小蘑菇
小蘑菇 2020-11-29 10:35

I have a simple Swing Java application that performs searches, and the results are shown in a new tab. While the search is running, I want to show a progress icon or animati

3条回答
  •  粉色の甜心
    2020-11-29 11:14

    @Andrew Thompson

    It would be great if the J2SE supported animated GIFs 'out of the box' 
    in more situations. I tried that animated GIF (nice image, BTW) as a 
    tab icon, and no, it remains static. 
    

    I don't want to read whole ...., but put together code by yours and @trashgod's majesty

    1) use Htlm (I'm not good in plain Html)

    2) use GlassPane with JLabel#(setOpaque(true))

    3) use JLayer (JXLayer is better, becasue Sn'Oracle remove important methods == my view)

    4) you have to force ..... for Swing JComponents by @aterai

    5) Rob's Animated Icon a few times metioned support by Rob for JTabbedPane

    code

    import java.awt.*;
    import java.awt.event.*;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Random;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    //https://stackoverflow.com/questions/3483485/java-jprogressbar-or-equivalent-in-a-jtabbedpane-tab-title/3484251#3484251
    public class JTabbedTest {
    
        private JFrame f = new JFrame();
        private JTabbedPane jtp = new JTabbedPane();
        private URL url = null;
    
        public JTabbedTest() {
            try {
                url = new URL("http://pscode.org/media/starzoom-thumb.gif");
            } catch (MalformedURLException ex) {
                Logger.getLogger(JTabbedTest.class.getName()).log(Level.SEVERE, null, ex);
            }
            ImageIcon ii = new ImageIcon(url);
    
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jtp.setPreferredSize(new Dimension(400, 200));
            createTab("Reds", Color.RED);
            createTab("Greens", Color.GREEN);
            createTab("Blues", Color.BLUE);
            f.add(jtp, BorderLayout.CENTER);
    
            jtp.setTitleAt(2, "");
    
            // change foreground Color for disabled tab        
            /*jtp.setTitleAt(2, ""
                    + jtp.getTitleAt(2) + "");*/
    
            Rectangle tabBounds = jtp.getBoundsAt(0);
            Container glassPane = (Container) f.getRootPane().getGlassPane();
            glassPane.setVisible(true);
            glassPane.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            gbc.fill = GridBagConstraints.NONE;
            gbc.insets = new Insets(tabBounds.y + 23, 0, 0, 5);
            gbc.anchor = GridBagConstraints.NORTHEAST;
            JButton button = new JButton("My Button Position", ii);
            button.setPreferredSize(new Dimension(button.getPreferredSize().width, (int) tabBounds.getHeight() - 2));
            glassPane.add(button, gbc);
            f.pack();
            f.setVisible(true);
        }
    
        private void createTab(String name, Color color) {
            ProgressIcon icon = new ProgressIcon(color);
            jtp.addTab(name, icon, new ColorPanel(jtp, icon));
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JTabbedTest jTabbedTest = new JTabbedTest();
                }
            });
        }
    
        private static class ColorPanel extends JPanel implements ActionListener {
    
            private static final Random rnd = new Random();
            private static final long serialVersionUID = 1L;
            private final Timer timer = new Timer(1000, this);
            private final JLabel label = new JLabel("Stackoverflow!");
            private final JTabbedPane parent;
            private final ProgressIcon icon;
            private final int mask;
            private int count;
    
            public ColorPanel(JTabbedPane parent, ProgressIcon icon) {
                super(true);
                this.parent = parent;
                this.icon = icon;
                this.mask = icon.color.getRGB();
                this.setBackground(icon.color);
                label.setForeground(icon.color);
                this.add(label);
                timer.start();
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                this.setBackground(new Color(rnd.nextInt() & mask));
                this.icon.update(count += rnd.nextInt(8));
                this.parent.repaint();
            }
        }
    
        private static class ProgressIcon implements Icon {
    
            private static final int H = 16;
            private static final int W = 3 * H;
            private Color color;
            private int w;
    
            public ProgressIcon(Color color) {
                this.color = color;
            }
    
            public void update(int i) {
                w = i % W;
            }
    
            @Override
            public void paintIcon(Component c, Graphics g, int x, int y) {
                g.setColor(color);
                g.fillRect(x, y, w, H);
            }
    
            @Override
            public int getIconWidth() {
                return W;
            }
    
            @Override
            public int getIconHeight() {
                return H;
            }
        }
    }
    

提交回复
热议问题