eclipse中自定义对话框按钮的创建
问题:我想在自定义对话框中创建自定义按钮,并点击按钮时触发相应的事件,结果运行时看不到自定义按钮;若注释掉按钮自定义部分,则运行时可看到默认的对话框按钮。
若想达到我所要的结果如何解决?
我的代码如下:
package action.control;
import org.eclipse.swt.graphics.*;
import gui.gyjh.getOutputPath;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
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.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import action.file.FileOperation;
import util.HibernateUtil.HibernateUtilModel;
import util.process.DataProcessUtil;
public class DlgDataMaintenance extends Dialog{
private Composite topComp;
private Combo combo; //将下拉框写为实例,否则其他方法无法访问它
private FileDialog dialog;
private MessageBox msgBox;
public DlgDataMaintenance(Shell parentShell) {
super(parentShell);
// TODO Auto-generated constructor stub
}
protected Point getInitialSize(){
return (new Point(300,100));
}
/* (non-Javadoc)
* 在此方法中构建Dialog中的界面内容
*/
protected Control createDialogArea(Composite parent){
DataProcessUtil dataProcess = new DataProcessUtil();
topComp = new Composite(parent,SWT.NONE);
topComp.setLayout(new RowLayout());
new Label(topComp,SWT.NONE).setText("序号:"); //添加一个文本标签
combo = new Combo(topComp,SWT.READ_ONLY);
combo.removeAll();
combo.add("序号",0);
String strSQL = "SELECT distinct data1 FROM [table1] order by sn desc";
for(int i =0;i< dataProcess.getStrBatchCodes(strSQL).length;i++){
combo.add(dataProcess.getStrBatchCodes(strSQL)[i]);
}
combo.select(0);
combo.setLayoutData(new RowData(120,-1)); //用RowData来设置下拉框的长度
return topComp;
}
protected Button createButton(Composite parent, int id, String label, boolean defaultButton){
return null;
}
public static final int APPLY_ID =101;
protected void initializeButton(){
Composite comp = (Composite)getButtonBar();
super.createButton(comp, APPLY_ID, "批量维护", true);
super.createButton(comp, IDialogConstants.OK_ID, "生成文件", false);
super.initializeBounds();
}
protected void buttonPresson(int buttonId){
if(buttonId == APPLY_ID){
dialog = new FileDialog( (Shell) topComp, SWT.OPEN|SWT.MULTI);
dialog.setFilterNames(new String[]{"数据(*.xls)|Microsoft Excel(*.xls)"});
dialog.setFilterExtensions(new String[]{"*.xls"});
String fileName = dialog.open();
dialog.open();
FileOperation fileOperation = new FileOperation();
fileOperation.readExcelFile(fileName);
msgBox = new MessageBox(getShell());
msgBox.setMessage("处理完成");
msgBox.open();
}
super.buttonPressed(buttonId);
}
}
对话框 eclipse 按钮自定义 --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 有个疑问,楼主使用什么 ide 。我代码弄下来都是出错 --------------------编程问答-------------------- 我使用的elcipse中swt,我创建了主窗体通过点击某个按钮来调用自定义的对话框 --------------------编程问答-------------------- 代码中
public static final int APPLY_ID =101;
protected void initializeButton(){
Composite comp = (Composite)getButtonBar();
super.createButton(comp, APPLY_ID, "批量维护", true);
super.createButton(comp, IDialogConstants.OK_ID, "生成文件", false);
super.initializeBounds();
}
中重建button时应使用initializeBounds()函数,而不是initializeButton()函数,我改过来之后就可以显示按钮了,但如何创建自定义按钮的事件及具体实现,还请高手请教
补充:Java , Eclipse