2012-08-14 48 views
1

我想在執行一些代碼之前,通過SelectionListener設置一個ProgressBar。 但是,代碼將被執行,但ProgressBar仍然不可見。setVisible()在eclipse中執行代碼之前SWT

的代碼如下:

... 
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

如何執行doSth()設置progressBar後可見?

全碼:

類Anonymous2:

package main; 

import view.MainView; 

public class Anonymous2 { 

    public static void main(String[] args) { 
      new MainView().open(); 
    } 
} 

類的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; 
} 
} 

類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) {} 


} 

類分析器:

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),以便解析器可以工作(replace-Button)至少1秒鐘。 progressBar將在一秒後可見,並應在點擊按鈕後立即可見。

+0

請給出更多關於'doSth()'做些什麼的信息。 – 2012-08-14 12:12:29

+0

我上面編輯過 – user1597957 2012-08-14 12:22:55

+1

@ user1597957 - 很難從你的代碼片段中知道問題。我有一個簡單的應用程序,正在按照您想要的結果工作。如果你能提供一段可執行的代碼,這將有所幫助。 – Favonius 2012-08-16 09:03:24

回答

相關問題