LittleZZ Veteran 442 Posts user info edit post |
I'm working on a Java app for work and I've got all of the base code written, I just need to get the GUI together. Basically I'm going to have a main window that will open when the program is run. From this you can open other windows to edit preferences, view preferences, etc. My question is, what is the best way of implementing this? I tried setting up sepereate window classes and calling one from the main class. This works, except when the 2nd window is closed the whole program is closed. I also tried InternalFrames, but I'm having difficulty getting the content Panel to be displayed and relizied when testing, that the internal frame can only be displayed inside of the calling frame (duh). Any suggestions would be great as I don't have much experience making a GUI.
Oh, and right now I'm using the java.awt.* and javax.swing.* libraries. 10/27/2005 5:15:33 PM |
Incognegro Suspended 4172 Posts user info edit post |
format and reinstall 10/27/2005 9:37:50 PM |
LittleZZ Veteran 442 Posts user info edit post |
haha...wait...dammit there went my computer
actually I'm going with SWT instead of swing but thanks anyway 10/28/2005 12:36:00 AM |
LittleZZ Veteran 442 Posts user info edit post |
ok, new question for anyone with SWT (or swing because apparently they have the same limitation) experience. The program I am working on is to check for updates on code levels for machines we have at work. I want to set it up where it checks for updates every X minutes. I've got it where it will check for updates without any issue, once, but I'm having trouble implementing a timer so that a window will pop up every X minutes and it checks for updates. With SWT you can't run SWT widgets in a new thread without calling asynch or synch on the Display from the root thread, which makes it difficult to do any kind of Timer or schedule that part of the program to run. I've tried using TimerTask and putting a thread to sleep for the duration of time, but I can't seem to get anything to work. Anyone have any suggestions of how to do this?
[Edited on October 30, 2005 at 1:44 AM. Reason : bleh] 10/30/2005 1:42:17 AM |
madmoose Starting Lineup 68 Posts user info edit post |
I played around with some code and I came up with this... if this doesn't work for you the way you need it, let me know and maybe I can figure something out that works for you. Not sure if all of this is strictly required but it was my first time trying something like this so lets see..
ConnectAction action = new ConnectAction(); Timer timer = new Timer(); timer.scheduleAtFixedRate(action,0,5000);
ConnectAction extends TimerTask, the run method in ConnectAction looks something like this...
public void run(){ final MyDialog dialog = new MyDialog(Display.getCurrent()); Display.getCurrent().syncExec(new Runnable(){ public void run() { dialog.open(); } }); }
This opens MyDialog every 5 seconds. I tried it out using an informational dialog (Ok/Cancel). I don't know if your case is that the dialog is a progress monitor that dismisses itself when it is finished? Does this work or have you already tried this?
Forgot to mention, I'm using Java 1.5
[Edited on October 30, 2005 at 9:34 PM. Reason : had some extra stuff that wasn't necessary]
[Edited on October 30, 2005 at 9:40 PM. Reason : Using Java 1.5] 10/30/2005 9:30:38 PM |
LittleZZ Veteran 442 Posts user info edit post |
cool moose, i'll give it a try when i get in tomorrow. i just started with SWT from the recomendation of a coworker and didn't know about the "getCurrent()" method, but what you posted makes sense. I also found something else today, a variation of using a thread and putting it to sleep, that may work as well....I'll post here if it works, thanks 10/31/2005 12:13:18 AM |
madmoose Starting Lineup 68 Posts user info edit post |
No problem.. oh and here is what all of my SWT dialog classes basically look like since in SWT you are forbidden from subclassing Shell and you need to include your own event loop.
public class HostDialog{
private Shell shell; public HostDialog() { shell = new Shell(); shell.setLocation(200,200); createPartControl(shell); } public HostDialog(Display display) { shell = new Shell(display,SWT.DIALOG_TRIM); shell.setLocation(200,200); createPartControl(shell); } public HostDialog(Shell parent) { shell = new Shell(parent,SWT.DIALOG_TRIM); Point loc = parent.getLocation(); shell.setLocation(loc.x+100,loc.y+100); createPartControl(shell); } private void createPartControl(Shell parent) { ... Create Widgets here with the Shell as the parent ... } public void open() { shell.open(); // SWT Event loop while(!shell.isDisposed()) { if(!shell.getDisplay().readAndDispatch()) { shell.getDisplay().sleep(); } } shell.dispose(); }
}
Opening a dialog in this way also means that the dialog will be owned by the current thread so that part where I launched the dialog from the syncExec block isn't necessary (I tend to overuse (a)syncExec because of the number of times I've ended up running into Invalid Thread Access exceptions) 10/31/2005 12:38:59 PM |
LittleZZ Veteran 442 Posts user info edit post |
moose, thanks for the help, but i'm still getting invalid thread access errors on the line where I create the shell in the dialog box. I'm wondering if this is a limitation of the Java that I am using, blackdown v 1.4.2. Unfortunetly, I cannot use SUN java so I may be stuck. I'm going to try another method that I found. Let me know if you have any other advice. 10/31/2005 5:52:47 PM |
LittleZZ Veteran 442 Posts user info edit post |
woohoo...got it...had to make a class that extends Thread and put it to sleep for the duration, but it works. thanks for your help moose, i'll post if i have any more issues 10/31/2005 11:22:31 PM |
madmoose Starting Lineup 68 Posts user info edit post |
Invalid Thread Access exceptions mean you need to make sure that you are calling syncExec or asyncExec on the right display. If you were still getting invalid thread access issues from within a syncExec block, I think that must mean that you are using the wrong Display. Might not be the Current, might try Display.getDefault() instead, or get the Display from the SWT control you are accessing that is causing the exception. I believe every control has a getDisplay() method. If not, I know you can call getShell() from every control.. and each Shell is associated with a Display.
But anyways, if you've got it working that's good enough.
As for Java version, I dunno if that is playing a difference. I only mentioned Java 1.5 because the APIs for the Timer stuff might have changed in 1.5. Most of the SWT programming I've done has been in 1.4.2 so that shouldn't be an issue itself. Never used Blackdown though, nor met anybody who has so I couldn't say for sure if that is the problem. 11/1/2005 12:47:31 AM |
LittleZZ Veteran 442 Posts user info edit post |
moose, if you're still around...how do i get a label to change it's text after the shell has been opened? basically my updated dialog that we were discussing before has a Label and a progress bar. When there isn't a file being downloaded, it should say something like "Checking for updates..." When it finds an update and downloads the file, it should change to something like "Downloading file: [file name]" I'm finding through trial and error that just setting the text of the label isn't working, and setting the text after the shell is opened doesn't do anything. Are there restrictions on when it can be changed?
nevermind, i got it...just have to update the label
[Edited on November 3, 2005 at 6:13 PM. Reason : answered my own question]
11/3/2005 6:03:02 PM |
LittleZZ Veteran 442 Posts user info edit post |
alright..back to the top bitches..haha...ok, so things are up and running smoothly and I am now adding some features to the program. Right now, to download code the user has to know the name of the code file they are wanting to get, and type it into a text box. I'm wanting to use a FileDialog on the ftp server so that the user can type in a search and the dialog can use that search to filter out all the extra stuff on the server. However, I am having trouble figuring out how to get the filter path to be set to the remote server and not the user's machine. Anyone done this before or have any suggestions? I'm using Enterprise ftp for my file transfers and stuff, and it does have a method to get the working path, but it is relative to the server and not my machine so I can't use it. If I can't figure it out I'll just have to write my own search function which will probably be even more trouble 11/22/2005 8:42:39 PM |