我想将Service guava接口用作SwingWorker,因为在SwingWorker中我总是需要一个开始和结束状态.
我应该这样写:
class WorkerSomething extends
javax.swing.SwingWorker<Void, com.google.common.util.concurrent.Service.State>
implements com.google.common.util.concurrent.Service
Run Code Online (Sandbox Code Playgroud)
这样,我只能使用番石榴服务状态来携带中间结果.但我需要其他一些中间状态来发布我的GUI.
我怎样才能做到这一点 ?这个想法是个好主意吗?
谢谢.
我一直在努力获取系统信息 Runtime.getRuntime().exec(cmd);
我收到以下错误:
java.lang.NullPointerException
at java.lang.Runtime.exec(Runtime.java:422)
at java.lang.Runtime.exec(Runtime.java:326)
at mytest.SystemInfoToTextArea.doInBackground(SystemInfoToTextArea.java:31)
at mytest.SystemInfoToTextArea.doInBackground(SystemInfoToTextArea.java:16)
Run Code Online (Sandbox Code Playgroud)
欢迎任何建议
课程代码
public class SystemInfoToTextArea extends SwingWorker<String, Void> {
private JTextArea textArea;
private String cmd;
public SystemInfoToTextArea(JTextArea textArea, String cmd) {
textArea = textArea;
cmd = cmd;
}
@Override
protected String doInBackground() throws Exception {
String outputMem = null;
try {
String lineMem;
outputMem = "";
Process procMem = Runtime.getRuntime().exec(cmd);
BufferedReader input =
new BufferedReader
(new InputStreamReader(procMem.getInputStream()));
while ((lineMem = input.readLine()) != null) {
outputMem = (lineMem + …Run Code Online (Sandbox Code Playgroud) 我正在编写一个可以进行音频分析的应用程序(用作吉他调音器,增加了和弦估计和其他一些东西),但我在使用GUI时遇到了一些问题.第一个问题是我有一个按钮,点击后应更改其显示的文本.单击它时,文本不会更改,但是应该更改应该更改它的代码行.
另一件事是我需要一个SwingWorker来重复执行(一旦完成就重新启动)并每次更新GUI.正如我的代码目前,我有一个while循环重复执行我的SwingWorker,但这导致GUI变得无响应,因为它在EDT中运行(大概).让SwingWorker重复执行的最佳方法是什么?我应该创建一个新线程来运行循环,还是其他什么?
我内部ActionListener类的代码如下:
private class TunerListener implements ActionListener {
private boolean firstUpdate = true;
private volatile boolean executing = false;
private final SwingWorker<Void, Void> tunerWorker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() {
model.update();
return null;
}
@Override
protected void done() {
if (!this.isCancelled()) {
prev.setText(model.getPrev());
currentNote.setText(model.getNote());
next.setText(model.getNext());
frequency.setText(model.getFrequency());
switch (model.getOffset()) {
case -2:
light_2.setIcon(onRed);
light_1.setIcon(off);
light0.setIcon(offBig);
light1.setIcon(off);
light2.setIcon(off);
break;
case -1:
light_2.setIcon(off);
light_1.setIcon(onRed);
light0.setIcon(offBig);
light1.setIcon(off);
light2.setIcon(off);
break;
case 0:
light_2.setIcon(off);
light_1.setIcon(off);
light0.setIcon(onGreen);
light1.setIcon(off);
light2.setIcon(off);
break;
case 1: …Run Code Online (Sandbox Code Playgroud) 需要一个例子,如何在SwingWorker中使用计时器?因为无法像我那样将ActionListener添加到我的Timer中!
private Timer timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
}
});
Run Code Online (Sandbox Code Playgroud)
消息错误:构造函数Timer(int,new ActionListener(){})未定义
我最初尝试在Java Action Listener中多次更新JFrame和JPanel,但两者都只会在Action Listener完成所有任务时更新.这是我原始问题的链接(在Action Listener中刷新JFrame).
我在反馈中被告知Swing Worker应该解决我的问题.但是,当我实现Swing Worker(如下所示)时,没有任何改变.仅当Action Listener完成所有任务时,JFrame和JPanel仍会更新.我的问题是,我错过了下面的内容吗?如果没有,我如何在Action Listener中实现它以及时正确更新Frame和Panel?
@Override
protected Integer doInBackground() throws Exception{
//Downloads and unzips the first video.
if(cameraBoolean==true)
panel.add(this.downloadRecording(camera, recording));
else
panel.add(new JLabel("Could not contact camera "+camera.getName()));
panel.repaint();
jframe.repaint();
return 1;
}
private JLabel downloadRecording(Camera camera, Recording recording){
//does a bunch of calculations and returns a jLabel, and works correctly
}
protected void done(){
try{
Date currentTime = new Timestamp(Calendar.getInstance().getTime().getTime());
JOptionPane.showMessageDialog(jframe, "Camera "+camera.getName()+" finished downloading at "+currentTime.getTime());
}catch (Exception e){
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个使用JPA的项目J2SE.在一些JDialogs中,我会getResultList()在类的构造函数中返回并填充JTable,JComboBox,JList等.
因此,当我为这些对话框创建任何实例时,有时会很慢.
我认为使用SwingWorker和JProgressbar创建一个(加载)打开JDialogs是一个很好的解决方案,但我不知道该怎么做.
我正在尝试这个.
//
JProgressbar progress = new JProgressBar();
//custommer dialog
JDialog custommer = new JDialog(); //here slow because I have List<Customer> and others lists
custommer.setModal(true);
private void openDialogs(JDialog dialog){
progress.setVisible(true);
progress.setIndeterminate(true);
SwingWorker sw = new SwingWorker(){
protected Object doInBackground(){
//opening dialog
dialog.setVisible(true);
return null;
}
}
//after opened
protected void done(){
progress.setVisible(false);
}
}
Run Code Online (Sandbox Code Playgroud)
如何打开JDialog并创建一个用SwingWorker和JProgressbar打开的加载?
我有一个简单的Swing按钮刷新jtable,但是在随机点击后,应用程序冻结,有时无限期,有时它会在一分钟左右后响应.我从来没有做任何特定于线程的方法所以我想也许s the issue but I don知道从哪里开始或如何修改方法,如果这是冻结的原因.
这是代码:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
recorrerLista();
}
public EventList recorrerLista(){
Persistir p = Persistir.getInstancia();
Vector<Pedidos> lista2 = p.listarPedidos();
Iterator it = lista2.iterator();
if(!pedList.isEmpty()){
pedList.clear();
}
while(it.hasNext()){
Vector v = new Vector();
v = (Vector) it.next();
int index = (Integer)v.get(0);
Pedidos ped = new Pedidos();
ped = p.buscarPedidoPorId(index);
pedList.add(ped);
}
return pedList;
}
Run Code Online (Sandbox Code Playgroud)
编辑:我尝试在SwingWorker中插入持久化类(Persistir)的方法,但现在UI不显示.
这是代码
public Vector listarPedidos() throws InterruptedException, ExecutionException{
SwingWorker listar = new SwingWorker<Vector, Vector>(){
@Override
protected Vector doInBackground() throws …Run Code Online (Sandbox Code Playgroud) 根据我之前的问题,拥有动态JTable的最佳方法是什么,其数据会随着数据库表中的更改而自行更新?,我使用以下代码自动更新jList
DefaultListModel x = new DefaultListModel();
jList1.setModel(x);
int initialDelay = 0;
int period = 5000;
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
x.clear();
try {
conn = Database.Connect("patient.db");
sql = "SELECT * FROM PATIENT_APPOINTMENTS";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next()) {
x.addElement(rs.getString("patientid"));
}
}
catch ( Exception e) {
JOptionPane.showMessageDialog(null,e,"Error",JOptionPane.ERROR_MESSAGE);
}
}
};
timer.scheduleAtFixedRate(task,initialDelay,period);
Run Code Online (Sandbox Code Playgroud)
但是我已经阅读了有关SwingWorker的内容,它似乎是正确的方法.swing工作程序的正常实现有3个受保护的doInBackground方法,发布和完成.我还尝试了使用SwingWorker的另一个版本
DefaultListModel x = new DefaultListModel();
jList1.setModel(x);
int initialDelay = 0;
int period = 5000; …Run Code Online (Sandbox Code Playgroud) 我正在使用SwingWorker类使进程在另一个线程中运行。我想要的是,一旦这个线程完成处理,它应该返回 aString并且它应该启用 a JMenuItem。我正在使用类中的done()方法SwingWorker来启用,JMenuItem但我收到一个NullPinterException. 该doInBackground()方法返回一个我想在主 GUI 类中访问的字符串 - GUIMain.java,存在于同一个包中。我该怎么做?我看到了很多实现done()或onPostExecute()方法的例子,但我认为我在某个地方出错了。这是我实现的代码:
public class GUIMain extends JFrame implements ActionListener, FocusListener, ItemListener, MouseListener, MouseMotionListener {
private JMenuBar menuBar; // Defined a menuBar item
private JMenu recalibrationMenu; // Define the recalibration menu item
private JMenuItem CGMenuItem;
private JMenuItem TGMenuItem;
private JMenu viewResultsMenu; // Define the View Results menu item
public JMenuItem cgResults;
public JMenuItem tgResults; …Run Code Online (Sandbox Code Playgroud) 我有个问题.
我有一个JFrame.它会创造一个JDialog.
当按下按钮时JDialog,应该处理它并发送电子邮件.与此同时,我需要另一个JDialog不确定的出现JProgressBar.发送电子邮件时,JDialog应该处理(和新的一个),或者它的内容应该更改.
我已经失败了好几个小时了,所以我问enyone他(或她)是否愿意给我写一个能做我想做的伪代码.
只是为了看看SwingWorker课程中应该包含什么(或者如果你觉得它更好的话就使用多线程),何时JDialog应该创建/处理,以及在电子邮件发送中的位置...
我知道我在这里要求一个完整的解决方案,但我已经在dedline并且已经失败了很多次......这是我的最后一招......