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 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

Previous Topic:alternate APIs for Deprecated APIs
Next Topic:eclipsec.exe command line
Goto Forum:
  


Current Time: Mon Jan 25 18:10:51 GMT 2021

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

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

Back to the top