Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Creating a JDialog in Java Swings

In this lesson, you will learn.

  • Working with JDialog
  • Constructors and Methods of JDiaglog
  • Examples

 

What is a JDialog

  1. JDialog is a part of Java Swing and is used to create dialog windows.
  2. These are pop-up windows that can be modal or non-modal, meaning they can either block user input to other windows while open or allow interaction with other windows.

 

Constructors of JDialog Java

Constructor Description
JDialog() Creates an unowned, non-modal dialog without a title.
JDialog(Frame owner) Creates a non-modal dialog with the specified frame as its owner.
JDialog(Frame owner, boolean modal) Creates a dialog with the specified frame as its owner and modality.
JDialog(Frame owner, String title) Creates a non-modal dialog with the specified frame as its owner and the specified title.
JDialog(Frame owner, String title, boolean modal) Creates a dialog with the specified frame as its owner, the specified title, and modality.

 

Example-1: Creating a JDialog

package jdialog;

import javax.swing.*;

public class SimpleJDialogExample {
	
    private static void createAndShowGUI() {     
        // Create a simple JDialog
        JDialog dialog = new JDialog();
        dialog.setTitle("Simple Dialog");
        dialog.setSize(200, 150);
        dialog.setVisible(true);
    }

    public static void main(String[] args) {
        createAndShowGUI();
    }
}

Output

 

Example-2: Creating a JDialog on a JFrame

package jdialog;

import javax.swing.*;

public class JDialogOnFrame {
	private static void createAndShowGUI() {
		JFrame frame = new JFrame("Simple JDialog Example");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400, 300);
		frame.setVisible(true);

		// Create a simple JDialog
		JDialog dialog = new JDialog(frame, "Simple Dialog", true);
		dialog.setSize(200, 150);
		// Center the dialog relative to the frame
		dialog.setLocationRelativeTo(frame); 

		dialog.setVisible(true);
	}

	public static void main(String[] args) {
		createAndShowGUI();
	}
}

Output

 

Explanation

In this example:

  • A JFrame is created and made visible.
  • A JDialog is created with a title, set to be modal, and given a size.
  • The dialog is centered relative to the frame using setLocationRelativeTo(frame).
  • The dialog is made visible with setVisible(true).

 

Summary of Common Methods in JDialog

Method Description
add(Component comp) Adds the specified component to the container.
dispose() Releases all of the native screen resources used by this window, its subcomponents, and all of its owned children.
getDefaultCloseOperation() Returns the operation that occurs when the user initiates a “close” on this dialog.
getTitle() Returns the title of the dialog.
setDefaultCloseOperation(int operation) Sets the operation that will happen by default when the user initiates a “close” on this dialog.
setTitle(String title) Sets the title for this dialog.
setVisible(boolean b) Shows or hides this dialog depending on the value of parameter b.
isModal() Returns whether this dialog is modal.
setModal(boolean modal) Sets whether the dialog is modal.
pack() Causes this window to be sized to fit the preferred size and layouts of its subcomponents.

 

 

 

 

 


 

End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review