Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9324763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:40:49+00:00 2026-06-19T04:40:49+00:00

JButton and JLabel disappears when adding custom background. I don’t see any problems in

  • 0

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T04:40:50+00:00Added an answer on June 19, 2026 at 4:40 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to extend JButton with Clojure, but I ran into a problem when
I'm try to change the text on a JLabel when the Jbutton is clicked
I've create a program with a JButton(image) and a normal image, now if you
I am adding an image into a JLabel and trying to set the width
I have a JTabbedPane with a custom tab component. That component contains a JLabel
I am trying to create dynamic JButton and JLabel based on the number of
What is the best way to add a background image to a JPanel/JLabel when
As you can see in the following code, I am accessing a JLabel from
I have a JPanel and JButton on the JFrame . on runtime add JLabel
import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.DriverManager; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.