PopupMenu freezing parent frame - java

I'm using a AWT PopupMenu on a frame that constantly repaints (it's a game), but whenever the PopupMenu shows, the parent frame freezes. Is there a way to disable this?

I assume the rest of the application is in made in Swing and you are using the AWT popmenu in combination with Swing components.
I've had my fair share of AWT PopMenu problems. Do you need to show the Popmenu outside the bounds of your application ? If you only need to be able to show it inside your application (JFrame) you are probably beter off with just putting a JComponent on top of all your other panels (with a JLayredPane) and draw your own pop menu there.
Even easier is to use JLayer in Java 7 for the same effect (or JXLayer if your not on Java 7 but on Java 5 or 6).
The above method is also way more powerful then the AWT popmenu because you control the drawing. So for example making it translucent or giving it round edges becomes a breeze.

Related

Dragging Object Between java windows inside java and other delphi panel

I have a Java AWT window inside of a Delphi panel (I know it's bad, but it's part of a large project that I can't change).
I want to drag an object from the Java AWT window to another Delphi panel (other than the one the AWT window is in).
Unfortunately, while dragging the object outside of the Java window, the focus stays on it (I guess the focus lasts as long as the mouse-dragging-event occurs).
Is there anyway to convert the mouse-dragging event of the Java window to the mouse-dragging event of the other Delphi panel while the mouse is being clicked?
Here is an image that shows how the window looks.

Heavy version of JPanel?

I want to render Swing over top of LWJGL. It's my understanding that it doesn't work because LWJGL is heavy and most swing components are light. I'm guessing it's an option to put all my Swing stuff inside heavy components so they can render on top of it, in the same way a JPopupMenu does. Is there something that is basically a heavy version of JPanel? Like JInternalFrame, but without the top bar, close button, etc, just a simple rectangle? If not, is extending and disabling all the features of a JInternalFrame the best choice? (I'm assuming that is doable).
Additional related question: I read somewhere heavy components can't be transparent, does that just mean no translucency? Or does it mean I can't make a doughnut shaped heavy component?

Recreate Windows Look-and-Feel border of JFrame or JDialog?

I need to recreate the translucent border of JFrame or JDialog that appears when setting the windows look and feel in swing.
I need it because Windows LaF does not let you access the title bar (on the border). In fact, I need to apply a MouseAdapter to the JDialog that gets notified when it is dragged/pressed/released. In windows laf, as you cannot get access to the bar component, you can only apply a ComponentListener which gives you notification only when moving (so you don't capture anything when the user has grabbed it but hasn't moved yet, or either when the user "releases" it).
Therefore, I decided to go with undecorated dialogs and apply the listeners to my custom bar. However I want the custom dialog looks exactly as in windows laf (it means I need to recreate the border).
I'm not very experienced in Graphics2D to override the paintBorder() method, so I'm asking for your help.
Has anyone ever faced this problem and has a tested solution?
As of the Java Platform, Standard Edition 6 (Java SE 6) Update 10 release, you can add translucent and shaped windows to your Swing applications.
This means that you can have your JFrame emulate a native window with the rounded corners and transparency.
In your case, your approach would be in the JFrame level instead of the border level because the border is painted on top of the JFrame (or JDialog, for that matter). Therefore, if the JFrame is not already rounded, for instance, the paintBorder() method will still be painting on top of a rectangular area of the screen.
Check this tutorial from Oracle covering shaped and translucent JFrame.

Emulate This SWT Shell in Swing

I've been experiencing with SWT (GUI lib Eclipse uses) and I'm wanting to create the following using Swing.
The previous screenshot is done by the following code using SWT
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display, SWT.RESIZE);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
I was wondering, how could I possible emulate this in Swing?
P.S The border around this Shell is currently the native border for my windows color scheme, I don't wanna just create a MatteBorder and emulate the color, I'd like to use the native border for windows.
So the question is: Can native-styled windows with borders only be created using Swing?
Shortly: No. Unfortunately it is impossible to do this using Swing.
Also: Same "No" goes for the tool windows which can also be created using SWT.
Now let me explain why.
Swing uses pure-Java written rendering to paint any components and custom decorations within your application. Window decoration in Swing is a bit different topic though - by default JFrame and JDialog are decorated using native style - basically when window instance is created in your system from Java application - that window is asked to have default decoration for either frame or dialog (plus some other possible options). After that this window is used to render Swing components on it. But Swing code has no real control over the system-provided decoration, it can only switch it on/off and configure a few options in it. Unfortunately the option you are looking for is not there - most probably wasn't not implemented as not very popular one.
Though Swing allows custom component/window styling through custom L&F (Look and Feel). L&F which supports custom window decorations simply provides its own way to paint and control window decoration. In case such L&F is installed - Swing uses undecorated frames/dialogs by default and simply asks L&F to do the decoration. Anyone can write their own L&F which means anyone can create custom decorations.
So as #camickr mentioned before you have only one option if you want to create such frame (like on the screenshot in your post) in Swing - use either JDialog/JFrame with setUndecorated set to true or JWindow which is undecorated by default and "paint your way through". Graphics2D provided for all painting operations will allow you to create any custom decoration of your window, even the one on your screenshot.
I won't lie - creating custom window decoration is pretty difficult - it requires a lot of knowledge in Swing and large amount of written code, not only painting code. So I really doubt you will choose that option unless you are passionate about UI creation.
In the end - you have to choose between SWT and Swing (or even JavaFX) as each of these UI frameworks offer totally different sets of features and options. Window decoration is just a tip of the large iceberg.
EDIT:
Since JDK 7 it is actually prossible to create tool windows, all you need is to set your window type to Window.Type.UTILITY. Though it is still not possible to create a window with native borders only without any title bar.
Use an undecorated JDialog. Then you can add a MatteBorder to the root pane of the dialog. Or you can always implement you own custom Border to emulate the LAF Border.
On Eclipse I'm pretty sure it's done with an org.eclipse.swt.widgets.Shell with SWT.Border as the style. I can't think of a way to do this in Swing. You could write one yourself but it wouldn't have the system LAF.
If this is really important I would suggest using SWT instead, but that might require a re-write. Otherwise you could use a SWT Shell with Swing components embedded in it, but in my experience this is extremely fiddly and hard to debug.

Swing method to show or hide part of a panel on button click?

I'd like to be able to show and hide (and therefore grow and shrink the jpanel as appropriate) in my swing application. I've seen something similar on websites that I imagine uses JS and JQuery.
What I'm after is the ability to click a button (that might say 'Advanced options') that then 'slowly' grows the panel and reveals (setVisible(true)) components.
Is this possible using Swing? I've tried looking round the web, and SO but I only seem to get JQuery questions, or unhelpful Java posts. This is the closest I've found.
Use a Swing Timer to animate your slide. For example, this link has code that places the components in a JLayeredPane and slides one component over the other, again using a Swing Timer: slide effect with JPanel.
Another useful effect is to fade out from one JPanel to another. For an example of this that uses a Swing Timer as well as a CardLayout, please check out this answer.

Categories