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

The Archive Base Latest Questions

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

void NewJDialogcallone(JFrame frame) { location = frame.getLocationOnScreen(); int x = location.x; int y =

  • 0
void NewJDialogcallone(JFrame frame) 
{
    location = frame.getLocationOnScreen();
    int x = location.x;
    int y = location.y;
    dialog.setLocation(x, y);
    dialog.setLocationRelativeTo(frame);
    dialog.setVisible(true);
    dialog.setAlwaysOnTop(true);
    dialog.addComponentListener(this);
}

public void componentMoved(ComponentEvent e,?????) 
{
    JOptionPane.showConfirmDialog (null,
                                "This is the \"Ok/Cancel\"message dialog box.",
                                "",
                                JOptionPane.OK_CANCEL_OPTION);
}

I want to use the frame object so that the dialog box moves relative to the parent frame a.k.a. I move the parent frame and the dialog box moves with it.I want to call dialog.setLocationRelativeTo(//parent frame object//), which is possible only if I have the parent frame object.

If there is any way to get this window behaviour, please help me.

  • 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-04T17:45:19+00:00Added an answer on June 4, 2026 at 5:45 pm

    You just need to add the keyword final in front of the method parameter JFrame frame.

     void NewJDialogcallone(final JFrame frame) 
     ...
    

    I would also recommend to avoid using this:

    dialog.setAlwaysOnTop(true);
    

    because it is really annoying for the user experience. Usually, this is the sign that you did not properly instantiated your dialog, ie, by passing the correct Frame/Dialog owner.

    Here is an example of window location synchronization and without using setAlwayOnTop():

    import java.awt.Point;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;
    
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class Test {
    
        protected void initUI() {
            final JFrame frame = new JFrame();
            frame.setTitle("Test dialog synch");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                // On the next line I pass "frame" to the dialog so that the dialog never
                // goes behind the frame, avoiding the need for setAlwaysOnTop
            final JDialog dialog = new JDialog(frame, false);
            dialog.setSize(200, 50);
            frame.addComponentListener(new ComponentAdapter() {
    
                private Point lastLocation;
    
                @Override
                public void componentMoved(ComponentEvent e) {
                    if (lastLocation == null && frame.isVisible()) {
                        lastLocation = frame.getLocation();
                    } else {
                        Point newLocation = frame.getLocation();
                        int dx = newLocation.x - lastLocation.x;
                        int dy = newLocation.y - lastLocation.y;
                        dialog.setLocation(dialog.getX() + dx, dialog.getY() + dy);
                        lastLocation = newLocation;
                    }
                }
    
            });
            frame.setSize(400, 200);
            frame.setVisible(true);
            dialog.setLocationRelativeTo(frame);
            dialog.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new Test().initUI();
                }
            });
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

void test() { int buf[1000]; //populate buf foo(buf);//is this correct? Can we pass buf
void (int a[]) { a[5] = 3; // this is wrong? } Can I
void FileManager::CloseFile(File * const file) { for (int i = 0; i < MAX_OPEN_FILES;
void GasPump::dispense() { bool cont = true; char stop; do{ cout << Press any
void f(int){} typedef void (*f_ptr)(int); struct Functor{ void operator()(int){} }; struct X{ operator f_ptr(){
void child(int pid){ printf(Child PID:%d\n,pid); exit(0); } void parent(int pid){ printf(Parent PID:%d\n,pid); exit(0); }
void foo(int) { } class X { void foo() { } void bar() {
- (void) doSomething: (id)with { int a; a = [with doSomething]; } How does
void sort(int a[], int b[], int m, int n) { int e = m
void swap(int a[], int size){ ............... } int main(){ int x[4] = {4,5,3,12} ;

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.