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

  • Home
  • SEARCH
  • 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 7945333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:49:45+00:00 2026-06-04T00:49:45+00:00

I have a JButton and pressing it opens a modal dialog. When I close

  • 0

I have a JButton and pressing it opens a modal dialog. When I close the dialog, the button still look like it is still in a pressed state until I move the mouse. I think this is happening because the JDialog is opened on top of an AWT component (it’s a 3rd party component that uses an AWT Canvas, and I can’t change that). If I open the dialog and close it over a swing component, then it works properly.

I’ve tried adding a window listener to the dialog and repainting the entire frame (using repaint and paintImmediately) when the dialog closes, but this doesn’t work. Any suggestions on how to fix this?

Here is an SSCCE example. Press the button and then close the dialog. Notice the focus border is still around the button until moving the mouse over the green section.

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class DialogExample
{

    public static void main(String[] args) throws Exception
    {

        final JFrame jf = new JFrame("AWT/Swing Dialog Test");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton button = new JButton("show dialog");
        final Canvas canvas = new Canvas() {
            @Override
            public void paint(Graphics g)
            {
                g.setColor(Color.RED);
                g.fillRect(0, 0, getWidth(), getHeight());
            }
        };
        canvas.setPreferredSize(new Dimension(200,200));

        button.setAction(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                JDialog dlg = new JDialog(jf,"modal");
                dlg.add(new JLabel("hello"));
                dlg.setModal(true);
                dlg.pack();
                dlg.setLocationRelativeTo(canvas);
                dlg.setVisible(true);
            }            
        });
        button.setText("press me");
        button.setFocusable(false);
        JPanel panel = new JPanel();
        panel.setBackground(Color.GREEN);        
        panel.setPreferredSize(new Dimension(200, 200));
        jf.setLayout(new BorderLayout());
        panel.add(button);
        jf.add(panel, BorderLayout.NORTH);
        jf.add(canvas, BorderLayout.SOUTH);
        jf.setSize(new Dimension(400,400));
        jf.setVisible(true);
    }    
}
  • 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-04T00:49:47+00:00Added an answer on June 4, 2026 at 12:49 am

    As an expedient, you can clear the rollover state when the dialog returns in actionPerformed():

    @Override
    public void actionPerformed(ActionEvent e) {
        JDialog dlg = new JDialog(jf, "modal");
        dlg.add(new JLabel("hello"));
        dlg.setModal(true);
        dlg.pack();
        dlg.setLocationRelativeTo(canvas);
        dlg.setVisible(true);
        button.getModel().setRollover(false);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jTable and a jButton. When clicked, the button's actionPerformed method calls
Let's say I have JButton test = new JButton(Test Button); and I want to
I have a JButton that has a callback function like so: myJButtonCallBackFunc (ActEvtListener evt)
I need to listen to a Jbutton getting pressed however the implementation I have
Is there any way to make a JButton look like it has the input
I have a custom button class called ImageButton that extends JButton. In it i
I have a JButton that I would like to change the background color of
I have a parent JFrame it contain a JButton . functionality of that button
I have a class that extends JButton because the custom look and feel I'm
Lets say i have a a button somewhere in the code: JButton closeButton. I

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.