首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

SWT/JFACE 中怎样把数据库中的表显示在table中,该怎么解决

2012-02-03 
SWT/JFACE 中怎样把数据库中的表显示在table中最近在用Visual Editor设计一个基于SWT 商品进销存的桌面应

SWT/JFACE 中怎样把数据库中的表显示在table中
最近在用Visual Editor设计一个基于SWT 商品进销存的桌面应用程序的毕业设计
数据库的连接,查询结果新界面显示已经解决
有几个像商品信息表,顾客信息表之类的数据库基本表需要在界面中显示出来
计划在swt controls下的table中显示
相应的有添加,修改,删除功能,然后在table中反应出来,进而修改数据库
可小弟水平有限,上网找了一下资料 也都是jtable显示数据库的教程和例子

不知道有没有高手有类似经验 swt下的table能不能达到我想要的效果
用什么方法可以?

希望能得到指导 不胜感激

[解决办法]
让单元格可编辑,然后要么做个提交按钮来统一修改数据,要么直接监听数据更新事件,只要一更新数据就提交数据库,不过这样的话跟数据库交流太过频繁,所以还是建议第一种方式。
[解决办法]
table的编辑还要自己实现,建议使用tableviewer,你的操作基本都支持,要好用的多。
和数据库的交互只要你在相应的事件中实现即可。
[解决办法]
使用tableviewer实现很简单的,在contentProvider中加入你的模型object到row的映射,使用labelProvider加入样式。。。
[解决办法]
tableView
VisitClockView:

Java code
public class VisitClockView extends ViewPart { 

private TableViewer tableViewer;

private Shell shell;

private Action reserveAction,rebesparkAction,clockAction,outAction,editAction,refreshAction,rightAction,deleteAction;

public static final int COMPANYNAME = 0;

public static final int VISITORNAME = 1;

public static final int VISITORCOUNT = 2;

public static final int BOOKTIME = 3;

public static final int NEEDTOSEE = 4;

public static final int VISITISSUE = 5;

public static final int BOOKBY = 6;

public static final int BOOKINGTIME = 7;

public static final int SHOWUPTIME = 8;

public static final int DEPOSITIDTYPE = 9;

public static final String ID = "com.dsrcom.ecq.dsreli_client.brucevisitmanager.views.VisitClockView"; //$NON-NLS-1$

/**
* Create contents of the view part
* @param parent
*/
@Override
public void createPartControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new FillLayout());
  tableViewer = new TableViewer(container,SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
/**
* 通过TableViewer中的Table对其布局
*/
Table table = tableViewer.getTable();
table.setHeaderVisible(true);                  //显示表头
table.setLinesVisible(true);                    //显示表格线
TableLayout tLayout = new TableLayout();
table.setLayout(tLayout);
/**
* 建立TableViewer中的列
*/
tLayout.addColumnData(new ColumnWeightData(40));
TableColumn col0 = new TableColumn(table,SWT.NONE);
tableViewer.setSorter(new MyTableSort());
col0.setText("公司名称");
col0.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
((MyTableSort) tableViewer.getSorter()).doSort(COMPANYNAME);
tableViewer.refresh();
}
});
tLayout.addColumnData(new ColumnWeightData(40));
TableColumn col1 = new TableColumn(table,SWT.NONE);
col1.setText("访客");
col1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
((MyTableSort) tableViewer.getSorter()).doSort(VISITORNAME);
tableViewer.refresh();
}
});
tLayout.addColumnData(new ColumnWeightData(20));
TableColumn col2 = new TableColumn(table,SWT.NONE);
col2.setText("来访人数");
col2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
((MyTableSort) tableViewer.getSorter()).doSort(VISITORCOUNT);


