SWT - MessageBox opening and closing - java

Is it possible to open and close a messagedialog without the user having to click buttons?
When the user clicks a button on my form, the action from that button goes server side and gathers a list of items, takes a couple of seconds. I want a way to tell the users that the action is in progress. I was thinking a messagedialog with some text. .
Opens the message
MessageDialog.openInformation(shell, "Information", "Getting List From Server");
Then some how closes it
(something like MessageDialog.close)?
I looked at a progress bar but that was more than I really needed.

It might look like a big overhead at first, but I would suggest using an IProgressMonitor which shows the progress of your task.
The user will know whats going on when he/she sees a progress bar, rather than a dialog that looks like the gui is frozen.
Here is an article by Eclipse on how to use progress monitors correctly.
If you really want to go for your idea (which I would not suggest), you can try the following:
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
BazMessageDialog dialog = new BazMessageDialog(shell, "Information", null, "Getting List From Server", MessageDialog.INFORMATION, new String[]{"OK", "Cancel"}, 0);
dialog.open();
/* Do your stuff */
dialog.reallyClose();
shell.dispose();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static class BazMessageDialog extends MessageDialog
{
public BazMessageDialog(Shell parentShell, String dialogTitle,
Image dialogTitleImage, String dialogMessage,
int dialogImageType, String[] dialogButtonLabels,
int defaultIndex) {
super(parentShell, dialogTitle, dialogTitleImage, dialogMessage,
dialogImageType, dialogButtonLabels, defaultIndex);
setBlockOnOpen(false);
}
public void reallyClose()
{
cancelPressed();
}
}
This however, will not block your remaining gui, so the user will be able to use it in the meantime.
EDIT:
Just found out, that Opal has something called an InfiniteProgressPanel, which might be something for you. Have a look...

Related

Java Swing - Close JDialog from external Thread

I am working with Swing right now and I do not get this to work properly.
What I need is the following:
I've got a class "Client" that is able to connect to a TCP server.
If the connection fails (wrong IP for example), then it will show an error dialog that can be closed by clicking on the "OK" Button.
However if the client connected successfully, a window should popup that runs until my client receives a specific message from the server.
My code looks like this:
if(ip != null) {
Client c = new Client();
try{
c.connect(ip, 56556);
JOptionPane msg = new JOptionPane("Connecting...", JOptionPane.INFORMATION_MESSAGE);
JDialog dlg = msg.createDialog("Connecting...");
dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dlg.setVisible(true);
c.addIncomingMessageHandler(new IncomingMessageHandler(){
#Override
public void incomingMessage(Connection<?> cnctn, Object o) {
dlg.setVisible(false);
dlg.dispose();
}
});
}catch(Exception e) {
int n = JOptionPane.showOptionDialog(this, "Oops! Something went wrong!",
"Title", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE,
null, new Object[] {"OK"}, JOptionPane.OK_OPTION);
}
}
So the exception is throws if c.connect() fails.
c.addIncomingMessageHandler() is a listener that listens to any incoming messages to the client. If the server sends something, this method will be called. If that's the case, the JDialog will be closed. But this window can be closed right now by clicking on the OK-Button.
I'd like to rename that button and add a function.
The new text should be "Cancel" and if the button is pressed, the client should be closed (c.disconnect) and the window itself should be closed as well.
How could I do that?
From the Documentation:
Stopping Automatic Dialog Closing
By default, when the user clicks a JOptionPane-created button, the dialog closes. But what if you want to check the user's answer before closing the dialog? In this case, you must implement your own property change listener so that when the user clicks a button, the dialog does not automatically close.
DialogDemo contains two dialogs that implement a property change listener. One of these dialogs is a custom modal dialog, implemented in CustomDialog, that uses JOptionPane both to get the standard icon and to get layout assistance. The other dialog, whose code is below, uses a standard Yes/No JOptionPane. Though this dialog is rather useless as written, its code is simple enough that you can use it as a template for more complex dialogs.
Besides setting the property change listener, the following code also calls the JDialog's setDefaultCloseOperation method and implements a window listener that handles the window close attempt properly. If you do not care to be notified when the user closes the window explicitly, then ignore the bold code.
final JOptionPane optionPane = new JOptionPane(
"The only way to close this dialog is by\n"
+ "pressing one of the following buttons.\n"
+ "Do you understand?",
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION);
final JDialog dialog = new JDialog(frame,
"Click a button",
true);
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(
JDialog.DO_NOTHING_ON_CLOSE);
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
setLabel("Thwarted user attempt to close window.");
}
});
optionPane.addPropertyChangeListener(
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (dialog.isVisible()
&& (e.getSource() == optionPane)
&& (prop.equals(JOptionPane.VALUE_PROPERTY))) {
//If you were going to check something
//before closing the window, you'd do
//it here.
dialog.setVisible(false);
}
}
});
dialog.pack();
dialog.setVisible(true);
int value = ((Integer)optionPane.getValue()).intValue();
if (value == JOptionPane.YES_OPTION) {
setLabel("Good.");
} else if (value == JOptionPane.NO_OPTION) {
setLabel("Try using the window decorations "
+ "to close the non-auto-closing dialog. "
+ "You can't!");
}
Click here!
Related question.

