Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

SWT File Browser

In SWT, the class FileDialog allow the user to navigate the file system and select a file name. In order to layout the text, we have used the method text.setLayoutData(data).

Ads

SWT File Browser

     

This section illustrates you how to browse a file.

In SWT, the class FileDialog allow the user to navigate the file system and select a file name. In order to layout the text, we have used the method text.setLayoutData(data). The method file.isFile() tests whether the file denoted by the pathname is a normal file. The method file.list() returns an array of files and directories. The method text.setText(files[i]) sets the selected file in the text. The method text.setEditable(true) sets the editable state.

 

 

 

Here is the code of FileBrowser.java

import java.io.File;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class FileBrowser {
  Display display = new Display();
  Shell shell = new Shell(display);
  Text text;

  public FileBrowser() {
  init();
  shell.pack();
  shell.setSize(350,60);
  shell.open();
 
  while (!shell.isDisposed()) {
  if (!display.readAndDispatch()) {
  display.sleep();
  }
  }
  display.dispose();
  }
  private void init() {
  shell.setText("File Browser");
  shell.setLayout(new GridLayout(2, true));
  GridData data = new GridData(GridData.FILL_BOTH);

  text = new Text(shell, SWT.NONE);
  text.setLayoutData(data);
  
  Button button = new Button(shell, SWT.PUSH);
  button.setText("Browse");
  button.addSelectionListener(new SelectionAdapter() {
  public void widgetSelected(SelectionEvent e) {
  FileDialog dialog = new FileDialog(shell, SWT.NULL);
  String path = dialog.open();
  if (path != null) {

  File file = new File(path);
  if (file.isFile())
  displayFiles(new String[] { file.toString()});
  else
  displayFiles(file.list());

  }
  }
  });  
  }
 public void displayFiles(String[] files) {
  for (int i = 0; files != null && i < files.length; i++) {
  text.setText(files[i]);
  text.setEditable(true);
  }
}
 public static void main(String[] args) {
  new FileBrowser();
  }
}


Output will be displayed as:

 

On clicking the browse button, file dialog will open:

 

After clicking the open button, output will be displayed as:

 

Download Source code

     

Advertisements

Ads
Share on Google+Share on Google+

SWT File Browser

Posted on: October 24, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: SWT File Browser   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Sw
November 18, 2011
FileDialog can let you select files only

A fileDialog will not return unless you select file/files. If you select a directory and click Open,then it just takes you inside the directory. Hence the code "else displayFiles(file.list()); " is Unreachable in above example

Ads

 

Ads