Tushar Agarwal
Tushar Agarwal

Reputation: 511

Swing JDialog size according to parent frame

I am creating a dialog and setting its size as

setSize(m_appview.getSize());

where m_appview is a JFrame that the dialog will appear above. This JFrame holds a JButton that when pressed will display the dialog. Now when I maximize the frame and the click the button the dialog opens up with a proper width that matches that of the frame, but with a height that is less than that of the JFrame.

What to do?

Upvotes: 1

Views: 3063

Answers (3)

User Not Exist
User Not Exist

Reputation: 109

To set JDialog size and location same as parent frame.

dialog.setBounds(this.getBounds());

Upvotes: 1

camickr
camickr

Reputation: 324098

Works fine for me when using this code in the ActionListener.

button.addActionListener( new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        JButton button = (JButton)e.getSource();
        Window window = SwingUtilities.windowForComponent(button);
        JDialog dialog = new JDialog();
        dialog.setBounds( window.getBounds() );
        dialog.setVisible(true);
    }
});

If you need more help then post your SSCCE that demonstrates the problem.

Upvotes: 3

mKorbel
mKorbel

Reputation: 109815

for example

enter image description here or enter image description here

from code

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class JFrameAndJDialog {

    private JFrame frame = new JFrame("PopupFactory Sample");
    private JDialog dialog = new JDialog();
    private JButton start1 = new JButton("Pick Me for Popup");
    private JButton start2 = new JButton("Close this Popup");
    private Point pnt = null;
    private Dimension dim = null;

    public JFrameAndJDialog() {
        start2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(false);
            }
        });
        dialog.add(start2, BorderLayout.SOUTH);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setUndecorated(true);
        dialog.setVisible(false);
        start1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dim = frame.getSize();
                pnt = frame.getLocationOnScreen();
                int x = dim.width - 8;
                int y = dim.height - 8;
                dialog.setSize(x, y);
                x = pnt.x + 4;
                y = pnt.y + 4;
                dialog.setLocation(x, y);
                visibleForJDialog();
            }
        });
        frame.add(start1, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(150, 150);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    private void visibleForJDialog() {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                dialog.setVisible(true);
            }
        });
    }

    public static void main(final String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrameAndJDialog uPF = new JFrameAndJDialog();
            }
        });
    }
}

Upvotes: 3

Related Questions