2013-02-22 44 views
0

我想创建一个浏览按钮FileDialog和Java SWT中的复合。 (复合,因为我是用CTabFolderCTabItem,我觉得将组件添加到CTabItem复合材料是不够好)FileDialog和复合

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.util.StringTokenizer; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.widgets.Button; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.FileDialog; 
import org.eclipse.swt.widgets.Group; 
import org.eclipse.swt.widgets.Label; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.widgets.TabFolder; 
import org.eclipse.swt.widgets.TabItem; 
import org.eclipse.swt.widgets.Table; 
import org.eclipse.swt.widgets.TableColumn; 
import org.eclipse.swt.widgets.TableItem; 
import org.eclipse.swt.widgets.Text; 
import org.eclipse.swt.custom.CBanner; 
import org.eclipse.swt.events.SelectionAdapter; 
import org.eclipse.swt.events.SelectionEvent; 
import org.eclipse.swt.graphics.Point; 
import org.eclipse.swt.layout.FillLayout; 
import org.eclipse.wb.swt.SWTResourceManager; 

public class DemoShell extends Shell { 
    protected Composite composite; 
    private Text filename; 
    private Table table; 
    protected Shell shell; 

public static void main(String args[]) { 
    try { 
Display display = Display.getDefault(); 
DemoShell shell = new DemoShell(display); 
shell.open(); 
shell.layout(); 
while (!shell.isDisposed()) { 
if (!display.readAndDispatch()) { 
    display.sleep(); 
} 
} 
} catch (Exception e) { 
e.printStackTrace(); 
} 
} 

private static boolean parseFile(Table table, String filename) { 
    try { 
      BufferedReader br = new BufferedReader(new FileReader(filename)); 
      String line = ""; 
      StringTokenizer token = null, subtoken = null; 

      int tokenNum = 1; 

      while((line = br.readLine()) != null) { 

       // lineNum++;      
       // break comma separated file line by line 

       token = new StringTokenizer(line, ";"); 
       String sno = null; 

       while(token.hasMoreTokens()) { 
        subtoken = new StringTokenizer(token.nextToken().toString(), ","); 
        sno = ""; 
        sno = Integer.toString(tokenNum); 
        TableItem item = new TableItem (table, SWT.NONE); 
        item.setText (0, sno); 
        item.setText (1, subtoken.nextToken()); 
        item.setText (2, subtoken.nextToken()); 
        item.setText (3, subtoken.nextToken()); 
        item.setText (4, subtoken.nextToken()); 
        item.setText (5, subtoken.nextToken()); 
         tokenNum++; 
         System.out.println("S.No # " + tokenNum + token.nextToken()); 
       } 

       tokenNum = 0; 
      } 
      br.close(); 
      return true; 
    } catch(Exception e) { 
      System.err.println("Parse Error: " + e.getMessage()); 
      return false; 
    } 
    } 


public void displayFiles(String[] files) { 
     for (int i = 0; files != null && i < files.length; i++) { 
      filename.setText(files[i]); 
      filename.setEditable(false); 
     } 
    } 
/** 
* Create the shell. 
* @param display 
*/ 
public DemoShell(Display display) { 
    super(display, SWT.SHELL_TRIM); 

    TabFolder tabFolder = new TabFolder(this, SWT.NONE); 
    tabFolder.setBounds(10, 10, 462, 268); 

    TabItem re_item = new TabItem(tabFolder, SWT.NONE); 
    re_item.setText("Road Network"); 

    TabItem ttable_item = new TabItem(tabFolder, SWT.NONE); 
    ttable_item.setText("Time Table"); 

    createContents_tt(tabFolder, ttable_item); 
} 

/** 
* Create contents of the shell. 
*/ 
protected void createContents_tt(TabFolder tabFolder, TabItem ttable_item) { 
    setText("SWT Application"); 
    setSize(530, 326); 


    //Table declaration 
    table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION); 
    table.setBounds(10, 153, 450, 107); 
    table.setHeaderVisible(true); 
    table.setLinesVisible(true); 
    String[] titles = {"S.No", "Route", "Transport #", "Cross Point", "Start Time", "End Time"}; 
    for (int i=0; i<titles.length; i++) { 
     TableColumn column = new TableColumn (table, SWT.NONE); 
     column.setText (titles [i]); 
    } 
    for (int i=0; i<titles.length; i++) { 
     table.getColumn (i).pack(); 
    } 


