- 浏览: 17039 次
最新评论
学习使用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();
}
发表评论
-
第7章 委托和事件
2012-01-20 01:08 655第7章 委托和事件 2011 ... -
史上最强劲之android模拟器命令详解
2012-01-20 01:08 757史上最强劲之android模拟器命令详解 2010年11月2 ... -
android_adb命令
2012-01-20 01:08 669android_adb命令 2010年12月17日 An ... -
虚拟研讨会:移动Web应用开发技术最新动态
2012-01-20 01:08 692虚拟研讨会:移动Web应用 ... -
编译Boost 1.38
2012-01-20 01:08 635编译Boost 1.38 2010年10月28日 首先, ... -
程序工具
2012-01-19 08:57 547程序工具 2010年10月15日 开发(Developm ... -
arm linux 编译环境建立(文摘)
2012-01-19 08:57 455arm linux 编译环境建立(文摘) 2011年02月2 ... -
计算机概论
2012-01-19 08:57 521计算机概论 2011年04月26日 import 我.认 ... -
探索C++的秘密之一详解extern "C"
2012-01-19 08:57 594探索C++的秘密之一详解extern "C" ... -
库文件和系统调用的区别
2012-01-19 08:57 591库文件和系统调用的区 ... -
国产机免流下载网上资源方法(仅限移动用户
2012-01-17 01:27 486国产机免流下载网上资源方法(仅限移动用户 2011年06月0 ... -
2010-11-15
2012-01-17 01:27 5632010-11-15 2010年11月15日 ... -
2011-1-20
2012-01-17 01:27 6792011-1-20 2011年01月20日 ... -
电脑蓝屏自动关机故障的检修方法
2012-01-17 01:27 760电脑蓝屏自动关机故障的检修方法 2011年09月23日 ... -
liniux学习
2012-01-17 01:27 872liniux学习 2010年06月08日 ... -
身历六帝宠不衰
2012-01-15 20:08 599身历六帝宠不衰 2009年0 ... -
唐太宗,霸占过隋炀帝之妻吗?(图)
2012-01-15 20:08 504唐太宗,霸占过隋炀帝之 ... -
传奇美人之萧皇后
2012-01-15 20:08 586传奇美人之萧皇后 2010年06月03日 命运奇特的女人 ... -
花果山的快乐之家――邻水中学实习队
2012-01-15 20:08 563花果山的快乐之家――邻水中学实习队 2009年06月08日 ... -
Power designer 15生成sql server 2005/2008列注释
2012-01-11 12:17 626Power designer 15生成sql server 2 ...
相关推荐
swing swingworker wingworker wingworker
NULL 博文链接:https://vearn.iteye.com/blog/344591
Swing线程的深入理解和SwingWorker基础知识介绍[参照].pdf
主要介绍了在Java的Swing编程中使用SwingWorker线程模式及顶层容器的方法,适用于客户端图形化界面软件的开发,需要的朋友可以参考下
Swt一般情况不允许在另外一个线程中更新控件内容,此程序使用java swing的SwingWorker对swt的控件进行操作更新。对于喜欢使用SwingWorker更新Swing控件的人来说,也可以使用SwingWorker更新Swt控件
Swing线程的深入理解和SwingWorker基础知识介绍
作业:完善swingworker排序功能
第二篇文章《使用Swing Worker线程》,演示了如何使用SwingWorker线程工具类。它也可以在存档中找到。 本文介绍了修订过的SwingWorker类,并演示了和基于模型的组件(model-based components)如JTable和JTree同时...
第二篇文章《使用SwingWorker线程》,演示了如何使用SwingWorker线程工具类。它也可以在存档中找到。本文介绍了修订过的SwingWorker类,并演示了和基于模型的组件(model-basedcomponents)如JTable和JTree同时使
SwingWorker的替代品,用于在Swing中执行异步任务。 我们将SwingWorker.java与Action接口合并,生成AbstractAsynchronousAction.java。 此类的具体扩展可以用来代替任何Action。
第二篇文章《使用SwingWorker线程》,演示了如何使用SwingWorker线程工具类。它也可以在存档中找到。本文介绍了修订过的SwingWorker类,并演示了和基于模型的组件(model-basedcomponents)如JTable和JTree同时使
NULL 博文链接:https://chenhailong.iteye.com/blog/1312202
Java21Days_Exercises 学习了《 21天的Java》一书中学习Java的练习。 类,它们的实例和方法。 覆盖,使用“ this”和“ super”。 包,接口,访问修饰符。 异常(尝试,捕获,引发,抛出)和线程。 数据结构(BitSet...
要通过修改代码来使用AnalysisWorker ,看到里面的代码注释SimpleApp的上单击处理程序analyzeButton 。 您只需要注释掉try-catch块并取消注释工作代码即可。 此外,该解决方案还需要: 处理从工作程序内部抛出的...
(3)学习Swing界面设计技术 二、实验环境 多媒体计算机 Windows7操作系统 JDK 8 Netbeans IDE8.2(开发语言Java) 实验内容 重温本章完成的基于TCP协议的客户机/服务器文件传输项目,综合运用Swing界面设计 ....
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....
运用javax.swingworker类生成DynamicJButton示例
使用JProgressBar和SwingWorker做的Java进度条 简单方便 效果看这里:http://blog.csdn.net/kakashi8841/archive/2011/05/03/6388797.aspx
Swing MVC 观察者 系统托盘 SwingWorker 电子相册
不确定某个程序运行多久,用进度条来监测,如果进度条监测到运行结果结束了,就退出进度条,否则以一种不确定的状态等待。