tableViewer.refresh();
}
});
tLayout.addColumnData(new ColumnWeightData(50));
TableColumn col3 = new TableColumn(table,SWT.NONE);
col3.setText("预约会见时间");
col3.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
((MyTableSort) tableViewer.getSorter()).doSort(BOOKTIME);
  tableViewer.refresh();
}

});
tLayout.addColumnData(new ColumnWeightData(20));
TableColumn col4 = new TableColumn(table,SWT.NONE);
col4.setText("来访对象");
col4.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
((MyTableSort) tableViewer.getSorter()).doSort(NEEDTOSEE);
tableViewer.refresh();
}
});
tLayout.addColumnData(new ColumnWeightData(40));
TableColumn col5 = new TableColumn(table,SWT.NONE);
col5.setText("来访事由");
col5.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
((MyTableSort) tableViewer.getSorter()).doSort(VISITISSUE);
tableViewer.refresh();
}
});
tLayout.addColumnData(new ColumnWeightData(20));
TableColumn col6 = new TableColumn(table,SWT.NONE);
col6.setText("预约人");
col6.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
((MyTableSort) tableViewer.getSorter()).doSort(BOOKBY);
tableViewer.refresh();
}
});
tLayout.addColumnData(new ColumnWeightData(40));
TableColumn col7 = new TableColumn(table,SWT.NONE);
col7.setText("输入时间");
col7.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
((MyTableSort) tableViewer.getSorter()).doSort(BOOKINGTIME);
tableViewer.refresh();
}
});
tLayout.addColumnData(new ColumnWeightData(40));
TableColumn col8 = new TableColumn(table,SWT.NONE);
col8.setText("到达时间");
col8.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
((MyTableSort) tableViewer.getSorter()).doSort(SHOWUPTIME);
tableViewer.refresh();
}
});
tLayout.addColumnData(new ColumnWeightData(50));
TableColumn col9 = new TableColumn(table,SWT.NONE);
col9.setText("访客暂存证件");
col9.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
((MyTableSort) tableViewer.getSorter()).doSort(DEPOSITIDTYPE);
tableViewer.refresh();
}
});
tableViewer.setContentProvider(new TableViewerContentProvider());  //内容器
tableViewer.setLabelProvider(new TableViewerLableProvider());    //标签器
//List inputObj = VisitInformationFactory.getInformations();
//tableViewer.setInput(inputObj);
/**
*
* 创建右键菜单和工具栏
*/
createActions();
hookContextMenu();
contributeToActionBars();
hookClickAction();
init();
}
}



标签器: TableViewerLableProvider

Java code
public class TableViewerLableProvider implements ITableLabelProvider { 

public void dispose() {
// TODO 自动生成方法存根

}

public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
// TODO 自动生成方法存根

}

public Image getColumnImage(Object element, int columnIndex) {
// TODO 自动生成方法存根
return null;
}

public String getColumnText(Object element, int columnIndex) {
VisitInformation visitInformation = (VisitInformation) element;


if (columnIndex == 0) {
if(visitInformation.getComanyName() == null) {
return "";
} else {
return visitInformation.getComanyName();        //公司名称
}
}
if (columnIndex == 1) {
return visitInformation.getVisitorName();        //访客对象
}
if (columnIndex == 2) {
return visitInformation.getVisitorCount().toString();  //来访人数
}
if (columnIndex == 3) {
/**
* DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
format.format(date);

textInputTime.setText(format.format(date).toString());
*/
DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return format.format(visitInformation.getBookTime()).toString();
//return visitInformation.getBookTime().toString();    //预约会见时间
}
if (columnIndex == 4) {
return visitInformation.getNeedToSee();        //来访对象
}
if (columnIndex == 5) {
return visitInformation.getVisitIssue();          //来访事由
}
if (columnIndex == 6) {
return visitInformation.getBookBy();          //预约人
}
if (columnIndex == 7) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return format.format(visitInformation.getBookingTime()).toString();  //输入时间
}
if (columnIndex == 8) {
if(visitInformation.getShowUpTime() == null) {
return null;
} else {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return format.format(visitInformation.getShowUpTime()).toString();    //到达时间
}
}
if (columnIndex == 9) {
if(visitInformation.getDepositIDType() == null) {
return "";
} else {
return visitInformation.getDepositIDType();    //访客暂存证件
}     
}
return "";
}

public void addListener(ILabelProviderListener listener) {

}

public boolean isLabelProperty(Object element, String property) {
return false;
}

public void removeListener(ILabelProviderListener listener) {

}
}

热点排行