Using the Java high-speed implementation progress bar

Source: Internet
Author: User

Based on how the progress bar is being asked, here's a simple procedure:

Mostly using JProgressBar (swing built-in Javax.swing.JProgressBar) and SwingWorker (Swing built-in Javax.swing.SwingWorker)

Someone would say, isn't it a thread? Using a thread to share a variable is really possible. Just this way is not too elegant, of course, you can again use the thread encapsulation also line. Just, SwingWorker already helped us to wrap up ~ ~ To use the ready-made things to do or convenient ^_^

First look at the effect:

The following look at the code: very easy, I believe you are greatly sure to understand ~

Package Org.ubird;import Java.awt.borderlayout;import Java.awt.dimension;import java.awt.image;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.util.arraylist;import java.util.List; Import Java.util.concurrent.executionexception;import Javax.imageio.imageio;import Javax.swing.imageicon;import Javax.swing.jbutton;import Javax.swing.jlabel;import Javax.swing.jpanel;import Javax.swing.JProgressBar;import Javax.swing.jscrollpane;import Javax.swing.swingutilities;import Javax.swing.swingworker;public Class Testswingworker extends swingworker<list<image>,string>{private JLabel status;private JPanel Viewer; Private string[] imagesname;private jprogressbar jpb;public static void Main (string[] args) {Swingutilities.invokelater (New Runnable () {public void run () {Testframe frame = new Testframe (); final JPanel panel = new JPanel (); final JLabel label = new JLabel (); final string[] Imagesname = new string[38];for (int i=0; i<imagesname.length; i++) {IMAGESNAme[i] = "RES/1 (" + (i+1) + "). jpg";} JScrollPane sp = new JScrollPane (panel), Sp.setsize (New Dimension (700,500)); Frame.add (Sp,borderlayout.center); JPanel stp = new JPanel (), final jprogressbar JPB = new JProgressBar (); Jpb.setminimum (1); Jpb.setmaximum ( Imagesname.length); Stp.add (JPB); stp.add (label); Frame.add (Stp,borderlayout.south); JButton button = new JButton ("Load image"), Button.addactionlistener (new ActionListener () {public void actionperformed ( ActionEvent e) {testswingworker SW = new Testswingworker (label, panel, Imagesname, JPB); Sw.execute ();}); Frame.add (Button,borderlayout.north); frame.setvisible (True);}}); Public Testswingworker (JLabel status, JPanel Viewer, string[] imagesname, JProgressBar jpb) {this.status = status; This.viewer = Viewer;this.imagesname = IMAGESNAME;THIS.JPB = JPB;} @Overrideprotected list<image> Doinbackground () throws Exception {list<image> Image = new arraylist< Image> (); for (int i=0; i<imagesname.length; i++) {Image.add (Imageio.read (getclass). GetCLassloader (). GetResource (Imagesname[i)));p ublish ("Already Loaded" + (i+1) + "/" +imagesname.length+ ":" +imagesname[i]);} return image; @Overrideprotected void process (list<string> chunks) {Status.settext (Chunks.get (Chunks.size ()-1)); int x = Integer.parseint (Chunks.get (Chunks.size ()-1). substring (Chunks.get (chunks.size ()-1). IndexOf ("(") +1,chunks.get ( Chunks.size ()-1). IndexOf (")"). Trim ()); Jpb.setvalue (x); for (String str:chunks) {System.out.println (str)}} @Overrideprotected void Done () {try {for (Image Image:get ()) {JLabel label = new JLabel (new ImageIcon (image)); Label.setsiz E (label.setpreferredsize); (New Dimension (160,120)); Viewer.add (label);}} catch (Interruptedexception e) {e.printstacktrace ();} catch (Executionexception e) {e.printstacktrace ()}}}

Package Org.ubird;import Javax.swing.jframe;public class Testframe extends JFrame {public testframe () {Settitle (" Test Frame "); SetSize (800,600); Setdefaultcloseoperation (Jframe.exit_on_close) ;}}

The executable source code, resource picture of the program: http://download.csdn.net/source/3245370

Note: after decompression, double-click inside: Compileandrun.bat can be directly executed under win

Using the Java high-speed implementation progress bar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.