    Composite composite = new Composite(tabFolder, SWT.NONE); 
      composite.setLayout(new FillLayout()); 

    Group group_1 = new Group(shell, SWT.NONE); 
    group_1.setBounds(10, 10, 450, 127); 

    filename = new Text(group_1, SWT.BORDER); 
    filename.setBounds(31, 95, 377, 21); 
    filename.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE)); 


    Group group = new Group(group_1, SWT.NONE); 
    group.setBounds(81, 46, 245, 43); 

    Label lblEitherBrowseFor = new Label(group_1, SWT.NONE); 
    lblEitherBrowseFor.setBounds(10, 19, 430, 21); 
    lblEitherBrowseFor.setText("Either Browse for a file or Try to upload the time table data through Manual entry"); 

    Button btnManual = new Button(group, SWT.NONE); 
    btnManual.setBounds(162, 10, 75, 25); 
    btnManual.addSelectionListener(new SelectionAdapter() { 
     @Override 
     public void widgetSelected(SelectionEvent e) { 

     } 
    }); 
    btnManual.setText("Manual"); 

    Button btnBrowser = new Button(group, SWT.NONE); 
    btnBrowser.setBounds(27, 10, 75, 25); 
    btnBrowser.setText("Browse"); 
    btnBrowser.addSelectionListener(new SelectionAdapter() { 
     @Override 
     public void widgetSelected(SelectionEvent e) { 

      FileDialog dialog = new FileDialog(composite, SWT.NULL); 
      String path = dialog.open(); 
      if (path != null) { 

       File file = new File(path); 
       if (file.isFile()) 
       { 
        displayFiles(new String[] { file.getAbsolutePath().toString()}); 
        DemoShell.parseFile(table, file.toString()); 
       } 

       else 
       displayFiles(file.list()); 

      } 
     } 
    }); 
    ttable_item.setControl(composite); 

} 

@Override 
protected void checkSubclass() { 
    // Disable the check that prevents subclassing of SWT components 
} 

}

试图以上述方式增加时,我收到以下错误。

Exception in thread "main" java.lang.Error: Unresolved compilation problems: The constructor FileDialog(Composite, int) is undefined Cannot refer to a non-final variable composite inside an inner class defined in a different method

我请求建议/帮助清除错误或以任何其他方式重新执行。

+0

我正在更新我的完整代码。也许有一个主要缺陷。 – 2013-02-22 08:35:41

+0

我的代码有缺陷。错误解决后,我明白我需要一个布局,并且应该将组件绑定到shell/layout以便可见。现在它完美的工作。 – 2013-02-26 06:23:58

回答

2

您正在尝试从您定义的ListenerSelectionAdapter)内访问composite。您用于创建侦听器的符号隐式创建一个新类。因此,您正在有效地尝试从内部类访问变量composite,只有在composite是外部类的static字段或者如果您制作了compositefinal时才能完成。

因此,只要做到以下几点:

final Composite composite = new Composite(tabFolder, SWT.NONE); 

,它会工作。


此外,由于错误状态,没有构造函数需要Composite。你必须使用Shell。因此,只要做到以下几点:

FileDialog dialog = new FileDialog(composite.getShell(), SWT.NULL); 

然而,这会给你一个例外,因为你使用字段shell某处上面一行是null。由于您的课程是Shell,请将所有出现shell的地方替换为this,它将起作用。