Java – jtabbedpane: displays the task progress in the tab
I have a simple swing Java application that performs a search and the results are displayed in a new tab When the search is running, I want to display progress icons or animations in the title of the tab I tried to add a GIF icon, but it has no animation Is there any reason not to do so?
Solution
Swing tutorial about progress bars is a good starting point It shows you how to use swing worker to perform persistent operations on worker threads, and update the UI at specific intervals to show users the progress of persistent operations For more information about swingworker and concurrency in swing, there is another tutorial available
As usual, the site is full of examples For example, a previous answer of mine uses the swingworker class to display progress to users
edit
Because I missed the title of your question label You can create progress icons and set them on the tab You can then use swing worker to update the icon
An example of such an icon is basically an image that rotates every time some progress is made Tabbed pane tutorial shows you how to add icons to tabs (even using custom components)
EDIT2
Because it looks like my Mac and jdk1 7. The combination makes it easier to display GIF animation on other systems. I created a small sscce, which is very similar to Andrew, but with a rotating icon that doesn't look like it. I quoted 'crazy chimpanzee' The rotation icon code is from this site (I used the compact version and added a timer) The only thing I'm not happy about is that I need to pass the tabbed pane to the rotation icon to trigger it A possible solution is to pull the timer out of the rotatingicon class, but it is just an sscce Pictures are not included, but they can be found on Google
import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTabbedPane; import javax.swing.Timer; import java.awt.Component; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.AffineTransform; public class ProgressTabbedPane { public static void main( String[] args ) { EventQueue.invokelater( new Runnable() { @Override public void run() { JFrame frame = new JFrame( "RotatingIcon" ); JTabbedPane tabbedPane = new JTabbedPane( ); tabbedPane.addTab( "Searching",new RotatingIcon( new ImageIcon( "resources/images/progress-indeterminate.png" ),tabbedPane ),new JLabel( new ImageIcon( "resources/images/rotatingIcon.gif" ) ) ); frame.getContentPane().add( tabbedPane ); frame.setDefaultCloSEOperation( JFrame.EXIT_ON_CLOSE ); frame.pack(); frame.setVisible( true ); } } ); } private static class RotatingIcon implements Icon{ private final Icon delegateIcon; private double angleIndegrees = 90; private final Timer rotatingTimer; private RotatingIcon( Icon icon,final JComponent component ) { delegateIcon = icon; rotatingTimer = new Timer( 100,new ActionListener() { @Override public void actionPerformed( ActionEvent e ) { angleIndegrees = angleIndegrees + 10; if ( angleIndegrees == 360 ){ angleIndegrees = 0; } component.repaint(); } } ); rotatingTimer.setRepeats( false ); rotatingTimer.start(); } @Override public void paintIcon( Component c,Graphics g,int x,int y ) { rotatingTimer.stop(); Graphics2D g2 = (Graphics2D )g.create(); int cWidth = delegateIcon.getIconWidth() / 2; int cHeight = delegateIcon.getIconHeight() / 2; Rectangle r = new Rectangle(x,y,delegateIcon.getIconWidth(),delegateIcon.getIconHeight()); g2.setClip(r); AffineTransform original = g2.getTransform(); AffineTransform at = new AffineTransform(); at.concatenate(original); at.rotate(Math.toradians( angleIndegrees ),x + cWidth,y + cHeight); g2.setTransform(at); delegateIcon.paintIcon(c,g2,x,y); g2.setTransform(original); rotatingTimer.start(); } @Override public int getIconWidth() { return delegateIcon.getIconWidth(); } @Override public int getIconHeight() { return delegateIcon.getIconHeight(); } } }
Screenshot for reference Unfortunately, the icon will not rotate in the screenshot