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.
Related
I would hide a JButton in a JApplet. I'm using setVisible() method but I've a problem: it works but my GUI is shifted because of the absence of the component. Is there a way to hide a component and make its space occupied???
I know that is possible in Android, but in Java?
ps. To insert component in my JPanel I'm using GridBagLayout!
There are several ways to achieve this in general.
Most proper way is to layout other components in a way that they remain correctly attached at their current positions.
Since for complex layouts the proper way can be hard to get and especially hard to change afterwards, you can apply some layout 'hacks'. For example, instead of adding the button to the panel directly, you could add the button to a separate panel of its own (let's name it buttonPanel), and then add that panel together with the button to the panel containing the other components. That way when you remove the button, buttonPanel will stay to fill the gap.
However, depending on the way how you specified constraints, buttonPanel may shrink when you remove the button. To prevent this, just before removing the button, take the buttonPanel's width and set it as its minimum/preferred width; most LayoutManagers will respect this property.
Of course, you can always resort to hardcoding dimensions to avoid dynamic size calculations, but keep in mind issues with L&F and i18n.
Try using the setOpaque() method. Just do button.setOpaque(false); and that should do the trick. Does that work?
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.
I have to realize this design with Java Swing (see screen shot).
Is it possible? How can an element appear off the frame?
Add a JToolBar (non-floatable) to JFrame or parent JPanel (in the BorderLayout.NORTH position).
Put an Icon inside a JButton and add to tool bar.
Set "MODE DIAL" (or whatever) as the tool tip for the button.
Repeat for each icon/button required.
Use a PLAF to tweak the look.
You need to use a JWindow to contain your component.
I have a JFrame.
I also have a Box class which extends Component.
This box class has a paint method which makes a filled rectangle.
When I add multiple of these Box components to my JFrame, only the most recently added one is displayed when I call repaint on the JFrame.
I took a look at the layout managers, but I am not sure that's what I want. All I want is to be able to make an animation of whole bunch of rectangles wherever I want on the screen.
(I also tried creating a panel, adding the panel to the JFrame, and then adding all the Box components to the panel. This did not work either).
Thanks in advance!
You have 2 choices.
You can change the layout of your frame:
JFrame frame;
frame.setLayout(new FlowLayout());
Now, if you add more than one box, it will show up on the frame.
The other option is to do what you said you tried. (Adding a panel to the frame)
JPanel pane = new JPanel();
frame.add(pane);
(add the boxes to 'pane')
Also, you should be careful with the sizing of your Box. You will probably want a call to setPreferredSize() somewhere in the creation of the Box. This will tell Java what size to make the box when it is added to the layout.
You should also take a look at the Java Layout Manager Tutorials. There is lots of great info there.
And, one more thing. The reason only one box at a time was being displayed on the frame was because JFrame's layout manager is BorderLayout. And, when you call add on a component that has a BorderLayout, the component is automatically added to the center of the component. Subsequent calls to add will overwrite the center component, leaving only one component in the middle.
You do need to check out other layout managers. JFrame by default uses BorderLayout and without specifying the "place" a component is added, they get added to CENTER. Depending on what you want your UI to look like depends on the layout manager to use. I would suggest maybe using Netbeans GUI builder.
EDIT: Missed the part about what you want to add but the concept is still the same, if you just add these components to the default layout manager, they will get overwritten. Sounds like you may need to do your painting inside of just one of your Box components or create a JPanel and set the layout to null but then you would have to place them explicitly. Really depends on what you want to do with it exactly.
Do your layout on paper first, then read up on Swing layout managers.
Be aware that some Swing components only allow one component to be added to them. I've run across this when using Tabbed panes. Each tab can only accept one control (JPane?) so you have to create a separate panel with a layout to arrange the related controls and then as a unit add the pane to the tab. There are similar arrangements in the Swing library.
You could set the frame layout to null and then use setBounds() to position your boxes exactly where you want.
Thank you for all your answers.
Since I am using my own custom class, Box, I have the ability of setting the position of my the rectangle through the paint method.
I realized my Box class was extending the wrong thing. It should have been extending javax.swing.Jcomponent.
If I now use a panel with an OverlayLayout, add my components to that panel, they all show up properly.
I'm using Netbean's form creator and I'm trying out some things. I'm not sure if it's the layout manager, but when I create my own JPanel and add it to the main content pane of the my window, the size of the panel always maximizes inside the FrameView no matter what re-dimensioning methodology I use such as setSize or setPreferred size. I'm very new to AWT and Swing.
With NetBeans WYSIWYG designer it's a peace of cake - just be sure to use Free Form Design and use mouse for resizing. Maybe the panel is itself larger than FrameView, so double click on it (you are editing it exclusively) and make it smaller. Than double click to get back to parent component and you should be fine.
Or maybe check some tutorials at NetBeans site.
You're not supposed to set sizes manually. Leave that to the layout managers, and your GUI will not break when the window size or the font or even just a button label changes.
The trick with layout managers is that you can use not just one but several, in nested JPanels. That way, nearly any layout is possible.
This happens because the layout manager of JPanel's container (perhaps it is either JFrame or another JPanel) instructs the JPanel to maximize.
Do the following:
Find out, who is the parent of this JPanel (take a look at NetBeans's containment object tree)
Check, what layout is defined for the container
Read the documentation about LayoutManager's (they're in java.awt package)
???
PROFIT!
In my experience, Swing is a very picky thing. I'd try setMaximumSize and setPreferedSize. As a side note though: when ever I used GridLayout, it always stretches whatever is in each of the cells (to make it symmetrical I guess). Flow, Box, Border, and I think GridBag don't have that problem though.
-Brett