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 8886101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:28:34+00:00 2026-06-14T21:28:34+00:00

Can someone explain why my components only get drawn when I hover over where

  • 0

Can someone explain why my components only get drawn when I hover over where they are supposed to be?

I setup a borderless frame than can be dragged anywhere and I’m trying to create an exit button at the top right but it doesn’t get drawn until I hover over it. I paint to the JFrame a background image, then draw my button and set the whole thing visible.

import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class GUI extends JFrame
{
    private Image Background = null;
    private static Point Offset = new Point();

    public GUI() {
        this.setUndecorated(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        AddListeners();
        SetCustomTheme();
        LoadBackground();
        Layout();
        pack();
        this.setSize(300, 300);
        this.setVisible(true);
    }

    private void Layout() {
        GroupLayout Info = new GroupLayout(this.getContentPane());
        this.getContentPane().setLayout(Info);
        JButton Button = new JButton();

        Info.setHorizontalGroup(
            Info.createSequentialGroup()
               .addComponent(Button)
         );

        Info.setVerticalGroup(
            Info.createParallelGroup()
                .addComponent(Button)
        );
    }

    private void SetCustomTheme() {
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
        }
    }

    private void LoadBackground() {
        try {
            Background = ImageIO.read(getClass().getResource("Images/meh.png"));
        } catch (Exception Ex) {

        }
    }

    private void SetCustomIcon() {
        Image Icon = Toolkit.getDefaultToolkit().getImage("Images/lol.jpg");
        setIconImage(Icon);
    }

    private void AddListeners() {
        this.addMouseListener(new MouseAdapter() {
            @Override public void mousePressed(MouseEvent e) {
              Offset.x = e.getX();
              Offset.y = e.getY();
            }
          });

        this.addMouseMotionListener(new MouseMotionAdapter() {
            @Override public void mouseDragged(MouseEvent e) {
              Point p = getLocation();
              setLocation(p.x + e.getX() - Offset.x, p.y + e.getY() - Offset.y);
            }
          });
    }

    @Override public void paint(Graphics g) {
        g.drawImage(Background, 0,0,this.getWidth(),this.getHeight(), 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-14T21:28:35+00:00Added an answer on June 14, 2026 at 9:28 pm
    1. All interactions with the UI must be executed from within the Event Dispatching Thread
    2. You should avoid extending from top level containers, like JFrame, instead, use JPanel instead.
    3. Failing to honor the paint chain contract is preventing any child components from begin painted
    4. The preferred method to override for performing custom painting is paintComponent

    You might like to have a read through

    • Painting in AWT and Swing
    • The Event Dispatch Thread
    • Performing Custom Painting

    Try something like this instead;

    public class BadPaint01 {
    
        public static void main(String[] args) {
            new BadPaint01();
        }
    
        public BadPaint01() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    Image Icon = Toolkit.getDefaultToolkit().getImage("Images/lol.jpg");
                    frame.setIconImage(Icon);
                    frame.setUndecorated(true);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new GUI());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public static class GUI extends JPanel {
    
            private Image Background = null;
            private static Point Offset = new Point();
    
            public GUI() {
                AddListeners();
                SetCustomTheme();
                LoadBackground();
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(300, 300);
            }
    
            private void Layout() {
                GroupLayout Info = new GroupLayout(this);
                setLayout(Info);
                JButton Button = new JButton();
    
                Info.setHorizontalGroup(
                        Info.createSequentialGroup()
                        .addComponent(Button));
    
                Info.setVerticalGroup(
                        Info.createParallelGroup()
                        .addComponent(Button));
            }
    
            private void SetCustomTheme() {
                try {
                    UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }
            }
    
            private void LoadBackground() {
                try {
                    Background = ImageIO.read(getClass().getResource("Images/meh.png"));
                } catch (Exception Ex) {
                }
            }
    
            private void AddListeners() {
                this.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        Offset.x = e.getX();
                        Offset.y = e.getY();
                    }
                });
    
                this.addMouseMotionListener(new MouseMotionAdapter() {
                    @Override
                    public void mouseDragged(MouseEvent e) {
                        Point p = getLocation();
                        setLocation(p.x + e.getX() - Offset.x, p.y + e.getY() - Offset.y);
                    }
                });
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
                g.drawImage(Background, 0, 0, this.getWidth(), this.getHeight(), null);
            }
        }
    }
    

    You also might like to have read through Code Conventions for the Java Programming Language, you’re not going to make any friends by ignoring them 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain the threading model in windows metro? I really get confusion about
Can someone explain this simple, yet deceiving, anomaly? There are two models, where B
Can someone explain me why this works: select val1, val2, count(case when table2.someID in
Can someone explain why, in this situation, scandir is getting my directory, but the
Can someone explain step by step how this query is processed? SELECT F.ID, F.FirstName,
Can someone explain why it's best use is for prototyping? Should it not be
Can someone explain how the View and ViewModel are connected? I can't find anywhere
Can someone explain how the organisation of classes in Pharo works in different versions
Can someone explain why unsigned int is taking a negative value? An unsigned int
Can someone explain the rules of casting, and when a conversion is ambiguous? I'm

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.