Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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] Wed, 12 August 2020 00:36 Go to next message
Eric Zuo is currently offline Eric ZuoFriend
Messages: 14
Registered: October 2014
Junior Member
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 15:59]

Report message to a 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 16:35 Go to previous messageGo to next message
Eric Zuo is currently offline Eric ZuoFriend
Messages: 14
Registered: October 2014
Junior Member
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 17:25]

Report message to a 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 07:01 Go to previous message
anita tukci is currently offline anita tukciFriend
Messages: 2
Registered: September 2021
Junior Member
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 07:13]

Report message to a moderator

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


Current Time: Tue Jul 05 12:36:53 GMT 2022

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

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

Back to the top