Here is the JPanel class that is having the issue:
public class EntryDialog extends JPanel{
private static final long serialVersionUID = 1L;
MainWindow mainWindow;
Image background;
JButton continueresume;
JButton newresume;
JButton settings;
JButton exit;
public EntryDialog(MainWindow mainWindow){
continueresume = new JButton("Continue resume");
newresume = new JButton("New resume");
settings = new JButton("Settings");
exit = new JButton("Exit");
this.mainWindow = mainWindow;
this.background = Utilities.getImage("images\\entryDialogBackground.jpg", true);
this.setLayout(null);
//continueresume button
continueresume.setBounds(Utilities.getScaledWidth(1150),
Utilities.getScaledHeight(150),
Utilities.getScaledWidth(500),
Utilities.getScaledHeight(50));
//newresume button
newresume.setBounds(Utilities.getScaledWidth(1150),
Utilities.getScaledHeight(220),
Utilities.getScaledWidth(500),
Utilities.getScaledHeight(50));
//settings button
settings.setBounds(Utilities.getScaledWidth(1150),
Utilities.getScaledHeight(290),
Utilities.getScaledWidth(500),
Utilities.getScaledHeight(50));
//exit button
exit.setBounds(Utilities.getScaledWidth(1150),
Utilities.getScaledHeight(360),
Utilities.getScaledWidth(500),
Utilities.getScaledHeight(50));
this.add(continueresume);
this.add(newresume);
this.add(settings);
this.add(exit);
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if(background != null){
g2d.drawImage(background, 0, 0, this);
}
g2d.dispose();
}
This JPanel is being painted to a main frame in another class. What is happening is “background” is being painted just fine, but the buttons are not. The buttons appear once I hover over them with my mouse but until then they are invisible. I have searched for hours and not come to a solution so any help would be appreciated. Thanks!
I’d have to guess it’s happening in your paintComponent method. You’re calling super.paintComponent() will draw everything like usual including the buttons. Then you’re drawing over it with g2d.drawImage(). You should draw the background first.