Adding a background image to JFrame using JLabel - java

I added a background image to a JFrame using a JLabel. But there are other JLabels in a panel added to the particular JFrame. After adding this background image all over the JFrame, other JLabels are not visible. How do I get them be visible?

Several options:
Make the JLabel opaque and make it the contentPane, giving it a decent layout, give it a decent getPreferredSize() override that makes sense in the context of your problem (JLabel's own override for this method won't work for you, since it depends on text and icons that the label holds and not on components added).
And then add other components to it -- to the JLabel.
Or:
Use a JPanel as the background image displayer
Display the image in its paintComponent(...) method override.
Give it decent layout manager(s) and if needed, a getPreferredSize() override.
Also:
Take care that some added components, such as other JPanels, are set to be non-opaque.

Related

How can I create responsive JList in Java Swing

I want to create a shop by having a main JPanel that each component inside it is a JPanel with an image, label and button.
I did tried using a JList but the problem with the JList its only holds the rendering of the component and because of that the button isn't working and its only an image. I can walk around and use MouseEvent but it feels wrong for me and I am sure that there is a better solution for it.
I want that the components will change their positions depend on the frame size, like in the JList.
For example, if I change from the width of the screen the positions of the components will change from this:
to this:
I do have an idea by using GridLayout or GridBagLayout in the paintComponent (because it calls every rendering. If you know another method that calls every rendering int the JPanel I would like to know) and changing the positions of the components by changing the layout variables inside the paintComponent.
I did surfed the internet to find a solution but I only found that people used JTable but I don't see it working here.
each component inside it is a JPanel with an image, label and button.
Makes sense.
changing the positions of the components by changing the layout variables inside the paintComponent.
The paintComponent() method has nothing to do with changing the layout of the panels. You should not be playing with the paintComponent() method.
I do have an idea by using GridLayout or GridBagLayout
You are correct to use a layout manager, but unfortunately, none of the default layout managers will wrap automatically at a random number of components.
The layout managers are invoked automatically as the frame is resized.
So you can use the Wrap Layout which is an extension to the FlowLayout that will allow random wrapping.

Some JPanel's backgrounds fade when I interact with page

As i wrote in the title, I've this problem with JPanel.
My page is structured as a mainJPanel, another JPanel inside the main (where I paint a background) and some component added to this background panel.
Everytime I interact with something (for example i click a button) the background of the components fade to a lighter version of themselves.
I really can't figure out why.
mainPanel have setOpaque(false) and setLayout(null);
backgroundPanel have setOpaque(false) and setLayout(null) (i painted the background when i create the class backgroundPanel that extends JPanel, with an override of the method paintComponent);
all the components that becomes lighter have setOpaque(true) and setLayout(null);
the background of this components are a custom grey (rgb(232,232,232)).
Thank you all
Using setOpaque(false) should not cause a problem (ie. JLabels are non opaque). It sounds like a painting problem when using transparent colors.
Check out Background With Transparency for an explanation of the problem and a couple of solutions.

How does a JLabel or JButton tell a JScrollPane that view size has changed?

How does a JLabel or JButton notify a JScrollPane that the view size has changed (for example when an icon has been set) so it can determine whether showing scrollbars are necessary?
How could I implement similar behaviour to display an image with a simple JPanel without resorting to the aforementionned components?
P.S: I've looked through the source code and so far all I see is that a Component is referred to as "view" and is passed on to a JView or JViewport which registers some listeners. From there on things seem unclear.
As noted in the JScrollPane API, unless you change the policy, "both horizontal and vertical scrollbars appear whenever the component's contents are larger than the view." Once pack() has sized the Window "to fit the preferred size and layouts of its subcomponents," any subsequent changes are seen by the scroll pane when the container is validated and repainted. See Painting in AWT and Swing for more.

Why does only the last JPanel I add to a JFrame resize?

So I am working on a GUI application using the swing framework. In short, I have 3 JPanels that act as different views of my application. Now the problem is that no matter the order I add the JPanels to my JFrame, only the final JPanel I add resizes when I switch to that view.
Some relevant bits of code:
When creating the window, I first create each individual JPanel, and add it to the JFrame:
JPanel newPanel = new SomeClassExtendingJPanel();
this.jframe.add(newPanel);
Next, whenever I switch between views of the application, I hide the panel that is currently active:
jframe.validate();
jframe.repaint();
oldPanel.setVisible(false);
And then activate the to be shown panel:
jframe.validate();
jframe.repaint();
newPanel.setVisible(true);
Does anyone know what could be wrong?
To resize the JFrame with each swap, you could call pack() on it, but this is kludgy having a GUI resize all the time. A better solution is to use the mechanism that Swing has for swapping views -- a CardLayout. This will size the container to the largest dimension necessary to adequately display all of the "card" components.
Check out the CardLayout tutorial and the CardLayout API for more on this.
JFrame's ContentPane has implemented BorderLayout by Default, and there is possible to put only one JComponents to the one Area,
you have to change used LayoutManager or put another JPanels to the other Areas

Resizing JLabel smaller than its icon?

I'm using a JPanel containing a JLabel with an icon. I'm using a ComponentAdapter on the JLabel to request a correctly sized thumbnail from the controller (using MVC pattern) when the JLabel is resized. This works fine when the JLabel is resized to be bigger than before, so then it's filled with an ImageIcon the size of the JLabel. However, when resizing the window to be smaller, it simply doesn't resize the JLabel at all (because of the icon's size, I'm assuming).
Is there possibly some layout manager or setting to make the JLabel disregard its content (the ImageIcon) and resize itself anyways? I mean, it can truncate text, so it ought to be able to just show part of the image when resized smaller.
Try experimenting with setPreferredSize() and setMinimumSize() to ensure the JLabel is allowing itself to shrink to the required size. You also need to double-check what kind of LayoutManager you are using and what constraints, if any, you use when adding your component to the panel. Try a different layout manager if you can. If none of that works, you will need to provide some example code.

Categories