JTabbedPane:在选项卡中显示任务进度 [英] JTabbedPane: show task progress in a tab

查看:122
本文介绍了JTabbedPane:在选项卡中显示任务进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个执行搜索的简单Swing Java应用程序,结果显示在新选项卡中。在搜索运行时,我想在选项卡的标题中显示进度图标或动画。我尝试添加一个gif图标,但它没有动画。有没有理由说这不起作用?

解决方案

向您展示如何向您的图标添加图标标签(甚至使用自定义组件)



Edit2



因为它似乎是我的Mac与JDK1.7相结合,可以更容易地在其他系统上显示GIF动画,我创建了一个小型SSCCE,与Andrew的相似,但是旋转图标看起来不像是由它创建的,我引用,疯狂的黑猩猩。旋转图标代码来自此网站(我使用了剥离图标下载版本并添加计时器)。我唯一不高兴的是我需要将选项卡式窗格传递给旋转图标才能触发。可能的解决办法是将计时器拉到 RotatingIcon 类之外,但是,它只是一个SSCCE。图片不包括在内,但可以在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;

公共类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);
}
});
}

私有静态类RotatingIcon实现Icon {
private final Icon delegateIcon;
private double angleInDegrees = 90;
private final Timer rotatingTimer;
private RotatingIcon(图标图标,最终JComponent组件){
delegateIcon = icon;
rotateTimer = 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();
}
}
}

截图供参考。遗憾的是,图标不会在屏幕截图中旋转。


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 animation in the title of the tab. I tried adding a gif icon, but it doesn't animate. Is there a reason why this isn't working?

解决方案

The Swing tutorial about progress bars (and showing progress in general) is a very good place to start. It shows you how to perform long-lasting operations on a worker thread by using a SwingWorker, and updating your UI at certain intervals to show progress of the long-lasting operation to the user. There is another tutorial available for more information on the SwingWorker and concurrency in Swing

And as always, this site is filled with examples. For example a previous answer of mine uses the SwingWorker class to show progress to a user

Edit

As I missed the title of tab part of your question. You could create a 'progress icon' and set that on the tab. The SwingWorker can then be used to update the icon.

An example of such an icon is , which is basically an image you rotate each time some progress is made. The tabbed pane tutorial shows you how to add icons to your tabs (or even use custom components)

Edit2

As it seems my Mac in combination with JDK1.7 makes it much easier to show an animated gif then on other systems, I created a small SSCCE as well, quite similar to that of Andrew but with a rotating icon which does not look like it has been created by, and I quote, 'demented Chimpanzee'. The rotating icon code comes from this site (I used a stripped down version and added the timer). Only thing I am not too happy about is the fact I need to pass my tabbed pane to the rotating icon to trigger. Possible solution is to pull the timer outside the RotatingIcon class, but hey, it's only an SSCCE . Images are not included but were found with 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();
    }
  } 
}

A screenshot for reference. A shame the icons do not rotate in the screenshot.

这篇关于JTabbedPane:在选项卡中显示任务进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