使用SWT制作一个简单的Table(新手专用)

使用SWT制作一个简单的Table

  1. 首先使用swt制作这样一个界面
    在这里插入图片描述
    它的一些属性是这样的
    在这里插入图片描述
    这个时候我们的eclipse会自动生成以下代码:
package homework;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;

import java.util.Set;

import org.apache.ibatis.session.SqlSession;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import cn.edu.aust.mybatis1.entity.Student;
import cn.edu.aust.mybatis1.mapper.StudentMapper;
import cn.edu.aust.mybatis1.util.MyBatisUtil;

import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class TableWin1 {

	protected Shell shell;
	private Table table;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			TableWin1 window = new TableWin1();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(865, 501);
		shell.setText("SWT Application");

		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(54, 67, 675, 312);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("\u7F16\u53F7");

		TableColumn oneColumn = new TableColumn(table, SWT.CENTER);
		oneColumn.setWidth(139);
		oneColumn.setText("\u5B66\u751Fid");

		TableColumn twoColumn = new TableColumn(table, SWT.CENTER);
		twoColumn.setWidth(131);
		twoColumn.setText("\u5B66\u751F\u59D3\u540D");

		TableColumn threeColumn = new TableColumn(table, SWT.CENTER);
		threeColumn.setWidth(154);
		threeColumn.setText("\u5B66\u751F\u5B66\u53F7");

		TableColumn fourColumn = new TableColumn(table, SWT.CENTER);
		fourColumn.setWidth(134);
		fourColumn.setText("\u5B66\u751F\u73ED\u7EA7");
		
		Button button = new Button(shell, SWT.NONE);
		button.setBounds(680, 30, 98, 30);
		button.setText("\u65B0\u751F\u4FE1\u606F\u6CE8\u518C");
		TableItem tableItem = new TableItem(table, SWT.NONE);

		}

	}
}

这里只是提供一个大致思路,就是如何把我们数据库当中的数据写到table中!


swt中的table数据写入是需要使用String[]数组进行写入的.如图:
在这里插入图片描述
图片中的一行数据就是一个String[]数组。我事先将每一列的数据都存放到各自的String[]数组中,在最后就可以一次性汇总了。

// 获得String[]的sno数组集合
		int len = set.size();
		String[] sno = new String[len];
		int i = 0;
		for (Student s : set) {	//这里的for循环就相当于数据库中一列有几个数据,
		//然后将这些数据转换为String类型,在存入数组中。
			sno[i] = s.getSno();
			i++;
		}
		
		
		// 获得String[]的sname数组集合
		String[] sname = new String[len];
		i = 0;
		for (Student s : set) {
			sname[i] = s.getName();
			i++;
		}
		
		//获得String[]的class班级的数组集合
		String[] sclass =  new String[len];
		i=0;
		for(Student s:set) {
			sclass[i] = s.getClazz().getName();
			i++;
		}
		//获得String[]的sid数组集合
		String[] sid =  new String[len];
		i=0;
		for(Student s:set) {
			sid[i] =String.valueOf(s.getId());
			i++;
		}

String[]数组都准备好以后,就要把这些数据写进table了,将一行数据写入table的函数是这个

tableItem.setText(new String[]{"这里是String类型","这里是String类型","这里是String类型"}); 
//有几列就要写几个String类型

将数据写入table,如下方代码所示:

	for ( i = 0; i < len; i++) {
			TableItem tableItem = new TableItem(table, SWT.NONE);	//每循环一次,就新建一行
			tableItem.setText(new String[]{String.valueOf(i+1),sid[i],sname[i],sno[i],sclass[i]}); 
			//因为我这个一共有五列,第一列是编号,不是从数据库中拿出来的,直接使用String.valueOf()函数将int类型转换为String写入table
			//剩下的四列就是从数据库中读出的数据写入四个不同的String数组中的值了,直接写入就可以了。
		}

因为我这个当中用了一些mybatis的知识,要是全部的代码都贴出来就太多了,各种包,烦不胜烦,所以这里就不展示了,只需要看个思路就可以了,知道最简单的如何将数据写入table表就可以了。要是想直接粘贴复制代码,那可能就行不通喽!

接下来是源码:

package homework;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;

import java.util.Set;

import org.apache.ibatis.session.SqlSession;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import cn.edu.aust.mybatis1.entity.Student;
import cn.edu.aust.mybatis1.mapper.StudentMapper;
import cn.edu.aust.mybatis1.util.MyBatisUtil;

import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class TableWin1 {

	protected Shell shell;
	private Table table;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			TableWin1 window = new TableWin1();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(865, 501);
		shell.setText("SWT Application");

		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(54, 67, 675, 312);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("\u7F16\u53F7");

		TableColumn oneColumn = new TableColumn(table, SWT.CENTER);
		oneColumn.setWidth(139);
		oneColumn.setText("\u5B66\u751Fid");

		TableColumn twoColumn = new TableColumn(table, SWT.CENTER);
		twoColumn.setWidth(131);
		twoColumn.setText("\u5B66\u751F\u59D3\u540D");

		TableColumn threeColumn = new TableColumn(table, SWT.CENTER);
		threeColumn.setWidth(154);
		threeColumn.setText("\u5B66\u751F\u5B66\u53F7");

		TableColumn fourColumn = new TableColumn(table, SWT.CENTER);
		fourColumn.setWidth(134);
		fourColumn.setText("\u5B66\u751F\u73ED\u7EA7");
		
		Button button = new Button(shell, SWT.NONE);
		button.setBounds(680, 30, 98, 30);
		button.setText("\u65B0\u751F\u4FE1\u606F\u6CE8\u518C");

		SqlSession session = MyBatisUtil.getSession();
		StudentMapper mapper = session.getMapper(StudentMapper.class);
		Student condition = new Student();
		Set<Student> set = mapper.selectStudentsAllInformation(condition);
		// 获得String[]的sno数组集合
		int len = set.size();
		String[] sno = new String[len];
		int i = 0;
		for (Student s : set) {
			sno[i] = s.getSno();
			i++;
		}
		
		// 获得String[]的sname数组集合
		String[] sname = new String[len];
		i = 0;
		for (Student s : set) {
			sname[i] = s.getName();
			i++;
		}
		
		//获得String[]的class班级的数组集合
		String[] sclass =  new String[len];
		i=0;
		for(Student s:set) {
			sclass[i] = s.getClazz().getName();
			i++;
		}
		//获得String[]的sid数组集合
		String[] sid =  new String[len];
		i=0;
		for(Student s:set) {
			sid[i] =String.valueOf(s.getId());
			i++;
		}
		
		for ( i = 0; i < len; i++) {
			TableItem tableItem = new TableItem(table, SWT.NONE);	
			tableItem.setText(new String[]{String.valueOf(i+1),sid[i],sname[i],sno[i],sclass[i]}); 
		}

	}
}


记录:这是刚学的SWT的一些知识,记录一下。

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值