作者:灬暗里着迷Dreamland | 来源:互联网 | 2023-10-11 10:05
表格
在表格中显示所有信息,包括在表格中显示照片的操作
/** * 将书籍信息显示在表格中 * @param list */
private void showTable(List<Map<String,Object>> list){
TableItem item = null;
//清空表格中原有数据
table.removeAll();
if(list==null||list.isEmpty()){
MessageDialog.openConfirm(shell, "提示", "查无书籍信息");
}
for(Map<String,Object> m :list){
item = new TableItem(table, SWT.NONE);
item.setText(new String[]{
//全都转成字符串
String.valueOf(m.get("ISBN")),
String.valueOf(m.get("B_NAME")),
String.valueOf(m.get("T_NAME")),
String.valueOf(m.get("B_AUTHOR")),
String.valueOf(m.get("B_COMPANY")),
String.valueOf(m.get("B_DATE")),
String.valueOf(m.get("B_PRICE")),
});
//图片在表格中显示
TableEditor editor = new TableEditor(table);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
editor.minimumWidth = 50;
editor.minimumHeight = 50;
Image image = null;
if(null==m.get("B_IAMGE")){
image = SWTResourceManager.getImage(Dialog_Book_Update.class,"/image/zanwu.png");
}else{
byte []bt = (byte[]) m.get("B_IAMGE");
ByteArrayInputStream in = new ByteArrayInputStream(bt);
image = new Image(Display.getCurrent(), in);
}
item.setImage(7,YcUtil.resize(image, 50, 50));
//编辑器重新加载item
editor.setItem(item);
}
}
/** * 指定高度和宽度对图片自适应 * @param src 原图片 * @param width 目标图片宽度 * @param height 目标图片高度 * @return */
public static Image resize(Image src,int width,int height){
Image image = new Image(Display.getDefault(), width,height);
GC gc = new GC(image);
try{
//打开高级绘图模式
gc.setAdvanced(true);
//设置消除锯齿
gc.setAntialias(SWT.ON);
gc.setInterpolation(SWT.HIGH);
gc.drawImage(src, 0, 0,src.getBounds().width,src.getBounds().height,0,0,width,height);
}finally{
gc.dispose();
}
return image;
}
点击表格中某一行后,在对应文本框显示
在table添加点击事件
table.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
TableItem item= (TableItem) e.item;
text_isbn1.setText(item.getText(0));
text_name1.setText(item.getText(1));
text_author1.setText(item.getText(3));
text_company1.setText(item.getText(4));
text_date1.setText(item.getText(5));
text_price1.setText(item.getText(6));
Image image = item.getImage(7);
YcUtil.imageSize(label_image);
}
});