Opening a new Window with Java RCP and SWT

I'm currently developing an app, and for this, I'm using Java RCP with SWT.
What I want :
I have a window, and when I click on a Button, i need a whole window to be opened. The window works perfectly and looks like this :
Window1
When I press it, a new window opens. It looks like this :
Window2 (Yup, the middle pic has its importance)
How it's currently done :
The Window 1 is a TrimmedWindow done with the Application.e4xmi, with some Parts in. The button is included in one of these parts. Here's its code :
#PostConstruct
public void postConstruct(Composite parent) {
Button b = new Button(parent, SWT.BORDER);
b.setText("Press me !");
b.addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event event) {
parent.getShell().dispose();
new Game(Display.getCurrent());
}
});
}
The Window 2, as you can see, is a new class called Game. Its constructor is as follows :
public Game(Display display) {
this.display = display;
this.shell = new Shell(this.display);
this.setData();
shell.setText("I don't work properly");
shell.setMinimumSize(800, 600);
this.buildUI();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!this.display.readAndDispatch ()) this.display.sleep ();
}
this.display.dispose ();
}
What's the problem ? :
When I launch the project with Eclipse, everything goes WELL. I mean, really. I click, it opens, it loads, yay ! But the idea after this is I export the project as an executable. So I do it. Here's my .exe file. And let's start. And it doesn't work. When I press the button, nothing happens. Not even an error message, nothing.
I've found some solution that says the problem comes from the display, because RCP is single threaded. So I followed the instructions, and here's another version of the Game constructor :
public Game() {
this.display = Display.getDefault();
this.display.asyncExec(new Runnable() {
#Override
public void run() {
shell = new Shell(display);
setData();
shell.setText("I work better but...");
shell.setMinimumSize(800, 600);
buildUI();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
});
}
And so here comes the new problem : I can't call an Image after this (hence the pic I showed on Window 2). Because Image (SWT) requires a display to be constructed, and because display doesn't work well without the Runnable instance, I can't use an image after this. And I need my Image here (and also somewhere else after this).
Edit : I have an error message in that case. It says :
org.eclipse.swt.SWTException : Failed to execute runnable
(java.lang.IllegalArgumentException : Argument cannot be null)
Any solutions anyone please ?
Thanks in advance.
Kosnyru.
If you add a Trimmed Window (or just a plain Window) to the 'Windows and Dialogs' section of the Application.e4xmi with 'To Be Rendered' turned off (leave Visible on) you can then show it using:
#Inject
EModelService modelService;
#Inject
MApplication app;
MUIElement window = modelService.find("window id", app);
window.setToBeRendered(true);

Preventing SWT shell from disposing

I created a SWT dialog with this implementation:
public class FindDialog extends Dialog {
private DialogResult result;
private Display display;
private Shell shell;
private Button okayButton;
/*...*/
public FindDialog(Shell parent) {
this(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
}
public FindDialog(Shell parent, int style) {
super(parent, style);
display = getParent().getDisplay();
initUI();
}
public DialogResult open() {
result = DialogResult.Cancel;
shell.open();
while (shell.isVisible()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
return result;
}
private void initUI() {
shell = new Shell(getParent(), getStyle());
shell.addListener(SWT.Close, new ShellClosingHandler());
okayButton = new Button(shell, SWT.PUSH);
okayButton.addListener(SWT.Selection, new ButtonClickedHandler());
/*...*/
}
private class ButtonClickedHandler implements Listener {
public void handleEvent(Event e) {
Button button = (Button)e.widget;
if (button == okayButton) {
result = DialogResult.OK;
shell.close();
}
}
}
private class ShellClosingHandler implements Listener {
public void handleEvent(Event e) {
if (result == DialogResult.OK) {
e.doit = validate();
}
}
private boolean validate() {
/*...*/
}
}
}
There are some more text fields, buttons and checkboxes but I think that it's not important for my question.
The dialog window popups correctly and I can make my changes on the GUI without any problems.
At last I click the ok button and then the following happens:
The SWT.Close event is firing, my validation method is called and depending on the result the dialog is closed or not. That's ok so far.
But shell.close() not only closes the dialog, it also disposes the shell. And exactly here is the problem because
I don't want to rebuild the dialog GUI everytime the open method is called. I create all my dialogs at program startup and then only want to open and close it if needed.
I need some values of the text fields or states of checkboxes after closing the dialog at different positions in the main program. So it seems a good idea to hold a reference of the dialog object and implement some getters to pull out the data. But if the shell is disposed I have no chance to get the information.
If the shell is disposed then I will loose the "state" of the dialog so I have to refill it next time I display the dialog.
So my question: Is there a possibility to prevent the shell from disposing?
Or is there another concept that I overlooked so I don't have to restructure my complete dialog set?
if (button == okayButton) {
result = DialogResult.OK;
shell.setVisible(false);
}
You can use setVisible(false) instead of close
So it will get hide and wont get dispose.
You can get the values of the text box after hide
No need to rebuild again
The past values in the textbox will be there after hide.

How do I detect that a SWT dialog has been opened and is visible?

I have an SWT WizardDialog with a number of pages. When this dialog first opens I have to do a check for some conditions and if those conditions are met I need to show a popup over the freshly opened dialog.
So I have this code to listen for SWT.Show event. The event listener responds to SWT.Show to conduct its tests and show a message box:
final WizardDialog dialog = new WizardDialog(shell, wizard);
dialog.setTitle("New Wizard");
dialog.create();
dialog.getShell().addListener(SWT.Show, new Listener()
{
private boolean firstShowing = true;
#Override
public void handleEvent(Event event)
{
if (firstShowing && someConditionExists())
{
MessageBox messageBox = new MessageBox(dialog.getShell(), SWT.OK
| SWT.ICON_WARNING);
messageBox.setMessage("Test");
messageBox.open();
firstShowing = false;
}
}
});
dialog.open();
Except it's called too soon! The dialog is not visible when the handler is called. My message box appears before the dialog is visible and the dialog only appears when I dismiss the message box.
So clearly the SWT.Show is unreliable, at least on Windows where I'm running it. I've also tried putting this code into a ShellListener on the activation but that happens even before the SWT.Show example above.
So how do I reliably show a message box when the dialog is made visible?
Plan B is a dirty timer based hack where a timer is set to fire 200 ms into the future and hope that it triggers when the dialog is visible but obviously this could introduce it's own issues.
I'm using in similar situation (need that appStarted() is called after application window is visible) something like below.
public class App extends ApplicationWindow {
#Override
protected Control createContents(Composite parent) {
// ...
getShell().addShellListener(new ShellAdapter() {
#Override
public void shellActivated(ShellEvent shellevent) {
if (!started) {
Shell s = (Shell) shellevent.getSource();
s.setVisible(true);
appStarted();
started = true;
}
}
});
}
}
Maybe You can use the same like below:
final WizardDialog dialog = new WizardDialog(shell, wizard);
dialog.setTitle("New Wizard");
dialog.create();
dialog.getShell().addShellListener(new ShellAdapter() {
#Override
public void shellActivated(ShellEvent shellevent) {
if (firstShowing && someConditionExists()) {
Shell s = (Shell) shellevent.getSource();
s.setVisible(true);
MessageBox messageBox = new MessageBox(dialog.getShell(), SWT.OK | SWT.ICON_WARNING);
messageBox.setMessage("Test");
messageBox.open();
firstShowing = false;
}
}
});
dialog.open();
Instead of hooking the SWT.Show event, you may get more luck with hooking a PaintListener on to your dialog's Composite. (You'll probably want to unhook it during the first execution.)
What about overriding dialog.open() methodon your WizardDialog class? The first line of the overridden method would call super.open(), which would make it visible. Just put your custom code after that, in the .open() method.
The issue with the approach you're taking above appears to be that it responds to a Show event, which is simply notification that Show has been requested, not that the dialog is visible. The Show event could very well be designed to allow you to know when something is about to be shown, and take some action before that happens, as you've experienced.
I know that this is an old thread. But in case someone finds it useful, I found that overriding Dialog.create() rather than Dialog.open() worked for me.
it's called too soon!
I also run recently in the same trouble. The code was executed too early - my upload action (which I wanted to start automatically under some conditions) was started before the page was displayed.
This happens because the page can only be shown after the code in the SWT.SHOW listener or in the inherited setVisible() method is completed.
#Override
public void setVisible(boolean visible) {
if (visible) {
org.eclipse.ui.progress.UIJob("Auto start the upload") {
#Override
public IStatus runInUIThread(IProgressMonitor monitor) {
if (isAutoStartQcUploadSelected)
startUpload();
return Status.OK_STATUS;
}
};
uiJob.schedule();
}
super.setVisible(visible);
}
org.eclipse.ui.progress.UIJob as described FAQ_Can_I_make_a_job_run_in_the_UI_thread has solved the issue.
P.S.: Yes, I know that's an old question :-)
But it is the first one propesed by google and the hint with the UI Job was missing.
The code of marioosh can be further improved, by storing the ShellAdapter in a variable.
Remove the ShellAdapter when the listener is triggered for the first time.
The variable started is no longer needed.
The statement s.setVisible(true); is not necessary, because this event is just triggered when the shell gets visible.
public class App extends ApplicationWindow {
#Override
protected Control createContents(Composite parent) {
// ...
ShellAdapter shellActivatedAdapter = new ShellAdapter() {
#Override
public void shellActivated(ShellEvent shellevent) {
shellevent.getSource().removeShellListener(shellActivatedAdapter);
appStarted();
}
};
getShell().addShellListener(shellActivatedAdapter);
}
}

"Nothing to display" on Nokia S40

I coded a j2me application using LWUIT. It works fine on emulator as well as a symbian device. But when i tried to run it on a nokia s40 device,it showed up a "nothing to display" message. I tried displaying a splash screen, as prescribed in some forums. Still,the app never gets past the splash screen.
EDIT 1
Display.init(this);
Resources r = Resources.open("/theme.res");
UIManager.getInstance().setThemeProps(r.getTheme(r.getThemeResourceNames()[0]));
Dialog splash = new Dialog("Splash Screen");
splash.setAutoDispose(true);
splash.setTimeout(5000);
splash.show();
RecordStore rs = null;
byte[] buffer = null;
rs = RecordStore.openRecordStore("xxxxxx", true);
if (rs.getNumRecords() > 0) {
buffer = rs.getRecord(rs.getNumRecords());
num = new String(buffer, 0, buffer.length);
rs.closeRecordStore();
offer(num); // a method which displays main form
} else {
rs.closeRecordStore();
registration("xxxxx"); //another method which displays the secondary form
}
In this snippet,a blank screen is displayed on the device after the dialog/splash screen.
The form gets displayed when i remove the codes managing the RecordStore.
How do i fix this mess ?
EDIT 2
Code for registration()
Form f = new Form();
f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
Image img = Image.createImage("logo.png");
f.addComponent(new Label(img));
Label lbl = new Label(msg);
f.addComponent(lbl);
f.addComponent(new Label("xxxxx"));
final TextArea number = new TextArea(1, 10, TextArea.NUMERIC);
f.addComponent(number);
Button btn = new Button("Register");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//perform rms related activities and move onto offer()
}
});
f.addComponent(btn);
Button help = new Button("Help?");
help.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//display a help dialog
}
});
f.addComponent(help);
f.addCommandListener(this);
f.show();
Change the splash.show() to splash.showModeless()
Regardless your code is incorrect since it assumes show() will display the dialog immediately which is not how most GUI frameworks work. Your method needs to complete and return control to LWUIT in order for the dialog to show. However, you read the RMS and then the code to show your form is unclear, when do you expect it to actually occur.
You need to show the dialog without a timeout (I would use a form for the splash screen there is no reason to use a dialog), then open a thread (new Thread(...)) to do whatever you want and then when the thread completes show your form.
From this blog, The Nothing to display issue is standard Nokia S40 behavior for delayed calls to setCurrent() and the normal recommendation is to show a splash screen early on to avoid this prompt.
Also look this same related discussion.
Edit:
Form splashscreen = new Form();
splashscreen.getStyle().setBgImage(imageName);
splashscreen.show()
Display.getInstance().callSerially(new Runnable() {
public void run() {
try {
Thread.sleep(5000L);
// do RMS related things here.
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
});

Categories

Resources