Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2006

Tip: Looking for answers? Try searching our database.

[SWT] How to enable and disable windows correctly, if using dialogs?

Thread view: 
Thomas B. - 29 May 2006 16:51 GMT
Hi,

I am programming a configuration gui for a hardware simulator. There I
wrote some gui elements, which enable the user to set up some
parameters used by the simulator.

For example it is possible to enter values in textfields. I do a check,
if the entered value even is an int, and if not, I create a short error
message. For example like this:

       textwidth.addModifyListener(new ModifyListener() {
           public void modifyText(ModifyEvent e) {
               try {
                   width = Integer.parseInt(textwidth.getText());
               } catch (NumberFormatException nfe){
                   if (!textwidth.getText().equals("")) {
                       width = 0;
                       ErrorHandling error = new ErrorHandling(
                           "Please enter numbers only!\r\nWidth is
reset to 0.",
                           selscomposite.getShell());
                       textwidth.setText("0");
                   }
               }
               //modify the scale so it represents the entered value
               scalewidth.setSelection(scalewidth.getMaximum() - width
+ scalewidth.getMinimum());
           }
       });

textwidth is the textfield, representing the value chosen through a
scale and enabling the user to enter a value manually, and the
scale-representation is updated if done so.

ErrorHandling is the class I use throughout the project to display
small error messages. It looks like this:

public class ErrorHandling {

   Shell error;
   Shell parentshell;

   public ErrorHandling(String errormessage, Shell shell) {

       //shell displaying the errormessage
       error = new Shell(SWT.TITLE | SWT.BORDER);
       error.setText("User input error");
       //the layout for the error shell
       FillLayout filllayout = new FillLayout(SWT.VERTICAL);
       filllayout.marginHeight = 5;
       filllayout.marginWidth = 5;
       filllayout.spacing = 3;

       error.setLayout(filllayout);
       //disable the parent shell
       parentshell = shell;
       parentshell.setEnabled(false);

       //set the errormessage
       CLabel errorlabelmessage = new CLabel(error, SWT.CENTER |
SWT.SHADOW_IN);
       errorlabelmessage.setText(errormessage);

       //a button to close the errormessage
       Button errorreceived = new Button(error, SWT.PUSH |
SWT.CENTER);
       errorreceived.setText("Ok, i got it!");
       errorreceived.addSelectionListener(new SelectionAdapter() {
           public void widgetSelected(SelectionEvent e) {
               parentshell.setEnabled(true);
               error.close();
           }
       });

       //create the errorshell
       error.setActive();
       error.pack();
       error.open();
   }
}

parentshell is needed, so I can disable the shell, in which the error
occured until the user hast closed the errormessage by using the "Ok, I
got it!"-button. I don't even know if this is good style, but it's all
I know to do so far. I need to have a new shell-variable (e.g.
parentshell), because otherwise I could not use it in the inner class
within the listener-context. This leads to the first question: Is there
no better way to handle this, but the new Object "parentshell"?

The second problem with this implementation is, that if I do not use
the SWT.BORDER and SWT.TITLE constants within the initialization of the
error-Shell-object, the small window displaying the error message looks
real crappy. The title and a window border is needed, but if I have
those, I also have a close-button on the upper right of the window.
Because a disabled the parent shell:

       parentshell = shell;
       parentshell.setEnabled(false);

I have to enable it, if the error message is closed. I am able to
enable it, if the user choses the button, but if the user closes the
window by the close-button of the window-manager, the parent-shell
never ever gets accessible, because there never came a command to
enable it again.

A last few words: I tried several constants in the creation of the
error-Shell-object, but most of them (like ON_TOP, or several modal
state specifiers) only have the effect, that the error-message has no
border and no title any more. I am using Linux and the X-Server, so I
don't know what the behaviour of these constants would be on other
window-architectures.

Now this was a whole lot of information, but ask me, if you want to
know something more specific. Any comments are welcome, even those,
telling me what I have done is totally crap and I should do it like
this or that.

Thank you in advance!

PS: Sorry for any errors and the bad english.
Thomas B. - 30 May 2006 13:51 GMT
I now found an easy way to setup message boxes, without doing it all by
yourself:

http://help.eclipse.org/help31/nftopic/org.eclipse.platform.doc.isv/reference/ap
i/org/eclipse/swt/widgets/MessageBox.html


*doh*
veryhairydog - 31 May 2006 14:39 GMT
> For example it is possible to enter values in textfields. I do a check,
> if the entered value even is an int, and if not, I create a short error
> message. For example like this:

if you use jformattedtextfields instead of textfields, then user wont
be able to enter anything but an integer, then you can set a formatter
factory like so:

NumberFormatter numberFormatter = new NumberFormatter();
 numberFormatter.setValueClass(Integer.class);
JFormattedTextField f=new JFormattedTextField(numberFormatter);


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.