It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am trying to create a shell in java swt application as dialog Shell. There is two button 'Accept' and 'Decline'. I want if user does not click any button during 30sec then shell will be disposed automatically. for this I am trying following code but its not working.Please help me Using any idea or suggestion
public class ServiceRequestDialog extends Dialog {
public ServiceRequestDialog(Shell parent,String nameofrequestor) {
// Pass the default styles here
this(parent,SWT.NO_TRIM|SWT.ON_TOP|SWT.DIALOG_TRIM|SWT.APPLICATION_MODAL);
this.parent=parent;
nameofRequester=nameofrequestor;
}
public ServiceRequestDialog(Shell parent, int style) {
// Let users override the default styles
super(parent, style);
}
public Shell open() {
shell = new Shell(getParent(), getStyle());
shell.setText(getText());
shell.setLocation(parent.getLocation().x+190, parent.getLocation().y+215);
shell.setSize(279, 181);
shell.setLayout(new FormLayout());
......
shell.open();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
// Return the entered value, or null
try {
System.out.println("Thread Sleep");
Thread.sleep(15000);
dispose();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return shell;
}
public void dispose(){
try {
if (shell != null) {
if (shell.isDisposed()==false) {
shell.dispose();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
I implemented something myself which should give you a starting point. It basically opens a JFace Dialog when you press the Button. This Dialog will close itself after a specified number of seconds (5 in the example):
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("StackOverflow");
Button button = new Button(shell, SWT.PUSH);
button.setText("Open dialog");
button.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
new MyDialog(shell, 5).open();
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static class MyDialog extends Dialog
{
private int counter = 0;
private int maxSeconds;
public MyDialog(Shell parentShell, int maxSeconds)
{
super(parentShell);
this.maxSeconds = maxSeconds;
setShellStyle(SWT.APPLICATION_MODAL | SWT.CLOSE);
setBlockOnOpen(true);
}
#Override
protected Control createDialogArea(Composite parent)
{
Composite composite = (Composite) super.createDialogArea(parent);
composite.setLayout(new GridLayout(1, false));
final Display display = composite.getShell().getDisplay();
final Label label = new Label(composite, SWT.NONE);
label.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
/* Set up the timer here */
final Runnable timer = new Runnable()
{
public void run()
{
if(!label.isDisposed())
{
label.setText("" + counter++);
label.pack();
if(counter <= maxSeconds)
display.timerExec(1000, this);
else
MyDialog.this.close();
}
}
};
/* And start it */
display.timerExec(0, timer);
return composite;
}
#Override
protected void configureShell(Shell newShell)
{
super.configureShell(newShell);
newShell.setText("Dialog");
}
}
Related
I'm trying to make a dialog stay on top of it's parent. The following code is similar to what I've done with the child dialog, minus the passing in of a parent. I started out by writing the following code:
public static void main(String [] args)
{
final Display display = new Display();
final Shell shell = new Shell(display, SWT.ON_TOP);
shell.setLayout(new FillLayout());
shell.open();
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
{
display.sleep();
}
}
}
This causes the dialog to remain on top, but now I'm no longer able to move it. I tried updating the call to the shell's constructor to:
final Shell shell = new Shell(display, SWT.ON_TOP | SWT.DIALOG_TRIM);
and
final Shell shell = new Shell(display, SWT.ON_TOP | SWT.SHELL_TRIM);
Both of these options allows me to change the size of the dialog by clicking and dragging on the border around the window but does not allow me to move the dialog.
The only thing I found online was to add a listener for mouse events and do the moving myself:
Listener l = new Listener()
{
Point origin;
#Override
public void handleEvent(Event pEvent)
{
switch(pEvent.type)
{
case SWT.MouseDown:
origin = new Point(pEvent.x, pEvent.y);
break;
case SWT.MouseUp:
origin = null;
break;
case SWT.MouseMove:
if(origin != null)
{
Point p = display.map(shell, null, pEvent.x, pEvent.y);
shell.setLocation(p.x - origin.x, p.y - origin.y);
}
break;
}
}
};
shell.addListener(SWT.MouseDown, l);
shell.addListener(SWT.MouseUp, l);
shell.addListener(SWT.MouseMove, l);
shell.open(); //Rest of code as above
I found this suggestion at: http://jexp.ru/index.php/Java_Tutorial/SWT/Shell
Is there anyway to create a dialog in SWT that is always on top and has the same look, feel and interactions of a default SWT dialog (a dialog with style: SWT.SHELL_TRIM) without having to write my own listener?
You need use own listeners. Below code should help:-
public class Demo {
static Boolean blnMouseDown=false;
static int xPos=0;
static int yPos=0;
public static void main(final String[] args) {
Display display=new Display();
final Shell shell = new Shell( Display.getDefault(), SWT.RESIZE);
shell.open();
shell.addMouseListener(new MouseListener() {
#Override
public void mouseUp(MouseEvent arg0) {
// TODO Auto-generated method stub
blnMouseDown=false;
}
#Override
public void mouseDown(MouseEvent e) {
// TODO Auto-generated method stub
blnMouseDown=true;
xPos=e.x;
yPos=e.y;
}
#Override
public void mouseDoubleClick(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
shell.addMouseMoveListener(new MouseMoveListener() {
#Override
public void mouseMove(MouseEvent e) {
// TODO Auto-generated method stub
if(blnMouseDown){
shell.setLocation(shell.getLocation().x+(e.x-xPos),shell.getLocation().y+(e.y-yPos));
}
}
});
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.close();
}
}
I have a JFace dialog and a toggle button( whose text is either "freeze" or "unfreeze") in the button bar.
Initially i select an object and click on a menu item to open the dialog.
From then on, whenever I click on toggle button(when text on it is "unfreeze") the dialog should close and reopen.
How do i achieve this ?
This should give you an idea how to do it (quick hack):
private static MyDialog dialog;
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Button openDialog = new Button(shell, SWT.TOGGLE);
openDialog.setText("Toggle dialog");
openDialog.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
if (openDialog.getSelection())
{
if (dialog == null)
{
dialog = new MyDialog(new Shell(display));
dialog.open();
}
if(dialog.getShell() != null && !dialog.getShell().isDisposed())
dialog.getShell().setVisible(openDialog.getSelection());
}
else
{
if (dialog != null && dialog.getShell() != null && !dialog.getShell().isDisposed())
dialog.getShell().setVisible(openDialog.getSelection());
}
}
});
shell.open();
shell.pack();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
private static class MyDialog extends Dialog
{
public MyDialog(Shell parentShell)
{
super(parentShell);
setShellStyle(SWT.CLOSE | SWT.MODELESS | SWT.BORDER | SWT.TITLE);
}
#Override
protected Control createDialogArea(Composite parent)
{
Composite container = (Composite) super.createDialogArea(parent);
Text text = new Text(container, SWT.BORDER);
return container;
}
#Override
protected void configureShell(Shell newShell)
{
super.configureShell(newShell);
newShell.setText("Some dialog");
}
#Override
protected Point getInitialSize()
{
return new Point(450, 300);
}
}
It will create and open the dialog on first press of the button and hide/unhide in on consequent press events (using Shell#setVisible(boolean)).
If this is not what you had in mind, please update your question or post a comment.
When the content of a the Text exceeds the width of the Text, I want to show dots (...) at the end of the text field.
I have tried to using a ModifyListener without success. Any help regarding this would be much appreciated.
#Override
public void modifyText(ModifyEvent e) {
// TODO Auto-generated method stub
if (wakeupPatternText.getText().length()>=12) {
String wakeuppattern=wakeupPatternText.getText(0, 11);
String dot="...";
String wakeup=wakeuppattern+dot;
wakeupPatternText.setText(wakeup);
}
}
});
This should do what you want:
private static String textContent = "";
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout(SWT.VERTICAL));
Text text = new Text(shell, SWT.BORDER);
text.addListener(SWT.FocusOut, new Listener()
{
#Override
public void handleEvent(Event e)
{
Text text = (Text) e.widget;
textContent = text.getText();
text.setText(textContent.substring(0, Math.min(10, textContent.length())) + "...");
}
});
text.addListener(SWT.FocusIn, new Listener()
{
#Override
public void handleEvent(Event e)
{
Text text = (Text) e.widget;
text.setText(textContent);
}
});
Button button = new Button(shell, SWT.PUSH);
button.setText("Lose focus");
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
It listens to focus events and truncates the visible String if it is too long.
This is how it looks:
With focus:
Without focus:
The reason why your code isn't working is the following: When you call wakeupPatternText.setText(wakeup); from within the VerifyListener the listener itself is called again recursively.
I am really needing to understand how parent/child dialogs work.
My users use a OTB Application called Teamcenter. I am writing a add on application that is invoked from a menu selection in the Teamcenter Application.
When they click the menu item, that executes a handler class and that creates the base dialog for my application.
public class AplotDialogHandler extends AbstractHandler {
private static AplotBaseDialog dlg = null;
public AplotDialogHandler() {
}// end Constructor
//////////////////////////////////////////////////////////////////////////
// execute() //
//////////////////////////////////////////////////////////////////////////
#Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
if (dlg == null) {
try {
AbstractAIFApplication app = AIFDesktop.getActiveDesktop().getCurrentApplication();
TCSession session = (TCSession) app.getSession();
TCUserService userService = session.getUserService();
AplotVersion.negotiateVersion(userService);
AplotQueryCapabilities.initialize(userService);
dlg = new AplotBaseDialog(null, session);
}
catch (Exception ex) {
MessageBox.post(HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell(), ex, true);
}
}
dlg.create();
dlg.getShell().setSize(700, 400);
dlg.open();
return null;
}// end execute()
}// end EdiDialogHandler()
Question 1. It seems like my application is not tied to the Teamcenter application. Meaning that I can close Teamcenter and my Application stays open.
Question 2. Should I get the workspace shell and pass it in the base dialog?
But even when my application is open, the user still needs to be able to use the Teamcenter application to select data to send to my application
Question 3. When opening dialogs from my base dialog, should I always pass the base dialog shell to those dialogs?
Question 4. Is there a standard way I should close down the dialogs when the user is done?
You need to pass the parent Shell to the dialog so that when you close parent shell, child shells will also be closed.
You should make your dialog MODELESS ( use SWT.MODELSS as style. Note: it is Hint) so that it will not block your parent shell.
Here is sample code:
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
shell.setSize(200, 200);
Button b = new Button(shell, SWT.NONE);
b.setText("Click");
b.addSelectionListener(new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent e) {
CDialog dialog = new CDialog(shell);
dialog.open();
}
#Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static class CDialog extends Dialog
{
/**
* #param parentShell
*/
protected CDialog(Shell parentShell) {
super(parentShell);
}
/* (non-Javadoc)
* #see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
*/
#Override
protected Control createDialogArea(Composite parent) {
Composite comp = (Composite) super.createDialogArea(parent);
Label lbl = new Label(comp, SWT.NONE);
lbl.setText("Test modeless dialog");
return comp;
}
/* (non-Javadoc)
* #see org.eclipse.jface.window.Window#getShellStyle()
*/
#Override
protected int getShellStyle() {
return SWT.DIALOG_TRIM|SWT.MODELESS;
}
}
I am working with SWT JFace dialog.
I added a listener to the OK button, I want to display a message box once the user clicks on the OK button.
The problem in this step is that when I once click on the OK button the shell gets disposed. How I can prevent this behavior?
The following code will prevent the dialog from beeing closed via the "OK" button. Just don't call this.close() in the okPressed() method:
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new OptionsDialog(shell).open();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private static class OptionsDialog extends Dialog {
private Composite composite;
public OptionsDialog(Shell parentShell)
{
super(parentShell);
setShellStyle(parentShell.getStyle() | SWT.CLOSE | SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
setBlockOnOpen(true);
}
protected Control createDialogArea(Composite parent) {
this.composite = (Composite) super.createDialogArea(parent);
GridLayout layout = new GridLayout(1, false);
layout.marginHeight = 5;
layout.marginWidth = 10;
composite.setLayout(layout);
createContent();
return composite;
}
private void createContent()
{
/* add your widgets */
}
protected void configureShell(Shell newShell)
{
super.configureShell(newShell);
newShell.setText("Shell name");
}
public void okPressed()
{
/* DO NOTHING HERE!!! */
//this.close();
}
}