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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:35:32+00:00 2026-06-09T07:35:32+00:00

Please have a look at the following code Main.Java import java.awt.event.*; import java.awt.*; import

  • 0

Please have a look at the following code

Main.Java

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


public class Main extends JFrame
{
    private JButton ok;

    public Main()
    {
        ok = new JButton("OK");
        ok.addActionListener(new ButtonAction());

        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        panel.add(ok);

        getContentPane().add(panel,"South");

        this.setVisible(true);
        this.setSize(new Dimension(200,200));
        this.validate();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new Main();
        }
        catch(Exception e)
        {

        }
    }

    private class ButtonAction implements ActionListener
    {
        public void actionPerformed(ActionEvent ae)
        {
            Dialog d = new Dialog();
            d.setVisible(true);
        }
    }

}

Dialog.java

import java.awt.Event;
import java.awt.*;
import javax.swing.*;


public class Dialog extends JDialog
{
    private JButton done;

    public Dialog()
    {
        done = new JButton("Done");

        this.add(done);

        this.setSize(new Dimension(400,200));
    }

}

In here, I want to “attach” the Dialog form to the main form. Which means, when I click the OK button in Main.Java, Dialog form will get attached to the right side of the main form. So, when I move the main form, the dialog also get moved. However, dialog form should be independent, which means, when I click “x” button in dialog form, only that form exists, not the main form.

How can I attach this dialog form, to the right side of the main form, when the button is clicked? Please help!

  • 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-09T07:35:35+00:00Added an answer on June 9, 2026 at 7:35 am

    The answer is not MouseListener, but it is ComponentListener. I managed to do it with using that listener’s “componentMoved()” method.

    Main.java

    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    
    public class Main extends JFrame implements ComponentListener, ActionListener
    {
        private JButton ok;
        private Dialog dialog;
    
        public Main()
        {
            ok = new JButton("OK");
            ok.addActionListener(this);
    
            dialog = new Dialog();
    
            JPanel panel = new JPanel();
            panel.setLayout(new FlowLayout());
            panel.add(ok);
    
            getContentPane().add(panel,"South");
    
            this.addComponentListener(this);
    
            this.setVisible(true);
            this.setSize(new Dimension(200,200));
            this.validate();
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        public static void main(String[]args)
        {
            try
            {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                new Main();
            }
            catch(Exception e){}
        }
    
        public void actionPerformed(ActionEvent ae)
        {   
            dialog.setVisible(true);
        }
    
        @Override
        public void componentHidden(ComponentEvent arg0) {}
    
        @Override
        public void componentMoved(ComponentEvent arg0) 
        {
            int x = this.getX() + this.getWidth();
            int y = this.getY();
    
            dialog.setDialogLocation(x, y);
        }
    
        @Override
        public void componentResized(ComponentEvent arg0) {}
    
        @Override
        public void componentShown(ComponentEvent arg0) {}
    }
    

    Dialog.java

    import java.awt.Dimension;
    
    import javax.swing.JButton;
    import javax.swing.JDialog;
    
    
    public class Dialog extends JDialog
    {
        private JButton done;
    
        public Dialog()
        {
            done = new JButton("Done");
    
            this.add(done);
    
            this.setSize(new Dimension(400,200));
        }
    
        public void setDialogLocation(int x, int y)
        {
            this.setLocation(x, y);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please have a look the following code import javax.swing.*; import java.awt.event.*; import java.awt.*; public
Please have a look at the following code import java.awt.event.*; import javax.swing.*; import java.awt.*;
Please have a look at the following code import java.awt.*; import java.awt.event.*; import javax.swing.*;
Please have a look at the following code. import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.*;
Please have a look at the following code import java.awt.*; import javax.swing.JTable; import javax.swing.*;
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
Please have a look at the following code. package normal; import java.awt.*; import java.awt.event.*;
Please have a look at the following code import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout;
Please have a look at the following code package Euler; import java.util.ArrayList; import java.util.List;

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.