`
dmp63dmp
  • 浏览: 17039 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

学习使用SwingWorker

 
阅读更多

学习使用SwingWorker
2011年03月04日
   /* 
  * To change this template, choose Tools | Templates 
  * and open the template in the editor. 
  */ 
  package cn.feelingsoft.photomgr.model; 
  import cn.feelingsoft.photomgr.i.FileSearchListener; 
  import cn.feelingsoft.photomgr.util.FileUtil; 
  import java.util.ArrayList; 
  import java.util.HashSet; 
  import java.util.concurrent.ExecutionException; 
  import java.util.logging.Level; 
  import java.util.logging.Logger; 
  import javax.swing.SwingWorker; 
  /** 
  * 
  * @author Sanwu 
  */ 
  public class SwingWorkerFileSearch extends SwingWorker, String> { 
  private String userDir; 
  private HashSet listeners = new HashSet(); 
  private int absoluteOrRelative; 
  public SwingWorkerFileSearch() { 
  this("", FileUtil.ABSOLUTEPATH); 
  } 
  public SwingWorkerFileSearch(String userDir) { 
  this(userDir, FileUtil.ABSOLUTEPATH); 
  } 
  public SwingWorkerFileSearch(String userDir, int absoluteOrRelative) { 
  this.userDir = userDir; 
  this.absoluteOrRelative = absoluteOrRelative; 
  } 
  @Override 
  protected ArrayList doInBackground() throws Exception { 
  return FileUtil.searchPath(userDir, absoluteOrRelative); 
  } 
  @Override 
  protected void done() { 
  try { 
  ArrayList al = this.get(); 
  for (FileSearchListener l : listeners) { 
  l.processFileSearchOver(al); 
  } 
  } catch (InterruptedException ex) { 
  Logger.getLogger(SwingWorkerFileSearch.class.getNa me()).log(Level.SEVERE, null, ex); 
  } catch (ExecutionException ex) { 
  Logger.getLogger(SwingWorkerFileSearch.class.getNa me()).log(Level.SEVERE, null, ex); 
  } 
  } 
  public void addFileSearchListener(FileSearchListener listener) { 
  if (null != listener) { 
  this.listeners.add(listener); 
  } 
  } 
  public void removeFileSearchListener(FileSearchListener listener) { 
  if (null != listener) { 
  listeners.remove(listener); 
  } 
  } 
  public String getUserDir() { 
  return userDir; 
  } 
  public void setUserDir(String userDir) { 
  this.userDir = userDir; 
  } 
  } 
  /* 
  * To change this template, choose Tools | Templates 
  * and open the template in the editor. 
  */ 
  package cn.feelingsoft.photomgr.util; 
  import java.io.File; 
  import java.util.ArrayList; 
  /** 
  * 
  * @author Sanwu 
  */ 
  public class FileUtil { 
  public final static int ABSOLUTEPATH = 1; 
  public final static int RELATIVEPATH = 2; 
  //    public static ArrayList searchPath(ArrayList filenames, File rootPath) { 
  // 
  //        if (rootPath.exists() && rootPath.isDirectory()) { 
  //            String[] subPathFilenames = rootPath.list(); 
  //            for (String filename : subPathFilenames) { 
  //                File file = new File(rootPath.getPath() + File.separator + filename); 
  //                if (file.isDirectory()) { 
  //                    searchPath(filenames, file); 
  //                } else { 
  //                    filenames.add(file.getAbsolutePath()); 
  //                } 
  //            } 
  //        } else if (rootPath.isFile()) { 
  //            filenames.add(rootPath.getPath() + File.separator + rootPath.getName()); 
  //        } 
  // 
  //        return filenames; 
  //    } 
  public static ArrayList searchPath(File rootPath, int absoluteOrRelative) { 
  ArrayList files = new ArrayList(); 
  searchPath(files, rootPath, rootPath.getPath(), absoluteOrRelative); 
  return files; 
  } 
  public static ArrayList searchPath(String rootPath, int absoluteOrRelative) { 
  ArrayList files = new ArrayList(); 
  searchPath(files, new File(rootPath), rootPath, absoluteOrRelative); 
  return files; 
  } 
  private static void searchPath(ArrayList filenames, File cRootPath, String rootPath, int absoluteOrRelative) { 
  if (cRootPath.exists() && cRootPath.isDirectory()) { 
  String[] subPathFilenames = cRootPath.list(); 
  for (String filename : subPathFilenames) { 
  File file = new File(cRootPath.getPath() + File.separator + filename); 
  if (file.isDirectory()) { 
  searchPath(filenames, file, rootPath, absoluteOrRelative); 
  } else if(absoluteOrRelative == FileUtil.RELATIVEPATH) { 
  filenames.add(StringUtil.getRelativePath(rootPath, file.getAbsolutePath())); 
  } else if (absoluteOrRelative == FileUtil.ABSOLUTEPATH) { 
  filenames.add(file.getAbsolutePath()); 
  } 
  } 
  } else if (cRootPath.isFile()) { 
  if (absoluteOrRelative == FileUtil.ABSOLUTEPATH) { 
  filenames.add(cRootPath.getAbsolutePath()); 
  } else { 
  filenames.add(StringUtil.getRelativePath(rootPath, cRootPath.getAbsolutePath())); 
  } 
  } 
  } 
  } 
  /* 
  * To change this template, choose Tools | Templates 
  * and open the template in the editor. 
  */ 
  package cn.feelingsoft.photomgr.util; 
  import java.io.File; 
  /** 
  * 
  * @author Sanwu 
  */ 
  public class StringUtil { 
  public static String getRelativePath(String rootPath, String absolutePath) { 
  if (null == rootPath 
  || null == absolutePath 
  || rootPath.trim().equals("") 
  || absolutePath.trim().equals("")) { 
  return null; 
  } 
  //统一表示格式 
  rootPath = new File(rootPath).getPath(); 
  absolutePath = new File(absolutePath).getAbsolutePath(); 
  if (absolutePath.startsWith(rootPath)){ 
  return absolutePath.substring(rootPath.length()); 
  } 
  return null; 
  } 
  } 
  /* 
  * To change this template, choose Tools | Templates 
  * and open the template in the editor. 
  */ 
  /* 
  * MainJFrame1.java 
  * 
  * Created on 2011-3-4, 10:26:15 
  */ 
  package cn.feelingsoft.photomgr.ui; 
  import cn.feelingsoft.photomgr.i.FileSearchListener; 
  import cn.feelingsoft.photomgr.model.SwingWorkerFileSearc h; 
  import java.util.ArrayList; 
  /** 
  * 
  * @author Sanwu 
  */ 
  public class MainJFrame1 extends javax.swing.JFrame implements FileSearchListener { 
  /** Creates new form MainJFrame1 */ 
  public MainJFrame1() { 
  initComponents(); 
  } 
  /** This method is called from within the constructor to 
  * initialize the form. 
  * WARNING: Do NOT modify this code. The content of this method is 
  * always regenerated by the Form Editor. 
  */ 
  @SuppressWarnings("unchecked") 
  //  
  private void initComponents() { 
  jPanel2 = new javax.swing.JPanel(); 
  jScrollPane1 = new javax.swing.JScrollPane(); 
  jTextAreaSearchResult = new javax.swing.JTextArea(); 
  jPanel1 = new javax.swing.JPanel(); 
  jButton1 = new javax.swing.JButton(); 
  jScrollPane2 = new javax.swing.JScrollPane(); 
  jTextArea1 = new javax.swing.JTextArea(); 
  setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE); 
  jTextAreaSearchResult.setColumns(20); 
  jTextAreaSearchResult.setEditable(false); 
  jTextAreaSearchResult.setRows(5); 
  jTextAreaSearchResult.setTabSize(4); 
  jTextAreaSearchResult.setFocusable(false); 
  jScrollPane1.setViewportView(jTextAreaSearchResult ); 
  javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); 
  jPanel2.setLayout(jPanel2Layout); 
  jPanel2Layout.setHorizontalGroup( 
  jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING) 
  .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE) 
  ); 
  jPanel2Layout.setVerticalGroup( 
  jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING) 
  .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE) 
  ); 
  jButton1.setText("jButton1"); 
  jButton1.addActionListener(new java.awt.event.ActionListener() { 
  public void actionPerformed(java.awt.event.ActionEvent evt) { 
  jButton1ActionPerformed(evt); 
  } 
  }); 
  jTextArea1.setColumns(20); 
  jTextArea1.setRows(5); 
  jScrollPane2.setViewportView(jTextArea1); 
  javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
  jPanel1.setLayout(jPanel1Layout); 
  jPanel1Layout.setHorizontalGroup( 
  jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING) 
  .addGroup(jPanel1Layout.createSequentialGroup() 
  .addContainerGap() 
  .addComponent(jButton1) 
  .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED) 
  .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE) 
  .addContainerGap()) 
  ); 
  jPanel1Layout.setVerticalGroup( 
  jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING) 
  .addGroup(jPanel1Layout.createSequentialGroup() 
  .addContainerGap() 
  .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING) 
  .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE) 
  .addComponent(jButton1)) 
  .addContainerGap()) 
  ); 
  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
  getContentPane().setLayout(layout); 
  layout.setHorizontalGroup( 
  layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING) 
  .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
  .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
  ); 
  layout.setVerticalGroup( 
  layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING) 
  .addGroup(layout.createSequentialGroup() 
  .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
  .addGap(1, 1, 1) 
  .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
  ); 
  pack(); 
  }//  
  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
  jTextArea1.append("button clicked.\n"); 
  } 
  /** 
  * @param args the command line arguments 
  */ 
  public static void main(String args[]) { 
  java.awt.EventQueue.invokeLater(new Runnable() { 
  public void run() { 
  MainJFrame1 mf = new MainJFrame1(); 
  mf.setVisible(true); 
  } 
  }); 
  new SwingWorkerFileSearch().execute(); 
  } 
  // Variables declaration - do not modify 
  private javax.swing.JButton jButton1; 
  private javax.swing.JPanel jPanel1; 
  private javax.swing.JPanel jPanel2; 
  private javax.swing.JScrollPane jScrollPane1; 
  private javax.swing.JScrollPane jScrollPane2; 
  private javax.swing.JTextArea jTextArea1; 
  private javax.swing.JTextArea jTextAreaSearchResult; 
  // End of variables declaration 
  public void processFileSearchOver(ArrayList arrayList) { 
  for (String str : arrayList) { 
  jTextAreaSearchResult.append(str + "\n"); 
  } 
  } 
  } 
  /* 
  * To change this template, choose Tools | Templates 
  * and open the template in the editor. 
  */ 
  package cn.feelingsoft.photomgr.i; 
  import java.util.ArrayList; 
  /** 
  * 
  * @author Sanwu 
  */ 
  public interface FileSearchListener { 
  void processFileSearchOver(ArrayList arrayList); 
  } 
  以下是测试代码 
  public static void main(String[] args) { 
  final String userDir = "H:/客户照片"; 
  MainJFrame1 mf = new MainJFrame1(); 
  SwingWorkerFileSearch swfs = new SwingWorkerFileSearch(userDir, FileUtil.ABSOLUTEPATH); 
  swfs.addFileSearchListener(mf); 
  mf.setVisible(true); 
  swfs.execute(); 
  } 
分享到:
评论

相关推荐

    Swingworker

    swing swingworker wingworker wingworker

    使用SwingWorker异步加载JTree

    NULL 博文链接:https://vearn.iteye.com/blog/344591

    Swing线程的深入理解和SwingWorker基础知识介绍[参照].pdf

    Swing线程的深入理解和SwingWorker基础知识介绍[参照].pdf

    Java的Swing编程中使用SwingWorker线程模式及顶层容器

    主要介绍了在Java的Swing编程中使用SwingWorker线程模式及顶层容器的方法,适用于客户端图形化界面软件的开发,需要的朋友可以参考下

    SWT与SwingWorker的结合

    Swt一般情况不允许在另外一个线程中更新控件内容,此程序使用java swing的SwingWorker对swt的控件进行操作更新。对于喜欢使用SwingWorker更新Swing控件的人来说,也可以使用SwingWorker更新Swt控件

    Swing线程的深入理解和SwingWorker基础知识介绍

    Swing线程的深入理解和SwingWorker基础知识介绍

    swingworker排序

    作业:完善swingworker排序功能

    Swing线程的最后讨论 -- 利用异步模型

    第二篇文章《使用Swing Worker线程》,演示了如何使用SwingWorker线程工具类。它也可以在存档中找到。 本文介绍了修订过的SwingWorker类,并演示了和基于模型的组件(model-based components)如JTable和JTree同时...

    Swing线程的最后讨论--利用异步模型

    第二篇文章《使用SwingWorker线程》,演示了如何使用SwingWorker线程工具类。它也可以在存档中找到。本文介绍了修订过的SwingWorker类,并演示了和基于模型的组件(model-basedcomponents)如JTable和JTree同时使

    An Alternative to SwingWorker.java-开源

    SwingWorker的替代品,用于在Swing中执行异步任务。 我们将SwingWorker.java与Action接口合并,生成AbstractAsynchronousAction.java。 此类的具体扩展可以用来代替任何Action。

    开发J2EE应用的要领

    第二篇文章《使用SwingWorker线程》,演示了如何使用SwingWorker线程工具类。它也可以在存档中找到。本文介绍了修订过的SwingWorker类,并演示了和基于模型的组件(model-basedcomponents)如JTable和JTree同时使

    ProcessBar 2种经典实现

    NULL 博文链接:https://chenhailong.iteye.com/blog/1312202

    Java21Days_Exercises:学习Java的练习

    Java21Days_Exercises 学习了《 21天的Java》一书中学习Java的练习。 类,它们的实例和方法。 覆盖,使用“ this”和“ super”。 包,接口,访问修饰符。 异常(尝试,捕获,引发,抛出)和线程。 数据结构(BitSet...

    swing-worker-troubleshooting:回购以解决SwingWorker的问题

    要通过修改代码来使用AnalysisWorker ,看到里面的代码注释SimpleApp的上单击处理程序analyzeButton 。 您只需要注释掉try-catch块并取消注释工作代码即可。 此外,该解决方案还需要: 处理从工作程序内部抛出的...

    互联网程序设计实验六.doc

    (3)学习Swing界面设计技术 二、实验环境 多媒体计算机 Windows7操作系统 JDK 8 Netbeans IDE8.2(开发语言Java) 实验内容 重温本章完成的基于TCP协议的客户机/服务器文件传输项目,综合运用Swing界面设计 ....

    swing-worker-1.1.jar

    Exceptionin thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/jdesktop/swingworker/SwingWorker at com.android.draw9patch.Application$1.run(Application.java:48) at java.awt.event....

    java动态按钮

    运用javax.swingworker类生成DynamicJButton示例

    Java快速实现的进度条

    使用JProgressBar和SwingWorker做的Java进度条 简单方便 效果看这里:http://blog.csdn.net/kakashi8841/archive/2011/05/03/6388797.aspx

    Swing MVC 观察者 电子相册

    Swing MVC 观察者 系统托盘 SwingWorker 电子相册

    swing不确定进度条

    不确定某个程序运行多久,用进度条来监测,如果进度条监测到运行结果结束了,就退出进度条,否则以一种不确定的状态等待。

Global site tag (gtag.js) - Google Analytics