`

Swing表格在SWT应用

 
阅读更多

Snippet135的例子:

public class Snippet135 {

	static class FileTableModel extends AbstractTableModel {		
		File[] files;        
		String[] columnsName = {"Name", "Size", "Date Modified"};
		
		public FileTableModel (File[] files) {
			this.files = files;
		}
		public int getColumnCount () {
			return columnsName.length;
		}
		public Class getColumnClass (int col) {
			if (col == 1) return Long.class;
			if (col == 2) return Date.class;
			return String.class;
		}
		public int getRowCount () {
			return files == null ? 0 : files.length;
		}
		public Object getValueAt (int row, int col) {
			if (col == 0) return files[row].getName();
			if (col == 1) return new Long(files[row].length());
			if (col == 2) return new Date(files[row].lastModified());
			return "";
		}
		public String getColumnName (int col) {
			return columnsName[col];
		}
	}

	public static void main(String[] args) {
		final Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setText("SWT and Swing/AWT Example");

		Listener exitListener = new Listener() {
			public void handleEvent(Event e) {
				MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION);
				dialog.setText("Question");
				dialog.setMessage("Exit?");
				if (e.type == SWT.Close) e.doit = false;
				if (dialog.open() != SWT.OK) return;
				shell.dispose();
			}
		};	
		Listener aboutListener = new Listener() {
			public void handleEvent(Event e) {
				final Shell s = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
				s.setText("About");
				GridLayout layout = new GridLayout(1, false);
				layout.verticalSpacing = 20;
				layout.marginHeight = layout.marginWidth = 10;
				s.setLayout(layout);
				Label label = new Label(s, SWT.NONE);
				label.setText("SWT and AWT Example.");
				Button button = new Button(s, SWT.PUSH);
				button.setText("OK");
				GridData data = new GridData();
				data.horizontalAlignment = GridData.CENTER;
				button.setLayoutData(data);
				button.addListener(SWT.Selection, new Listener() {
					public void handleEvent(Event event) {
						s.dispose();
					}
				});
				s.pack();
				Rectangle parentBounds = shell.getBounds();
				Rectangle bounds = s.getBounds();
				int x = parentBounds.x + (parentBounds.width - bounds.width) / 2;
				int y = parentBounds.y + (parentBounds.height - bounds.height) / 2;
				s.setLocation(x, y);
				s.open();
				while (!s.isDisposed()) {
					if (!display.readAndDispatch()) display.sleep();
				}
			}
		};
		shell.addListener(SWT.Close, exitListener);
		Menu mb = new Menu(shell, SWT.BAR);
		MenuItem fileItem = new MenuItem(mb, SWT.CASCADE);
		fileItem.setText("&File");
		Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
		fileItem.setMenu(fileMenu);
		MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);
		exitItem.setText("&Exit\tCtrl+X");
		exitItem.setAccelerator(SWT.CONTROL + 'X');
		exitItem.addListener(SWT.Selection, exitListener);
		MenuItem aboutItem = new MenuItem(fileMenu, SWT.PUSH);
		aboutItem.setText("&About\tCtrl+A");
		aboutItem.setAccelerator(SWT.CONTROL + 'A');
		aboutItem.addListener(SWT.Selection, aboutListener);
		shell.setMenuBar(mb);

		RGB color = shell.getBackground().getRGB();
		Label separator1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		Label locationLb = new Label(shell, SWT.NONE);
		locationLb.setText("Location:");
		Composite locationComp = new Composite(shell, SWT.EMBEDDED);
		ToolBar toolBar = new ToolBar(shell, SWT.FLAT);
		ToolItem exitToolItem = new ToolItem(toolBar, SWT.PUSH);
		exitToolItem.setText("&Exit");
		exitToolItem.addListener(SWT.Selection, exitListener);
		ToolItem aboutToolItem = new ToolItem(toolBar, SWT.PUSH);
		aboutToolItem.setText("&About");
		aboutToolItem.addListener(SWT.Selection, aboutListener);
		Label separator2 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		final Composite comp = new Composite(shell, SWT.NONE);
		final Tree fileTree = new Tree(comp, SWT.SINGLE | SWT.BORDER);
		Sash sash = new Sash(comp, SWT.VERTICAL);
		Composite tableComp = new Composite(comp, SWT.EMBEDDED);
		Label separator3 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		Composite statusComp = new Composite(shell, SWT.EMBEDDED);

		java.awt.Frame locationFrame = SWT_AWT.new_Frame(locationComp);
		final java.awt.TextField locationText = new java.awt.TextField();
		locationFrame.add(locationText);

		java.awt.Frame fileTableFrame = SWT_AWT.new_Frame(tableComp);
		java.awt.Panel panel = new java.awt.Panel(new java.awt.BorderLayout());
		fileTableFrame.add(panel);
		final JTable fileTable = new JTable(new FileTableModel(null));
		fileTable.setDoubleBuffered(true);
		fileTable.setShowGrid(false);
		fileTable.createDefaultColumnsFromModel();
		JScrollPane scrollPane = new JScrollPane(fileTable);
		panel.add(scrollPane);

		java.awt.Frame statusFrame = SWT_AWT.new_Frame(statusComp);
		statusFrame.setBackground(new java.awt.Color(color.red, color.green, color.blue));
		final java.awt.Label statusLabel = new java.awt.Label();
		statusFrame.add(statusLabel);
		statusLabel.setText("Select a file");

		sash.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				if (e.detail == SWT.DRAG) return;
				GridData data = (GridData)fileTree.getLayoutData();
				Rectangle trim = fileTree.computeTrim(0, 0, 0, 0);
				data.widthHint = e.x - trim.width;
				comp.layout();
			}
		});

		File[] roots = File.listRoots();
		for (int i = 0; i < roots.length; i++) {
			File file = roots[i];
			TreeItem treeItem = new TreeItem(fileTree, SWT.NONE);
			treeItem.setText(file.getAbsolutePath());
			treeItem.setData(file);
			new TreeItem(treeItem, SWT.NONE);
		}
		fileTree.addListener(SWT.Expand, new Listener() {
			public void handleEvent(Event e) {
				TreeItem item = (TreeItem)e.item;
				if (item == null) return;
				if (item.getItemCount() == 1) {
					TreeItem firstItem = item.getItems()[0];
					if (firstItem.getData() != null) return;
					firstItem.dispose();
				} else {
					return;
				}
				File root = (File)item.getData();
				File[] files = root.listFiles();
				if (files == null) return;
				for (int i = 0; i < files.length; i++) {
					File file = files[i];
					if (file.isDirectory()) {
						TreeItem treeItem = new TreeItem(item, SWT.NONE);
						treeItem.setText(file.getName());
						treeItem.setData(file);
						new TreeItem(treeItem, SWT.NONE);
					}
				}
			}
		});
		fileTree.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				TreeItem item = (TreeItem)e.item;
				if (item == null) return;
				final File root = (File)item.getData();
				EventQueue.invokeLater(new Runnable() {
					public void run() {
						statusLabel.setText(root.getAbsolutePath());
						locationText.setText(root.getAbsolutePath());
						fileTable.setModel(new FileTableModel(root.listFiles()));
					}
				});
			}
		});
		
		GridLayout layout = new GridLayout(4, false);
		layout.marginWidth = layout.marginHeight = 0;
		layout.horizontalSpacing = layout.verticalSpacing = 1;
		shell.setLayout(layout);
		GridData data;		
		data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 4;
		separator1.setLayoutData(data);
		data = new GridData();
		data.horizontalSpan = 1;
		data.horizontalIndent = 10;
		locationLb.setLayoutData(data);
		data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 2;
		data.heightHint = locationText.getPreferredSize().height;
		locationComp.setLayoutData(data);
		data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 1;
		toolBar.setLayoutData(data);
		data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 4;
		separator2.setLayoutData(data);
		data = new GridData(GridData.FILL_BOTH);
		data.horizontalSpan = 4;
		comp.setLayoutData(data);
		data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 4;
		separator3.setLayoutData(data);
		data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 4;
		data.heightHint = statusLabel.getPreferredSize().height;
		statusComp.setLayoutData(data);
		
		layout = new GridLayout(3, false);
		layout.marginWidth = layout.marginHeight = 0;
		layout.horizontalSpacing = layout.verticalSpacing = 1;
		comp.setLayout(layout);			
		data = new GridData(GridData.FILL_VERTICAL);
		data.widthHint = 200;
		fileTree.setLayoutData(data);		
		data = new GridData(GridData.FILL_VERTICAL);
		sash.setLayoutData(data);		
		data = new GridData(GridData.FILL_BOTH);
		tableComp.setLayoutData(data);

		shell.open();
		while(!shell.isDisposed()) {
			if (!display.readAndDispatch()) display.sleep();
		}
		display.dispose();
	}
}

 

 

感觉效果JTable的效果还不错。

 



 

  • 大小: 79 KB
分享到:
评论

相关推荐

    Java Swing to SWT.pdf

    - **教程简介**:本教程旨在帮助开发者了解如何将基于 Java Swing 的应用程序迁移到 SWT(Standard Widget Toolkit)。自2001年首次发布并捐赠给开源社区以来,Eclipse 平台在工具提供商社区中持续获得重要地位。...

    java Swing、SWT分页

    Java Swing和 SWT 是两种在Java中创建图形用户界面(GUI)的库,它们都提供了丰富的组件和工具来构建桌面应用程序。在这篇文章中,我们将深入探讨Java Swing中的分页技术和SWT(Standard Widget Toolkit)的使用,...

    Eclipse Swing Swt builder 插件安装包及安装说明

    - **广泛的组件库**:支持Swing和SWT的所有组件,包括按钮、文本框、表格、树视图等,以及自定义组件。 总的来说,Eclipse Swing SWT Builder插件是Java GUI开发的强大工具,它简化了UI设计,提高了开发效率,是...

    eclipse开发SWT应用

    在Eclipse中开发SWT应用,首先需要了解SWT提供的基本组件,如按钮(Button)、文本框(Text)、列表(List)、表格(Table)等,这些组件可以构建出丰富的用户交互界面。SWT的优势在于其与操作系统底层的紧密集成,...

    如何创建简单的SWT 应用程序.pdf清晰版

    ### 如何创建简单的SWT应用程序 #### SWT与JFace简介 SWT(Standard Widget Toolkit)与JFace是构建Java图形用户界面(GUI)的强大工具,它们最初由IBM开发,广泛应用于Eclipse平台及其周边项目中。SWT提供了一组...

    DJNativeSwing-SWT.jar、DJNativeSwing.jar、swt-win-x64.jar

    DJNativeSwing.jar是DJProject的主要库,它包含了一系列用于构建混合Swing和SWT应用的类和接口。这个JAR文件提供了丰富的API,让开发者可以轻松地在Swing和SWT之间切换,创建出既美观又高效的用户界面。例如,它可以...

    Java图形界面开发-awt、swing、swt

    - **Swing**:Swing是建立在AWT基础上的一个更高级的图形界面工具包,它提供了一系列用于构建桌面应用程序的组件。Swing的优势在于跨平台性更强,且提供了更多的定制选项和外观控制。 - **SWT**:SWT是另一个用于...

    SWT应用的开发实例:没有使用到OSGi

    在这个"SWT应用的开发实例:没有使用到OSGi"中,我们将会探讨如何在不依赖OSGi(OSGi - Open Service Gateway Initiative)框架的情况下,利用SWT创建一个功能完备的应用程序。OSGi是一种模块化系统,常用于Java应用...

    SWT Swing与AWT区别

    因此,SWT的应用在某些情况下可以提供更高的性能和更好的外观。SWT与JFace结合使用时,可以提供类似于Swing的组件和布局管理功能,但其组件和API与Swing并不兼容。SWT的事件模型与AWT和Swing有所不同,需要开发者...

    swt-64.rar_64位swt_64的SWT_SWT64_swt.jar 64位_swt64位

    这种设计使得SWT应用程序在性能上通常优于Swing,并且可以更好地融入本地桌面环境,因为它们可以使用操作系统提供的原生控件。 64位版本的SWT是为了在64位Java运行时环境中运行而编译的,这意味着它可以充分利用64...

    The Java Foundation Classes (J.F.C.Swing) API and SWT (The Eclipse Standard Widget Toolkit).pdf

    - **Swing**和**SWT**都支持跨平台开发,但Swing在不同平台上的表现更加一致,而SWT则更倾向于提供本地化的体验。 #### 四、总结 JFC/Swing和SWT都是用于Java应用程序GUI开发的强大工具。选择哪一个取决于具体的...

    swt虚拟表格.rar

    在Java编程领域, SWT(Standard Widget Toolkit)是一个用于构建用户界面的开源库,它是Java AWT和Swing之外的一个选择,特别适用于需要原生外观和感觉的桌面应用程序。SWT提供了丰富的控件集,其中包括虚拟表格...

    Eclipse_Swt_Jface_核心应用

    Eclipse的SWT (Standard Widget Toolkit) 和JFace是Java GUI开发中的两个重要库,尤其在开发基于Eclipse平台的应用程序时,它们扮演着至关重要的角色。这些库提供了丰富的用户界面组件,使得开发者能够创建出功能...

    Eclipse SWT/JFace 核心应用的全部源代码

    首先,SWT是Eclipse提供的一个开源的GUI库,它与Java标准的AWT和Swing不同,SWT直接与操作系统API交互,因此在性能和外观上更接近原生应用程序。SWT提供了诸如按钮、文本框、列表、树等基本控件,以及对话框、菜单和...

    SWT界面设计PDF

    2. **壳体(Shell)**:壳体是SWT应用的基本窗口,所有控件都放置在壳体内。 3. **布局管理器(Layouts)**:布局管理器负责决定组件在壳体内的排列方式,如FillLayout、GridLayout、RowLayout等。 4. **事件处理**...

    eclipse swt 核心应用

    这些工具能够帮助开发者更有效地构建和优化SWT应用。 遗憾的是,由于提供的文件列表只有一个名为“新建文件夹”的项,没有具体的代码或文档,我们无法进一步了解博客的具体内容。通常,这样的文件可能包含示例代码...

    基于SWT和Hibernate的应用例子

    在SWT中,开发者可以创建各种窗口、按钮、表格等控件,构建出功能丰富的桌面应用程序界面。 Hibernate则是一个对象关系映射(ORM)框架,它简化了Java应用程序与数据库之间的交互。ORM允许开发者使用面向对象的方式...

    Eclipse SWT/JFace 核心应用光盘源码

    Eclipse SWT(Standard Widget Toolkit)和JFace是Java开发中用于构建图形用户界面(GUI)的库,尤其在开发Eclipse插件和RCP(Rich Client Platform)应用程序时非常重要。这两个库提供了丰富的组件和框架,使得...

    Java GUI SWT/Swing/AWT的介绍及比较

    1. 跨平台兼容性:如果应用需要在多种操作系统上运行,Swing可能是更好的选择,因为它的外观和行为一致性更好。 2. 性能:对于性能敏感的应用,SWT可能更合适,因为它使用本地系统资源。 3. 用户体验:如果希望应用...

Global site tag (gtag.js) - Google Analytics