Search code examples
javaswingjframejbuttonjlabel

JButton and JLabel dissappears when adding custom background


JButton and JLabel disappears when adding custom background. I don't see any problems in my program, but maybe you guys find an solution! I think it's only a little thing I forgot, but I can't figure it out.

Here's the code:

GameWindow.java:

setContentPane(new StartImagePanel(RollrackLogo));
out.println("adding JLWelcome");
JLWelcome.setText("Welcome to Rollrack, " + namewindow.name);
add(JLWelcome);
JLWelcome.setVisible(true);
out.println("JLWelcome added");
out.println("adding JBRandom");
JBRandom.setText("Random");
add(JBRandom);
JBRandom.setVisible(true);
out.println("added JBRandom");

The background appears perfect, but not the JButton and JLabel!

Code to the StartImagePanel.java:

public class StartImagePanel extends JComponent{
    private Image image;
    public StartImagePanel(Image image) {
        this.image = image;
    }
    @Override
    protected void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, null);
    }
}

Solution

  • Your button and label are added to your GameWindow frame while they should be added to its contentPane, setContentPane(new StartImagePanel(RollrackLogo)); instead. That's why they are not showing, they are added to the frame.

    Make a variable of the StartImagePanel and add the button and label to it and they should show up.

    StartImagePanel contentPanel = new StartImagePanel(RollrackLogo);
    setContentPane(contentPanel);
    
    ...
    
    out.println("adding JLWelcome");
    JLWelcome.setText("Welcome to Rollrack, " + namewindow.name);
    contentPanel.add(JLWelcome);
    JLWelcome.setVisible(true);
    out.println("JLWelcome added");
    out.println("adding JBRandom");
    JBRandom.setText("Random");
    contentPanel.add(JBRandom);
    JBRandom.setVisible(true);
    out.println("added JBRandom");
    

    Answer dispute

    The claims in the first paragraph are plain wrong. Here is source that proves it.

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    
    public class AddToCustomContentPane {
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
    
                @Override
                public void run() {
                    // the GUI as seen by the user (without frame)
                    JPanel gui = new JPanel(new FlowLayout());
                    gui.setBorder(new EmptyBorder(2, 3, 2, 3));
                    gui.setBackground(Color.RED);
    
                    JFrame f = new JFrame("Demo");
                    f.setContentPane(gui);
    
                    // Acid test.  Can we add buttons direct to the frame?
                    f.add(new JButton("Button 1"));
                    f.add(new JButton("Button 2"));
    
                    // Ensures JVM closes after frame(s) closed and
                    // all non-daemon threads are finished
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    // See http://stackoverflow.com/a/7143398/418556 for demo.
                    f.setLocationByPlatform(true);
    
                    // ensures the frame is the minimum size it needs to be
                    // in order display the components within it
                    f.pack();
                    // should be done last, to avoid flickering, moving,
                    // resizing artifacts.
                    f.setVisible(true);
                }
            };
            // Swing GUIs should be created and updated on the EDT
            // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
            SwingUtilities.invokeLater(r);
        }
    }
    

    Edit after the custom panel code was given

    Here's a snippet that works to show both button and label on a black image background, I removed that was not needed (listeners).

    public static void main(String[] v) {
    
    class StartImagePanel extends JPanel {
      private Image image;
      public StartImagePanel(Image image) {
          this.image = image;
      }
      @Override
      protected void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, null);
      }
    }
    
    class GameWindow extends JFrame{
      public GameWindow() {
        BufferedImage RollrackLogo;
        RollrackLogo = new BufferedImage(400,200,BufferedImage.TYPE_INT_RGB);
        final JButton JBRandom = new JButton();
        final JLabel JLWelcome = new JLabel();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        StartImagePanel panel = new StartImagePanel(RollrackLogo);
        setContentPane(panel);
        setExtendedState(MAXIMIZED_BOTH);
        setVisible(true);
        JLWelcome.setText("Welcome to Rollrack");
        panel.add(JLWelcome);
        JLWelcome.setVisible(true);
        JBRandom.setText("Random");
        panel.add(JBRandom);
        JBRandom.setVisible(true);
      }
    }
    
    GameWindow window = new GameWindow();
    window.pack();
    window.setVisible(true);
    }