Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
808 views
in Technique[技术] by (71.8m points)

swing - Java: JProgressBar (or equivalent) in a JTabbedPane tab title

If I really wanted to do something like that, can I put a JProgressBar (or it's equivalent) in a JTabbedPane tab? (I mean, not in the tab itself,

How would I do something like that?

EDIT I really do want to put the progressbar in the title of the tab, not the tab itself.

Here's some ascii art:

----------------------------------------------------
|  Tab 1  || Tab   2||Tab-with-progress-bar||Tab  4|
-----------         --------------------------------
'                                                  '
'                                                  '
'                                                  '
'                                                  '
'                                                  '
'                                                  '
'                                                  '
'                                                  '
----------------------------------------------------

So it's really "tab 2" that is currently visible, but I want the progress bar (or equivalent) to be visible in the third tab's title.

EDIT 2

This has to work on Java 1.5: this has to work on the countless MacOS 10.4 and MacOS 10.5 Apple computers that will never be equipped with Java 6 (some do, some don't: and quite a lot never will, it is not my call)

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For earlier versions, you might try addTab() with a suitable implementation of Icon used to indicate progress.

JTabbedTest

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class JTabbedTest {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            private final JTabbedPane jtp = new JTabbedPane();

            public void run() {
                JFrame f = new JFrame();
                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);
                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));
            }
        });
    }

    private static class ColorPanel extends JPanel implements ActionListener {

        private static final Random rnd = new Random();
        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();
        }

        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;
        }

        public void paintIcon(Component c, Graphics g, int x, int y) {
            g.setColor(color);
            g.fillRect(x, y, w, H);
        }

        public int getIconWidth() {
            return W;
        }

        public int getIconHeight() {
            return H;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...