Results 1 to 1 of 1
Thread: Dir File Selection
-
Dir File Selection
Java Code:import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.DirectoryDialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class DirFileSelection { Display display = new Display(); Shell shell = new Shell(display); // the label used to display selected dir/file. Label label; Button buttonSelectDir; Button buttonSelectFile; String selectedDir; String fileFilterPath = "F:/jdk1.5"; public DirFileSelection() { label = new Label(shell, SWT.BORDER | SWT.WRAP); label.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); label.setText("Select a dir/file by clicking the buttons below."); buttonSelectDir = new Button(shell, SWT.PUSH); buttonSelectDir.setText("Select a directory"); buttonSelectDir.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { DirectoryDialog directoryDialog = new DirectoryDialog(shell); directoryDialog.setFilterPath(selectedDir); directoryDialog.setMessage("Please select a directory and click OK"); String dir = directoryDialog.open(); if(dir != null) { label.setText("Selected dir: " + dir); selectedDir = dir; } } }); buttonSelectFile = new Button(shell, SWT.PUSH); buttonSelectFile.setText("Select a file/multiple files"); buttonSelectFile.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { FileDialog fileDialog = new FileDialog(shell, SWT.MULTI); fileDialog.setFilterPath(fileFilterPath); fileDialog.setFilterExtensions(new String[]{"*.rtf", "*.html", "*.*"}); fileDialog.setFilterNames(new String[]{ "Rich Text Format", "HTML Document", "Any"}); String firstFile = fileDialog.open(); if(firstFile != null) { fileFilterPath = fileDialog.getFilterPath(); String[] selectedFiles = fileDialog.getFileNames(); StringBuffer sb = new StringBuffer("Selected files under dir " + fileDialog.getFilterPath() + ": \n"); for(int i=0; i<selectedFiles.length; i++) { sb.append(selectedFiles[i] + "\n"); } label.setText(sb.toString()); } } }); label.setBounds(0, 0, 400, 60); buttonSelectDir.setBounds(0, 65, 200, 30); buttonSelectFile.setBounds(200, 65, 200, 30); shell.pack(); shell.open(); //textUser.forceFocus(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); } public static void main(String[] args) { new DirFileSelection(); } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
MySQL connector selection
By Ms.Ranjan in forum JDBCReplies: 3Last Post: 06-23-2008, 05:27 PM -
Node selection in jtree
By Preethi in forum AWT / SwingReplies: 4Last Post: 06-19-2008, 11:25 PM -
Selection sort in Java
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 07:41 PM -
radiobutton selection
By newtojava7 in forum New To JavaReplies: 1Last Post: 03-17-2008, 02:58 AM -
Code for selection
By kneekow in forum EclipseReplies: 0Last Post: 02-01-2008, 03:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks