指定JFace.Dialog初始化的位置

本文介绍了如何在SWT中通过重写特定方法实现对话框的精确位置控制,包括使新旧对话框底部对齐显示及让登录对话框记住上次的位置。
作者: dearwolf 发表于:javaeye 原文链接:http://www.javaeye.com/topic/40872
目的1: 打开一个新的 对话框 时,如何设定它和父 对话框 的相对位置?比如在登录 对话框 有一个“创建新帐号”的按钮,用户点击以后,就出现新的对话框用于注册,请问如何能让新的 对话框 和旧 对话框 排列的整齐一些?应该是能设定二者的相对位置吧?

最开始,以为要用 Shell.setLocation来设置,但是对于一个Dialog而言,它的Shell在什么时候才能初始化呢?

我在构造函数里面,configureShell(Shell newShell)方法里面,Control createDialogArea(Composite parent)方法里面都调用过了this.getShell方法想得到当前的Shell,结果都抛出空指针异常....

后来看书发现,应该重写protected Point getInitialLocation(Point initialSize)方法

比如,在最开始的例子中,在第二个对话框中我重写了该方法,代码如下:
  1. protected Point getInitialLocation(Point initialSize) {  
  2.         Point location = new Point(this.getParentShell().getLocation().x  
  3.                 + this.getParentShell().getBounds().width, this  
  4.                 .getParentShell().getLocation().y  
  5.                 + this.getParentShell().getBounds().height  
  6.                 - this.getInitialSize().y);  
  7.         return location;  
  8.     } 
其结果就是两个对话框底部对齐的平行排列:)

目的2: 登陆对话框要记住上次的位置。

想了半天,好像只能用IPreferenceStore来做了,在继承了AbstractUIPlugin的类中放入两个常量: 
  1. public static final String LOGINDIALOG_POSITION_X = "LOGINDIALOG_POSITION_X";  
  2.   
  3. public static final String LOGINDIALOG_POSITION_Y = "LOGINDIALOG_POSITION_Y"
然后重写两个方法: 
  1. @Override  
  2.     protected Point getInitialLocation(Point initialSize) {  
  3.   
  4.         String xposition = preferenceStore  
  5.                 .getString(Peer68TPlugin.LOGINDIALOG_POSITION_X);  
  6.         String yposition = preferenceStore  
  7.                 .getString(Peer68TPlugin.LOGINDIALOG_POSITION_Y);  
  8.         if (xposition == null || yposition == null || xposition == ""  
  9.                 || yposition == "") {  
  10.             return super.getInitialLocation(initialSize);  
  11.         } else {  
  12.             return new Point(Integer.parseInt(xposition), Integer  
  13.                     .parseInt(yposition));  
  14.         }  
  15.     }  
  16.   
  17.     @Override  
  18.     public boolean close() {  
  19.         preferenceStore.setValue(Peer68TPlugin.LOGINDIALOG_POSITION_X, this  
  20.                 .getShell().getLocation().x);  
  21.         preferenceStore.setValue(Peer68TPlugin.LOGINDIALOG_POSITION_Y, this  
  22.                 .getShell().getLocation().y);  
  23.         return super.close();  
  24.     } 
