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!

PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • 1
    In the ButtonAction's actionPerformed, you should set the newly created dialog's location to the corner of the main frame you wish for it to be connected to. – FThompson Aug 06 '12 at 18:14
  • I don't think it is the answer. That's the simplest thing possible. If the user moves the window, the dialog won't move – PeakGen Aug 06 '12 at 18:21
  • It's a start. Add a WindowListener to track location, and a ComponentListener to track size. – FThompson Aug 06 '12 at 18:25
  • It is highly recommended to call Swing related code on the EDT. Use `SwingUtilities.invokeLater(Runnable)` to do this. – Radu Murzea Aug 06 '12 at 18:36
  • @Vulcan: Hi, I found the answer. Have a look :) – PeakGen Aug 09 '12 at 06:39
  • 1
    Good job. Does it work if you resize your main frame though? ;) – FThompson Aug 09 '12 at 06:48
  • @Vulcan: Awesome remind:). There is another method resizable. However, the original app is not allowed to resize :) – PeakGen Aug 09 '12 at 13:54

2 Answers2

2

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);
    }

}
Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
PeakGen
  • 21,894
  • 86
  • 261
  • 463
0

I'm not aware of any built-in function that you can just say "dialog.moveWithThisOtherWindow(otherWindow)" or some such and it just happens. You would have to write code to do this yourself.

Create a mouse listener or mouse adapter on the parent form. In the "mouse moved" event in the mouse listener, move the child form. Of course the parent would have to have a handle to the child. Depending how you create the windows, you may need some sort of "register" function that the child can call to identify himself to the parent.

Jay
  • 26,876
  • 10
  • 61
  • 112
  • "*you may need some sort of "register" function that the child can call to identify himself to the parent.*" This is why the constructor `JDialog (Window owner)` exists. – Radu Murzea Aug 06 '12 at 18:34
  • I was thinking that the child window in this "stick together" sense may not also a child in the built-in "modality and close together" sense. I suppose I should have been more explicit. – Jay Aug 06 '12 at 18:39
  • OK, but how to get the location of the right corner? this.getX(), this.getY() returns the left side location right? So, I have to add the width to the getX() to get the right side X. hight of the window to get the right side, y. Isn't it? – PeakGen Aug 06 '12 at 19:01
  • 1
    @Yohan Yes, something like that. You have to get the location of the window and add its width/height to the corresponding coordinates, depending on which right corner you want. After that, you must write a `MouseAdapter` and attach it to the main frame. When the user drags the frame, recalculate the window's position (inside that `MouseAdapter`) and re-position the dialog. Try it out. If you get stuck, come back and we'll try to figure it out. – Radu Murzea Aug 06 '12 at 19:12
  • @SoboLAN: Hi, I found the answer. Have a look :) – PeakGen Aug 09 '12 at 06:40
  • @Yohan Interesting, didn't know that it would work with a `ComponentListener`. It's probably not perfect but if it works and you're satisfied, good for you :) . PS: Execute GUI-related code on the EDT, not on the main thread. This is very important. – Radu Murzea Aug 09 '12 at 07:22
  • @Yohan See `SwingUtilities.invokeLater(Runnable)`. You pass to that method a `Runnable` containing your GUI code. PS: I found an older answer of mine where this can be seen: http://stackoverflow.com/questions/9533072/add-a-title-to-a-jtable/9533238#9533238 . Notice the `SwingUtilities.invokeLater` call in the `main` method. Also, see this link: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html – Radu Murzea Aug 09 '12 at 14:35
  • @SoboLAN: Thanks for the reply. No prob I guess. I am aware of that method :) – PeakGen Aug 09 '12 at 14:37