I have a Java desktop application that at certain point shows up a JPanel with a GridBagLayout and others JPanels inside the grid. This JPanel is showed with a certain color. What I want is to export this JPanel to PDF (I'm using iText) with another color.
No problem with the export (the PDF is generated with the JPanel in its original color) but I'm not sure of the right way to go for changing the JPanel color on the PDF.
My first approach was to set the backgroung color of the original JPanel for the color intended to go on the PDF. I have an utilitary class to generate my PDFs with a method that receives a JPanel and on this method, I made jPanel.setBackgroundColor. This works, but the problem (maybe obvious for most) is that it also changed the original JPanel showed on the application. I thought that invoking this method passing the JPanel would be Java pass-by-value. After some reading, I now understand that it is indeed a pass-by-value, but the value of the pointer to the object JPanel, and not really the object, thus changing it's property, also changes the original.
Another approach was to clone the original JPanel. But then noticed the JPanel is not Cloneable. Also thought of recreate the original JPanel over a new JPanel, but it's getting too complex.
So I'd like to have some opinions on which would be the right way to go.
Thank you very much in advance.
Cheers!
One simple, but slightly different solution would be to wrap another panel around your panel before showing it on your GUI and before printing it.
If you use a different Panel for GUI and for printing, you can use two different background colors (and also different other settings).
Of course this is not a strict solution to the stated problem, but probably a rather good workaround.
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?
I recently did a program which moves an image along the window and forces it to change its direction when it hits the borders. The animation happened within a JPanel class which was then of course added to a JFrame class which contains the main() function. Now my question is why must it be done in this manner. Can't I simply have just the JFrame and use that both as my window and my animation class which moves the image? Is the JFrame incapable of representing animation on it's own.
Regards.
Omar's answer is correct but I thought I might elaborate just a bit:
Though JFrame's are capable of hosting your animation, it is not a typical use of the JFrame. JFrame's are typically the outermost visual container and host one or more other visual containers (such as JPanel). Though there are exceptions, I would consider the JPanel the most flexible and common way to contain your animation (or other GUI 'controls') to leave you flexibility for adding visual features later.
But as Omar points out, either JFrame or JPanel can be used as they are both considered "containers" in Swing. Tying back to my last comment, using a JPanel is a bit more flexible. For instance, you might later want to incorporate your animation into an Applet/JApplet which you might find confusing and/or difficult if you used a JFrame.
I tried to just comment on Omar's answer but I am a new user and it won't let me add comments yet. Therefore, upvotes are appreciated :)
It's just better to use JPanel as you may want multiple Panels in one frame each with different animations.
Yes, you could do it with JFrame as well.
i wanted to ask, if somebody might have a solution about a problem i face. I am working at an application, which draws an animation - for instance a map with objects moving onto. My problem is, that on top of the drawing, a Jtable, Jlist as well as other Components are also placed.
In my particular example all of those components have been added to the Panel, which holds the map. In result each component gets redrawn as often as good my fps is. Therefore making one of the tables invisible reduces the already high cpu usage of sometimes around 50% to less than 30%.
My question is, how can i avoid calling somewhat static visual contents paintComponent() method, without having the "background" - the map - whited out the menu.
Since the animation redraws permanently the menu is not shown at all, if its separated from the corresponding JPanel.
First thoughts move into following directions:
Clipping - actually not as good as i would like to, since id like to enable moving around the menus.
JLayeredPane - already tried but seemed to turn out, that the paintComponent method of menus still gets called frequently.
JWindow/Internal Frame - had that thought a couple of minutes ago. Having a complete independent container shall be able to handle my regard, or?
I am looking forward, if somebody has an elegant idea, how to fix that and reduce the cpu usage significantly.
Thanks!!
Best regards.
I would create a custom Shape for clip. Use Area class and subtract from the Area all the children components' bounds.
For painting over JComponent(s) placed into JPanel you have look at
JLayer (Java7) based on JXLayer(Java6)
GlassPane, notice all JComponents must be lightweight, otherwise is GlassPane behind heavyweight (J)Components
is possible painting to the JViewport,
EDIT
you have to use Swing Timer for moving with Icon (in the JLabel) placed into JXLayer, GlassPane or JViewport, don't use Runnable#Thread and never, not by using plain Thread.sleep(int)
I am writing in java some kind of application and i have to place something in front of another objects( in Z order)
I know that that i should use JLayeredPane but in fact i am not very familiar with it.
My idea is to make two JPanel's with different Z-order factors while inserting them to JLayeredPane.
i pasted my code http://www.wklejto.pl/130038
i would be very grateful if you tell me what is wrong because i am doing it for a long of time with no effect.
I don't see anything wrong with this code. Maybe you're trying to paint a transparent (not opaque) JPanel (e.g. with message) on top of the underlying base JPanel.
In that case you should invoke setOpaque(false) on your front JPanel.
JPanel second = new JPanel();
second.setOpaque(false);
second.add(new JLabel("message"));
jlp.add(second, new Integer(300));
JPanels are opaque by default - on the other hand JLabels aren't.
And take a look into tutorial.
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