jface中对话框返回数据给调用窗口
By:Roy.LiuLast updated:2009-08-27
最近学习JFACE,在做一个POC的过程中,要用弹出对话框,填写数据,在关闭时,将数据传给调用窗口,
如果是一般的程序,或者用SWT的SHELL来做,是很简单的事,但确实不懂JFACE. 没办法,下班后,自己琢磨了一阵,发现自己真的老了,用类最基本的方法就可以解决,居然没有反应过来,get ,set 方法呀。晕呀。在 TitleAreaDialog 对话框的 ok 按钮上 把要传递的值给赋上,用get 方法来取。 废话少说,看代码最好了。
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class ImageWaterMarkDlg extends TitleAreaDialog {
private Text waterTopText;
private Text waterLeftText;
private Text picPathText;
private String picPath;
private int waterLeft;
private int waterTop;
/**
* Create the dialog
* @param parentShell
*/
public ImageWaterMarkDlg(Shell parentShell) {
super(parentShell);
}
@Override
protected Control createContents(Composite parent) {
super.createContents(parent);
setMessage("选择图片路径和输入图片水印的坐标,都必须填写!");
setTitle("图片水印设置");
return parent;
}
/**
* Create contents of the dialog
* @param parent
*/
@Override
protected Control createDialogArea(Composite parent) {
Composite area = (Composite) super.createDialogArea(parent);
Composite container = new Composite(area, SWT.NONE);
container.setLayoutData(new GridData(GridData.FILL_BOTH));
final Label label = new Label(container, SWT.NONE);
label.setText("图片路径:");
label.setBounds(38, 30, 77, 12);
final Label label_1 = new Label(container, SWT.NONE);
label_1.setText("水印横坐标:");
label_1.setBounds(25, 64, 77, 12);
final Label label_2 = new Label(container, SWT.NONE);
label_2.setText("水印纵坐标:");
label_2.setBounds(26, 100, 72, 12);
picPathText = new Text(container, SWT.BORDER);
picPathText.setBounds(121, 22, 311, 22);
waterLeftText = new Text(container, SWT.BORDER);
waterLeftText.setBounds(121, 59, 311, 22);
waterTopText = new Text(container, SWT.BORDER);
waterTopText.setBounds(121, 95, 311, 22);
final Button selectPicbutton = new Button(container, SWT.NONE);
selectPicbutton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
FileDialog dlg = new FileDialog(getShell(), SWT.OPEN);
dlg.setFilterExtensions(new String[] {"*.bmp", "*.jpg", "*.tif", "*.*"});
String fileName = dlg.open();
if (fileName != null) {
picPathText.setText(fileName);
}
}
});
selectPicbutton.setText("...");
selectPicbutton.setBounds(435, 21, 30, 22);
final Label label_3 = new Label(container, SWT.NONE);
label_3.setText("(数字)");
label_3.setBounds(435, 64, 36, 12);
final Label label_4 = new Label(container, SWT.NONE);
label_4.setText("(数字)");
label_4.setBounds(435, 100, 33, 12);
//
return area;
}
/**
* Create contents of the button bar
* @param parent
*/
@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, "确定",
true);
createButton(parent, IDialogConstants.CANCEL_ID,
"取消", false);
}
/**
* Return the initial size of the dialog
*/
@Override
protected Point getInitialSize() {
return new Point(500, 293);
}
protected void buttonPressed(int buttonId) {
if (buttonId == IDialogConstants.OK_ID) {
picPath=picPathText.getText();
waterLeft=Integer.parseInt(waterLeftText.getText());
waterTop=Integer.parseInt(waterTopText.getText());
super.okPressed();
return;
}
super.buttonPressed(buttonId);
}
public String getPicPath() {
return picPath;
}
public int getWaterLeft() {
return waterLeft;
}
public int getWaterTop() {
return waterTop;
}
}
调用窗口的代码:
picWaterButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
ImageWaterMarkDlg imageWaterMarkDlg=new ImageWaterMarkDlg(getSite().getShell());
if (imageWaterMarkDlg.open()==Dialog.OK){
keyImage.AddImageWaterMark(imageWaterMarkDlg.getPicPath(),
imageWaterMarkDlg.getWaterLeft(), imageWaterMarkDlg.getWaterTop());
}
}
});
就这么简单,年纪大了,不服老都不行了,思维没有以前敏捷了。
如果是一般的程序,或者用SWT的SHELL来做,是很简单的事,但确实不懂JFACE. 没办法,下班后,自己琢磨了一阵,发现自己真的老了,用类最基本的方法就可以解决,居然没有反应过来,get ,set 方法呀。晕呀。在 TitleAreaDialog 对话框的 ok 按钮上 把要传递的值给赋上,用get 方法来取。 废话少说,看代码最好了。
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class ImageWaterMarkDlg extends TitleAreaDialog {
private Text waterTopText;
private Text waterLeftText;
private Text picPathText;
private String picPath;
private int waterLeft;
private int waterTop;
/**
* Create the dialog
* @param parentShell
*/
public ImageWaterMarkDlg(Shell parentShell) {
super(parentShell);
}
@Override
protected Control createContents(Composite parent) {
super.createContents(parent);
setMessage("选择图片路径和输入图片水印的坐标,都必须填写!");
setTitle("图片水印设置");
return parent;
}
/**
* Create contents of the dialog
* @param parent
*/
@Override
protected Control createDialogArea(Composite parent) {
Composite area = (Composite) super.createDialogArea(parent);
Composite container = new Composite(area, SWT.NONE);
container.setLayoutData(new GridData(GridData.FILL_BOTH));
final Label label = new Label(container, SWT.NONE);
label.setText("图片路径:");
label.setBounds(38, 30, 77, 12);
final Label label_1 = new Label(container, SWT.NONE);
label_1.setText("水印横坐标:");
label_1.setBounds(25, 64, 77, 12);
final Label label_2 = new Label(container, SWT.NONE);
label_2.setText("水印纵坐标:");
label_2.setBounds(26, 100, 72, 12);
picPathText = new Text(container, SWT.BORDER);
picPathText.setBounds(121, 22, 311, 22);
waterLeftText = new Text(container, SWT.BORDER);
waterLeftText.setBounds(121, 59, 311, 22);
waterTopText = new Text(container, SWT.BORDER);
waterTopText.setBounds(121, 95, 311, 22);
final Button selectPicbutton = new Button(container, SWT.NONE);
selectPicbutton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
FileDialog dlg = new FileDialog(getShell(), SWT.OPEN);
dlg.setFilterExtensions(new String[] {"*.bmp", "*.jpg", "*.tif", "*.*"});
String fileName = dlg.open();
if (fileName != null) {
picPathText.setText(fileName);
}
}
});
selectPicbutton.setText("...");
selectPicbutton.setBounds(435, 21, 30, 22);
final Label label_3 = new Label(container, SWT.NONE);
label_3.setText("(数字)");
label_3.setBounds(435, 64, 36, 12);
final Label label_4 = new Label(container, SWT.NONE);
label_4.setText("(数字)");
label_4.setBounds(435, 100, 33, 12);
//
return area;
}
/**
* Create contents of the button bar
* @param parent
*/
@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, "确定",
true);
createButton(parent, IDialogConstants.CANCEL_ID,
"取消", false);
}
/**
* Return the initial size of the dialog
*/
@Override
protected Point getInitialSize() {
return new Point(500, 293);
}
protected void buttonPressed(int buttonId) {
if (buttonId == IDialogConstants.OK_ID) {
picPath=picPathText.getText();
waterLeft=Integer.parseInt(waterLeftText.getText());
waterTop=Integer.parseInt(waterTopText.getText());
super.okPressed();
return;
}
super.buttonPressed(buttonId);
}
public String getPicPath() {
return picPath;
}
public int getWaterLeft() {
return waterLeft;
}
public int getWaterTop() {
return waterTop;
}
}
调用窗口的代码:
picWaterButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
ImageWaterMarkDlg imageWaterMarkDlg=new ImageWaterMarkDlg(getSite().getShell());
if (imageWaterMarkDlg.open()==Dialog.OK){
keyImage.AddImageWaterMark(imageWaterMarkDlg.getPicPath(),
imageWaterMarkDlg.getWaterLeft(), imageWaterMarkDlg.getWaterTop());
}
}
});
就这么简单,年纪大了,不服老都不行了,思维没有以前敏捷了。
From:一号门
Previous:DELPHI ftp 断点续传(indy)
Next:JAVA图像压缩,头都大了的问题.
COMMENTS