我想在执行某些代码之前通过ProgressBar
设置SelectionListener
。
但是,代码将被执行,但ProgressBar
仍然不可见。
代码如下:
...
progressBar().setVisible(true);
parseFile(...);
...
public void parseFile(File file) {
System.out.println("Parsing File: " + file);
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(
new InputStreamReader(new FileInputStream(file), "UTF-8"));
File fileToWrite = new File(file.getParentFile().getAbsolutePath()
+ File.separator + "anonym_"
+ file.getName());
bw = new BufferedWriter(new FileWriter(fileToWrite));
String line;
while ((line = br.readLine()) != null) {
bw.write(replacements.getSubstitutedText(line));
bw.newLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (bw != null) {
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
parseFile()
将被执行,但setVisible
将不会执行。如果我删除parseFile()
,则会执行setVisible
。
如何在设置progressBar后显示doSth()
?
完整代码:
Anonymous2类:
package main;
import view.MainView;
public class Anonymous2 {
public static void main(String[] args) {
new MainView().open();
}
}
Class MainView:
package view;
import java.io.File;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import controller.MainViewController;
public class MainView {
protected Shell shlAnonymous;
private List list;
private Table table;
private TableViewer tableViewer;
private TableEditor editor;
private ProgressBar progressBar;
public Table getTable() {
return table;
}
public TableViewer getTableViewer() {
return tableViewer;
}
public Shell getShell() {
return shlAnonymous;
}
public TableEditor getEditor() {
return editor;
}
public ProgressBar getProgressBar() {
return progressBar;
}
/**
* @wbp.parser.entryPoint
*/
public void open() {
Display display = Display.getDefault();
shlAnonymous = new Shell();
shlAnonymous.setSize(800, 404);
shlAnonymous.setText("Anonymous");
shlAnonymous.setLayout(new FormLayout());
createContents();
shlAnonymous.open();
shlAnonymous.layout();
while (!shlAnonymous.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
MainViewController controller = new MainViewController(this);
Label lblHeader = new Label(shlAnonymous, SWT.NONE);
FormData fd_lblHeader = new FormData();
fd_lblHeader.top = new FormAttachment(0, 10);
fd_lblHeader.left = new FormAttachment(0, 10);
lblHeader.setLayoutData(fd_lblHeader);
lblHeader.setText("Files to Replace");
Button btnQuit = new Button(shlAnonymous, SWT.NONE);
btnQuit.addSelectionListener(controller);
FormData fd_btnQuit = new FormData();
fd_btnQuit.left = new FormAttachment(100, -106);
fd_btnQuit.bottom = new FormAttachment(100, -10);
fd_btnQuit.right = new FormAttachment(100, -10);
btnQuit.setLayoutData(fd_btnQuit);
btnQuit.setText("Quit");
btnQuit.setData("name", "quit");
Button btnAnon = new Button(shlAnonymous, SWT.NONE);
btnAnon.addSelectionListener(controller);
FormData fd_btnAnon = new FormData();
fd_btnAnon.right = new FormAttachment(btnQuit, 0, SWT.RIGHT);
fd_btnAnon.bottom = new FormAttachment(btnQuit, -6);
fd_btnAnon.left = new FormAttachment(btnQuit, 0, SWT.LEFT);
btnAnon.setLayoutData(fd_btnAnon);
btnAnon.setText("Replace");
btnAnon.setData("name", "anon");
Button btnAdd = new Button(shlAnonymous, SWT.NONE);
btnAdd.addSelectionListener(controller);
FormData fd_btnAdd = new FormData();
fd_btnAdd.right = new FormAttachment(0, 106);
fd_btnAdd.bottom = new FormAttachment(100, -10);
fd_btnAdd.left = new FormAttachment(0, 10);
btnAdd.setLayoutData(fd_btnAdd);
btnAdd.setText("Add");
btnAdd.setData("name", "add");
Button btnDelete = new Button(shlAnonymous, SWT.NONE);
btnDelete.addSelectionListener(controller);
FormData fd_btnDelete = new FormData();
fd_btnDelete.right = new FormAttachment(btnAdd, 102, SWT.RIGHT);
fd_btnDelete.bottom = new FormAttachment(100, -10);
fd_btnDelete.left = new FormAttachment(btnAdd, 6);
btnDelete.setLayoutData(fd_btnDelete);
btnDelete.setText("Delete");
btnDelete.setData("name", "delete");
list = new List(shlAnonymous, SWT.BORDER | SWT.MULTI);
list.addKeyListener(controller);
FormData fd_list = new FormData();
fd_list.left = new FormAttachment(0, 10);
fd_list.bottom = new FormAttachment(btnAnon, 0, SWT.BOTTOM);
fd_list.top = new FormAttachment(lblHeader, 6);
list.setLayoutData(fd_list);
table = new Table(shlAnonymous, SWT.BORDER | SWT.FULL_SELECTION);
table.setLinesVisible(true);
fd_list.right = new FormAttachment(table, -6);
FormData fd_table = new FormData();
fd_table.right = new FormAttachment(btnAnon, -22);
fd_table.bottom = new FormAttachment(100, -41);
fd_table.top = new FormAttachment(0, 31);
fd_table.left = new FormAttachment(0, 336);
table.setLayoutData(fd_table);
table.setHeaderVisible(true);
tableViewer = new TableViewer(table);
tableViewer.setUseHashlookup(true);
TableColumn tblclmnTerm = new TableColumn(table, SWT.NONE);
tblclmnTerm.setWidth(160);
tblclmnTerm.setText("Term");
TableItem tableItem = new TableItem(table, SWT.NONE);
tableItem.setText("");
TableColumn tblclmnSubstitution = new TableColumn(table, SWT.NONE);
tblclmnSubstitution.setWidth(156);
tblclmnSubstitution.setText("Replace With");
progressBar = new ProgressBar(shlAnonymous, SWT.NONE);
FormData fd_progressBar = new FormData();
fd_progressBar.right = new FormAttachment(table, 0, SWT.RIGHT);
fd_progressBar.bottom = new FormAttachment(btnQuit, 0, SWT.BOTTOM);
fd_progressBar.left = new FormAttachment(btnDelete, 6);
progressBar.setLayoutData(fd_progressBar);
progressBar.setMinimum(0);
progressBar.setVisible(false);
}
public void addListItem(String item) {
list.add(item);
}
public void addListItems(String rootFolder, String[] items) {
for (String item : items) {
addListItem(rootFolder + File.separator + item);
}
}
public File[] getFilesToParse() {
File[] files = new File[list.getItemCount()];
for (int i = 0; i < list.getItemCount(); i++) {
files[i] = new File(list.getItem(i));
}
return files;
}
}
Class MainViewController:
package controller;
import model.Parser;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.FileDialog;
import view.MainView;
public class MainViewController implements SelectionListener, KeyListener {
private MainView mainView;
public MainViewController(MainView mainView) {
this.mainView = mainView;
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
processSelectionEvent(arg0);
}
@Override
public void widgetSelected(SelectionEvent arg0) {
processSelectionEvent(arg0);
}
private void processSelectionEvent(SelectionEvent e) {
if (e.getSource() instanceof Button) {
Button button = (Button)e.getSource();
if (button.getData("name").equals("quit")) {
} else if (button.getData("name").equals("anon")) {
mainView.getProgressBar().setVisible(true);
Parser parser = new Parser();
parser.parseFiles(mainView.getFilesToParse());
} else if (button.getData("name").equals("add")) {
FileDialog fileDialog = new FileDialog(mainView.getShell(), SWT.MULTI);
fileDialog.setText("Dateien wählen");
fileDialog.open();
mainView.addListItems(fileDialog.getFilterPath(), fileDialog.getFileNames());
}
}
}
@Override
public void keyPressed(KeyEvent arg0) {}
@Override
public void keyReleased(KeyEvent arg0) {}
}
Class Parser:
package model;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class Parser {
public boolean parseFile(File file) {
System.out.println("Parsing File: " + file);
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(
new InputStreamReader(new FileInputStream(file), "UTF-8"));
File fileToWrite = new File(file.getParentFile().getAbsolutePath()
+ File.separator + "anonym_"
+ file.getName());
bw = new BufferedWriter(new FileWriter(fileToWrite));
String line = br.readLine();
if (line != null) {
bw.write(line);
while ((line = br.readLine()) != null) {
bw.newLine();
bw.write(line);
}
}
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (bw != null) {
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
public void parseFiles(File[] files) {
for (File file : files) {
parseFile(file);
}
}
}
我已经削减了很多代码,但问题仍然存在。
有问题的行是Parser类中的第40-42行。第40行在第41-42行之后执行。
一些解释: 解析器完成工作后,progressBar变为可见。要查看问题,我建议添加足够多的文件(add-Button),以便解析器可以工作(替换 - 按钮)至少1秒。 progressBar将在一秒钟后显示,并且在单击按钮后应立即可见。
答案 0 :(得分:0)
您不应该在UI线程中运行解析器。当您的代码解析时,swt没有机会重新绘制Progressbar。因此解析完成后会绘制Progressbar。您应该在一个单独的线程中进行解析,并使用Display.asyncExec()从此Thread更新Progressbar。有关如何执行此操作的示例,请参阅http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet56.java。