Can I place an image outside JFrame?
I am developing an app, and I wanted to make the Gui good looking and some part of the buttons should go outside. Is there a way to do this?
Yes, but it's not going to be easy, as you going to constantly need to monitor the position of the parent frame in order to maintain the position of the child window.
Essentially, what you can do is create a second, undecorated and transparent window. You would need to align and size the window next to the parent window.
On to this child window, you would need to then add a transparent component which would act as your primary container.
Take a look at How to Create Translucent and Shaped Windows for more details
For example:
How to draw images on transparent window?
How to make a transparent JFrame but keep everything else the same?
Creating a JFrame you can click through
No; the Swing framework doesn't handle painting outside the root component.
Related
I am maintaining a system which requires me to make components in the dialog resizable, the dialog box calls out a java class Panel.
What is supposed to happen:
What is currently happening:
Note: The image on the bottom layer represents the resized one. While the image at the top layer is the dialog box which is not yet resized.
As you can see, the component JPanel(the one with the black border) is not resized. I am trying to achieve what happend to the bottom layer image of the first attachment.
I tried to apply the answer in How to dynamically control auto-resize components in Java Swing and pattern it in current code but since my panel is only called in a dialog box so there are limitations. The problem is that the components and its hierarchy have been already implemented, I just have to make it auto-resize.
Here is my current outline:
If you want a simple solution you can use a layout manager as described here.
Or, if you wish to avoid a layout manager(like me) then you can have your program resize your elements every time there is a resize event.
Here is some sudo-code
frameOrPanel.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent componentEvent) {
element.setLocation(frameOrPanel.getWidth()*1/4, frameOrPanel.getHeight*1/4);
element.setSize(frameOrPanel.getWidth()*1/2, frameOrPanel.getHeight()*1/2);
}
});
You will have to add unimplemented methods.
Note: the 1/4 and 1/2 is merely a ratio you can change those to fit your application.
I have two JPanel instances in a JLayeredPane, on different z-orders. I want both of my child JPanels to always fill the space of the LayeredPane.
The idea is for me to toggle the display of a 2nd panel over top of the first to display a modal-like dialog. Yes, I could just use a JDialog, but I thought it would be fun to try and create some transparancy overtop of the covered JPanel for a nice effect.
I find that using a layout manager on the JLayeredPane, like BorderLayout, and trying to set both children to CENTER conflicts since both panels can't be in the Center.
Is there a trick that I'm not seeing?
The idea is for me to toggle the display of a 2nd panel over top of the first
The easiest way to do this is to use a Glass Pane.
Check out the Disabled Glass Pane for an example of this approach.
There are two ways to create some "Glass Panel like" overlay for JPanels with JLayeredPane:
Add a ComponentListener to the JLayeredPane and update the sizes of all child components whenever the size of the JLayeredPane changes
Create a simple FillLayout, which expands the size of its child Components to the size of the Layout Container (In our case the JLayeredPane). You need to keep a list of children Components. During layoutContainer you copy the dimensions of the Container to these child Components. I wrote this and its really simple, but unfortunately I can't post it, since it's corporate. But if anyone is interested just ask in the comments. The implementation basically consists of one-liners.
For both solutions you need to make sure, that the panels on top are transparent, by setting setOpaque to false. This ensures that underlying panels render their content.
So basically, I am trying to get a JPanel window which will display all components inside dynamically. In other words, which will re-size the window, and display to fit its content.
I have been able to do it with help of JFrame and its pack() method which : "causes this Window to be sized to fit the preferred size and layouts of its subcomponents".
In my situation, I dont want to use JFrame because it will require much effort to make all changes.
Right now, I am able to make it work but only with the help of jscroll inside which wraps the text and or any new lines, so the window size is more static. So my JPanel is extending a TopComponent and am able to display it with:
jpanel.open();
jpanel.requestActive();
So the question is how to resize a window to fit its content upon actions in that window.
The JPanel has to be added to a Window in order to make sense. So I suggest you use layout managers correctly and you will get to a decent user interface.
When you add/remove components from a visible panel you need to use:
panel.revalidate();
panel.repaint();
Then the layout manager will lay out the components again.
I'm in the need of a translucent AWT component/container to be placed on top of other components to receive the mouse events.
The problem here is to create a translucent component. My investigation so far showed that Swing has the possiblity to do setOpaque(false) on e.g. a JPanel. Is there something similiar for AWT?
The idea is to make a ScrollPane scrollable by simply clicking somewhere and moving the mouse around. Thats why I need a transparent component to place it above all other components.
Long story short, I need a translucent component, no matter which one, to place on top of other elements to receive the mouse events.
Thanks in advance
AFAIK not possible correctly for prehistoric AWT Container and Components you can to try that with changing this code from Swing to the AWT, Translucency is supported for Swing JComponents only
The JNA project provides some utilities for making AWT components translucent. There's a WebStart demo of the functionality. The window transparency utilities are in platform.jar, which augments the base jna.jar.
However, if all you want to do is trap mouse events, you should look into the different layers that are already built into a JFrame. The component already has a concept of layers via JLayeredPane, and you can fairly easily use a JPanel as a glass pane which sits above all other components to filter events.
I need to create a panel which should be invisible but the components inside it (for example, JTextArea, JButton, etc.) should be visible. When I click on the invisible panel, it should become visible.
I can only use JRE 1.4 and nothing more than that. :(
Any idea how to create such a transparent panel???
Transparancy is controlled with
setOpaque(false)
JComponent.setOpaque(false) is the way to go.