SWT(JFace)体验之ImageRegistry时间:2022-11-11 15:48:43测试代码如下: package swt_jface.demo; import java.io.File; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.resource.ImageRegistry; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.DirectoryDialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableItem; public class SimpleFileBrowser { Display display = new Display(); Shell shell = new Shell(display); ImageRegistry imageRegistry; Table table; public SimpleFileBrowser() { init(); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } private void init() { shell.setText("File Browser"); shell.setLayout(new GridLayout(1, true)); Button button = new Button(shell, SWT.PUSH); button.setText("Browse ..."); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { DirectoryDialog dialog = new DirectoryDialog(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()); } } }); GridData gd = new GridData(GridData.FILL_BOTH); table = new Table(shell, SWT.MULTI); table.setLayoutData(gd); imageRegistry = new ImageRegistry(); ImageDescriptor defaultIcon = ImageDescriptor.createFromFile(null, "img/default.gif"); imageRegistry.put("default", defaultIcon); ImageDescriptor jarIcon = ImageDescriptor.createFromFile(null, "img/jar.gif"); imageRegistry.put("jar", jarIcon); } public void displayFiles(String[] files) { table.removeAll(); for (int i = 0; files != null && i < files.length; i++) { TableItem item = new TableItem(table, SWT.NULL); Image image = null; if (files[i].endsWith(".jar")) { image = imageRegistry.get("jar"); } else { image = imageRegistry.get("default"); } item.setImage(image); item.setText(files[i]); } } public void displayFiles2(String[] files) { TableItem[] items = table.getItems(); for(int i=0; items != null && i < items.length; i++) { if(items[i].getImage() != null) items[i].getImage().dispose(); } table.removeAll(); for (int i = 0; files != null && i < files.length; i++) { TableItem item = new TableItem(table, SWT.NULL); Image image = null; if (files[i].endsWith(".jar")) { image = new Image(display, "img/jar.gif"); } else { image = new Image(display, "img/default.gif"); } item.setImage(image); item.setText(files[i]); } } public static void main(String[] args) { new SimpleFileBrowser(); } }