Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » How to set background image of a JFrame in Spring Designer/Application Window with eclipse
How to set background image of a JFrame in Spring Designer/Application Window with eclipse [message #1831123] Tue, 11 August 2020 20:36 Go to next message
Eclipse UserFriend
Dear experts,
I have a class, type: WindowBuilder/Spring Designer/Application Window.
I need a background image, and based on this image, I will add a lot of buttons, text fields. My code to set background image is:
frame.setBackground(Color.WHITE);
frame.setBounds(1, 1, 788, 655);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null); //have to use absolute layout.
JPanel contentPane = new JPanel() {
public void paintComponent(Graphics g) {
URL url = null;
try {
url = new URL("http://localhost:8080/hayward_shop.png");
Image img = Toolkit.getDefaultToolkit().getImage( url);
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
} catch(Exception e) {
System.out.println(e.toString());
}
}
};
contentPane.setBorder(new EmptyBorder(1, 1, 1, 1));
contentPane.setBounds(1, 1, 788, 655);
frame.setContentPane(contentPane);
contentPane.setLayout(null);

When I run the class (run as Java Application in Eclipse), I can see the image, and buttons I added . but in code design mode, I cannot see this background image. Please tech me how to make it visible in design mode.
Thank you in advance.
Eric

[Updated on: Wed, 12 August 2020 11:59] by Moderator

Re: How to set background image of a JFrame in Spring Designer/Application Window with eclipse [message #1831160 is a reply to message #1831123] Wed, 12 August 2020 12:35 Go to previous messageGo to next message
Eclipse UserFriend
I find an answer at https://stackoverflow.com/questions/30160133/i-want-to-add-background-image-in-jframe
There is a defect with this approach. After I add a JButton into the JFrame, it disappears, To make it appear, I have to go back to Source code, move my new buttons' definition ahead of the code that add the background image object in the JFrame.
In case you have solution for this defect, or have a better resolution for my request, please help.
Thanks,

Eric

[Updated on: Wed, 12 August 2020 13:25] by Moderator

Re: How to set background image of a JFrame in Spring Designer/Application Window with eclipse [message #1848981 is a reply to message #1831160] Tue, 28 December 2021 02:01 Go to previous message
Eclipse UserFriend
I'm happy that I found this thread. Thank you, Eric.

When I went to source code and did exactly as you said - everything has started to work as it should. Honestly, I've always had these issues with the background images because I didn't know all the features of the JFrame and Spring Designer app. I even had to use the background remover to make the images less wide, to cut their size, and etc. Sometimes it helped me, but I'm glad I know the real solution to the problem now. Thank you once again!

[Updated on: Thu, 30 December 2021 02:13] by Moderator

Previous Topic:How to open a generated file
Next Topic:How does IParameterValues Interface work?
Goto Forum:
  


Current Time: Tue Apr 15 02:05:51 EDT 2025

Powered by FUDForum. Page generated in 0.03342 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top