I'm new to java swing and I've been spending two days already on how to make a dynamically resizing background for jframe using a jlabel. I've searched a lot of videos on youtube and tried different suggestions here in stackoverflow but I did not succeed.
So here's the situation. I made a GUI using the "Design View" in Netbeans. The first component that I have (below jframe) is a jSplitPane (I made this transparent). Inside this split pane is many many other components. I succeeded in making a background image using the jlabel but it won't dynamically resize with the jframe (when i resize the window).
Here's a sample code of how I did this:
// This is the constructor.
public MyGUI() {
initComponents(); //auto-generated code where the many many other components are made
/*
* other methods called here (background is not affected by these)
*/
setBackgroundImage();
}
private void setBackgroundImage(){
JLabel background = new JLabel();
setContentPane(background);
background.add(mySplitPane); // this is the jSplitPane I mentioned in my explanation
BufferedImage image = null;
try {
image =ImageIO.read(new File("C:\\Users\\Documents\\vinylRecord.png"));
}
catch (IOException e) {
e.printStackTrace();
}
background.setIcon(new ImageIcon(image));
}
Note: I can't post every part of my code but the setBackgroundImage() is exactly what it is. Please tell me if you need to see other parts of it. Thanks!