大功告成!
初次使用kettle连接mysql时遇到的问题,使用的版本为kettle8.2,报错java.lang.NullPointerException at org.pentaho.di.ui.core.database.dialog.XulDatabaseDialog.open(XulDatabaseDialog.java:112) at org.pentaho.di.ui.core.database.dialog.DatabaseDialog.open(DatabaseDialog.java:61) at org.pentaho.di.ui.spoon.delegates.SpoonDBDelegate.newConnection(SpoonDBDelegate.java:495) at org.pentaho.di.ui.spoon.delegates.SpoonDBDelegate.newConnection(SpoonDBDelegate.java:482) at org.pentaho.di.ui.spoon.Spoon.newConnection(Spoon.java:8684) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.pentaho.ui.xul.impl.AbstractXulDomContainer.invoke(AbstractXulDomContainer.java:313) at org.pentaho.ui.xul.impl.AbstractXulComponent.invoke(AbstractXulComponent.java:157) at org.pentaho.ui.xul.impl.AbstractXulComponent.invoke(AbstractXulComponent.java:141) at org.pentaho.ui.xul.jface.tags.JfaceMenuitem.access$100(JfaceMenuitem.java:43) at org.pentaho.ui.xul.jface.tags.JfaceMenuitem$1.run(JfaceMenuitem.java:106) at org.eclipse.jface.action.Action.runWithEvent(Action.java:498) at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:545) at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:490) at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:402) at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source) at org.eclipse.swt.widgets.Display.sendEvent(Unknown Source) at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source) at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source) at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source) at org.pentaho.di.ui.spoon.Spoon.readAndDispatch(Spoon.java:1381) at org.pentaho.di.ui.spoon.Spoon.waitForDispose(Spoon.java:7817) at org.pentaho.di.ui.spoon.Spoon.start(Spoon.java:9179) at org.pentaho.di.ui.spoon.Spoon.main(Spoon.java:707) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.pentaho.commons.launcher.Launcher.main(Launcher.java:92) 1、DB连接打不开,双击报错空指针
05-17
package com.ceasw.itb.itb.importer.wizard; import java.lang.reflect.InvocationTargetException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.Wizard; import org.eclipse.ui.IImportWizard; import org.eclipse.ui.IWorkbench; import com.ceasw.itb.itb.importer.nl.Messages; import com.ceasw.itb.itb.importer.util.ImportITBUtil; public class ITBImportWizard extends Wizard implements IImportWizard { private ITBImportWizardPage page; public ITBImportWizard() { super(); setWindowTitle(Messages.ImportAAMSWizard_0); setNeedsProgressMonitor(true); } @Override public void addPages() { page = new ITBImportWizardPage(Messages.ImportAAMSWizard_1); page.setTitle(Messages.ImportAAMSWizard_2); addPage(page); super.addPages(); } @Override public boolean performFinish() { final Object[] checkedElements = page.getViewer().getCheckedElements(); // isOverwrite = false; try { getContainer().run(true, true, new IRunnableWithProgress() { @Override public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { monitor.beginTask(Messages.ImportAAMSWizard_3, IProgressMonitor.UNKNOWN); ImportITBUtil.saveBlock(checkedElements, monitor); ImportITBUtil.saveImportedIcd(); ImportITBUtil.savePacketValues(); } }); } catch (Exception e) { e.printStackTrace(); } return true; } @Override public boolean performCancel() { return super.performCancel(); } @Override public void dispose() { page.getViewer().setInput(new Object[0]); System.gc(); super.dispose(); } @Override public void init(IWorkbench workbench, IStructuredSelection selection) { } }
11-18
!SESSION 2025-09-10 09:26:59.442 ----------------------------------------------- eclipse.buildId=unknown java.version=1.8.0_402 java.vendor=Amazon.com Inc. BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en Framework arguments: -app_id vector.cfg.r70613db Command-line arguments: -os win32 -ws win32 -arch x86_64 -app_id vector.cfg.r70613db !ENTRY com.vector.cfg.user.com.vector.cfg.gui.app.dvcfg.Application 1 0 2025-09-10 09:27:03.852 !MESSAGE Thread 'main' [#1]: Version 5.29.30 SP1 (64-bit Build: 70613db) !ENTRY com.vector.cfg.workflow.vtt.linux.gtk.impl 2 0 2025-09-10 09:30:15.242 !MESSAGE Could not resolve module: com.vector.cfg.workflow.vtt.linux.gtk.impl [333] Unresolved requirement: Require-Capability: eclipse.platform; filter:="(osgi.os=linux)" Unresolved requirement: Require-Capability: eclipse.platform; filter:="(osgi.os=linux)" !ENTRY org.eclipse.osgi 4 0 2025-09-10 09:30:15.244 !MESSAGE Application error !STACK 1 java.lang.NullPointerException at com.vector.cfg.gui.app.dvcfg.assistants.telemetry.TelemetryPopup.rF(SourceFile:169) at com.vector.cfg.gui.app.dvcfg.assistants.telemetry.TelemetryPopup.createDialogArea(SourceFile:132) at org.eclipse.jface.dialogs.PopupDialog.createContents(PopupDialog.java:682) at org.eclipse.jface.window.Window.create(Window.java:431) at org.eclipse.jface.dialogs.PopupDialog.open(PopupDialog.java:1127) at com.vector.cfg.gui.app.dvcfg.internal.ApplicationWorkbenchAdvisor.c(SourceFile:336) at com.vector.cfg.gui.app.dvcfg.internal.ApplicationWorkbenchAdvisor.a(SourceFile:319) at com.vector.cfg.gui.app.dvcfg.internal.ApplicationWorkbenchAdvisor.postStartup(SourceFile:204) at org.eclipse.ui.internal.Workbench.lambda$18(Workbench.java:2818) at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1115) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338) at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1049) at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:155) at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:633) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338) at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:557) at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150) at com.vector.cfg.gui.app.dvcfg.Application.start(SourceFile:78) at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:203) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:137) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:107) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:400) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:660) at org.eclipse.equinox.launcher.Main.basicRun(Main.java:597) at org.eclipse.equinox.launcher.Main.run(Main.java:1468) 故障分析一下
09-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值