i built a GUI which also can save something and what I want is that during that saving, no user input is possible and buttons etc cannot be clicked or at least do not react on that.
Maybe you can give me an hint, how to that!
Thanks!
On way might be to use a progress bar. Maybe an indeterminate progress bar that displays animation while waiting for the task to finish.
Check out the section from the Swing tutorial on How to Use Progress Bars for more information and working examples.
Another option could be to use the Disable Glass Pane which will prevent the mouse an keyboard from working.
If you want to avoid user actions, you can disable every objects :
myButton.setEnabled(false);
Related
I have a program that load images and let user edit images with options that I provided.
I'm working on the Undo/Redo button. So when I click on the Undo button, it should bring my image back to the last time before editing. I have ideas about UndoManager but I'm really don't know how to create a function for that.
Should I cast my image to BufferedImage?
I recommend looking into Command pattern. If you are using Swing, that means Actions.
If you do it right, you can have "unlimited" undo/redo easily. You might need to serialize your edits after some time, to avoid running out of memory.
I am developing a Swing application with custom CSS rendering. As part of the system I use JDialogs as well, but I need to access certain components outside of the dialog (emergency buttons).
For this I chose following method:
all dialogs are non-modal. Those I want to make quasi-modal, will be setAlwaysOnTop.
There is always one quasi modal dialog up. In this case I put another dialog up, below this one, full-screen, semi-permanent.
This "blur" dialog shall cover the whole screen, catch mouse events and forward them to the emergency buttons only. In parallel I opened gaps on it so that the emergency buttons can be seen through the semipermanent surface without any effect.
The blur dialog can never be focused or activated (I have a vetoable listener for this)
Everything works fine, until someting happens and the blur dialog seems to cover my quasi-modal dialog. However, if I move any external application window over it, the quasi-modal dialog remains on the top. Instead of a complicated explanation, see picture un following link: http://lost.lost.hu/javascreen.png
So far I tried to debug repaints, events, everything, and could not find anything that would cause this. Especially the case depicted above challenges me to understand what is going on here.
I have recently updated to Java 1.7, in hope to get rid of this phenomenon, but today it came back.
This is a crosspost to the thread in Javaranch (includes some images): http://www.coderanch.com/t/567472/GUI/java/Optimal-solution-creating-multiple-dialog
I'm trying to develop a simple swing desktop application where I imagine alot of different dialog's jumping around to fetch user input. Would need to present labels, textfields, passwordfields, combobxes, checkboxes etc in various dialog windows.
For example: creating the database firsthand, creating the first admin account, adding users, changing user accounts etc.
I have an understanding that JOptionPane is used to create simple quick & easy modal dialog's. I would really like to know why one would choose one over another in this case. Which one is more preferable to use: JOptionPane vs. JDialog
Also I could use some pointers how one should appropriately design and implement this.
Thank you.
Here's a statement I found on the Java website that says one key point about the difference between the two.
How to make Dialogs
A Dialog can be modal. When a modal Dialog is visible, it blocks user input to all other windows in the program. JOptionPane creates JDialogs that are modal. To create a non-modal Dialog, you must use the JDialog class directly.
So it sounds like you would use JOptionPane if you want a user to have to make a choice and close the box before returning to the main screen. If you use a JDialog box, then they can just click around it and get back to the main screen without making a choice. For example, say you wanted to make a user choose the number of results before clicking submit, you wouldn't want them to be able to click around that window and click submit. You would use JOptionPane to force them to select a value first before going back to submit.
Check out http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html it pretty much has everything you would need.
As i understand it, JOptionPane is great for what it can do, but you can't really change the functionality beyond that (not easily). JDialog is better to inherit from if you want to create your own custom Dialogs.
I have a simple program that utilizes Java Swing Timer to display an image for 400 miliseconds, in this period of time I just want to stop all ActionListeners or stop taking ActionEvents. I've got 40+ buttons and want a simple way to do this.
Is there anyway to do that in Java?
Can you determine that you are in this "image displayed" state? The image goes up and you set the state to "image displayed" or whatever. Go through your widgets and decide which ones are supposed to be dead while the image is up. Turn them into Observers of this state value. When the state changes, they either enable or disable, as appropriate. The image code doesn't do anything directly to any widget. It just declares that the state is now "image displayed". It's up to the Observers to decide what to do, if anything, with that information.
Or use the GlassPane. That works too. Of course, the GlassPane shuts down everything. If you need to be more selective, you need a more fine-tuned approach.
You can use a temporary GlassPane instance to consume all events by registering empty listeners to it.
Use an undecorated modal JDialog to display the image. Before you make the dialog visible you would start a Timer. When the Timer fires in 400 ms you close the dialog.
I've had similar issues and typically found that its a design issue that got me in that situation. Being the case, I still had to find away around it. To fix the issue, I kept a list of the elements that I wanted to disable (stop listening) and iterated through them at the beginning and end of the timer. For buttons it should be as simple as:
for(Component c : listOfToggledComponents){
c.setEnabled(shouldItBeEnabled);
}
For buttons, this will grey out the button. Similar things happen to other swing components.
Wow, what a stupid question you might say. But is it possible? I have a mouse move event in a Jpanel and it works even when the application is not in focus, now can I have something like that for the clicking event or something similar. And most importantly I don't want other apps (even something like the main menu) to lose focus when i click on my app.
I thought of the system's event queue but i'm not sure where that would lead me to.
Thanks in advance.
[EDIT - the purpose]
I want to create an app that mimics the users interactions with the system in a later time. for example a user takes the mouse and clicks and writes and my app will mimic that in say 2 hours time. ofcourse i would need a system hook for the outer events but i wanted to avoid os-dependant code so i basically capture the screen, take it to my app,for example the user clicks on an icon in the captured picture and then for making that come to life, i translate the coordinates to the real icon and click it (with a Robot) and in this way i can capture the user's events in my own app. the problem occurs when the user clicks on the main menu or right clicks (he's doing that in my app, and my app does that to the system so my app is in between) and ofcourse the real main menu will lose focus when the user tries to click on one of it's items.
sorry for my english.
I'm still not sure I follow what you are trying to do. But the concept of an app getting focus when you click on it is fundamental to the GUI and I suspect rather difficult to get around.
I just found this:
Focusable Windows
To support palette windows and input methods, client code can prevent a Window from becoming the focused Window. By transitivity, this prevents the Window or any of its descendants from becoming the focus owner. Non-focusable Windows may still own Windows that are focusable. By default, every Frame and Dialog is focusable. Every Window which is not a Frame or Dialog, but whose nearest owning Frame or Dialog is showing on the screen, and which has at least one Component in its focus traversal cycle, is also focusable by default. To make a Window non-focusable, use Window.setFocusableWindowState(false).
In this doucment http://java.sun.com/j2se/1.5.0/docs/api/java/awt/doc-files/FocusSpec.html
That sounds like it might do what you want.