You are on page 1of 15

Object Oriented Programming

Tutorial On JDialog Class

Presented By: Group#6


JDialog Class (JAVA)
OBJECTIVES:
Since this tutorial has been designed for novices to help them learn the fundamental
functioning of JAVA Swing , Jdialog Pane and their structure in Java, the primary goals of
this research effort are as follows:

 Understand how to create a dialog box based on the JDialog class.

 Understand the interface provided by the dialog class that allows the
class using the dialog to know what operations a user performed.

 Understand how a data model is used to transfer information between


the dialog and the main program.

The program you will work with implements a simple inventory system for Bart's
Butterfinger bar collection. The user can add and remove bars from the collection.
For this exercise, you will complete the functionality of adding a new bar to the
collection by completing the implementation of a dialog box.

CLASS NAME:
The class name for this tutorials discussion is “JDialog.”

INTRODUCTION:
JDialog class works as a general-purpose dialog which is used as a base to have
multiple lightweight components. The superclass of JDialogs is java.awt.Dialog.
JRootPane is its container and so provides a default window “close” button without
re-sizable buttons. It uses a JRootPane as its container, and it provides default
window-closing behavior. Since JDialog extends java.awt.Dialog, it has a
heavyweight peer and is managed by the native windowing system . JDialog class
can be summarized as summation 3 containers:
 Windows Constants + Root Pane Container +Dialog in java.awt ->
JDialog in Swings
JDialog Declaration:
public class JDialog extends Dialog implements WindowConstants, Accessible,
RootPaneContainer

CLASS HIERARCHY:

CONSTRUCTORS:

Sr. NAME Description


1 JDialog()  Creates an empty dialog without any title
or any specified owner
2 JDialog(Frame o)  Creates an empty dialog with a specified
frame as its owner
3 JDialog(Frame o, String s) Creates an empty dialog with a specified
frame as its owner and a specified title
4 JDialog(Window o)  Creates an empty dialog with a specified
window as its owner
5 JDialog(Window o, String t)  creates an empty dialog with a specified
window as its owner and specified title.
6 JDialog(Dialog o)  Creates an empty dialog with a specified
dialog as its owner
7 JDialog(Frame owner, String title, This creates a new dialog with an owner
boolean modal, GraphicsConfiguration gc) dialog, dialog title, Boolean value for a
modal setting (0 or 1) and graphics
configuration settings.
8 JDialog(Dialog o, String s)  Creates an empty dialog with a specified
dialog as its owner and specified title.

Functions/Methods:

Modifier And Methods Description


Type
void setLayout(LayoutManager m) Sets the layout of the dialog to
specified layout manager
void setJMenuBar(JMenuBar menu) Sets the menubar of the dialog to
specified menubar
Protected void add(Component c) Adds component to the dialog
void remove(Component c) Removes the specified component
c from the container.
static boolean isVisible(boolean b) Sets the visibility of the dialog, if
value of the boolean is true then
visible else invisible.
JMenuBar getJMenuBar() Returns the menubar set on this
dialog.
void update(Graphics g) Calls the paint(g) function
static void setDefaultLookAndFeelDecorated Provides a hint as to whether or
(boolean defaultLookAndFeelDecorated) not newly created JDialogs
should have their Window
decorations (such as borders,
widgets to close the window,
title...) provided by the current
look and feel.
Graphics getGraphics() Returns the graphics context of
the component.
JLayeredPane getLayeredPane() Returns the layered pane for the
dialog
void setContentPane(Container c) Sets the content pane for the
dialog.
void setLayeredPane(JLayeredPane l) Set the layered pane for the
dialog
Protected void setRootPane(JRootPane r) Sets the rootPane for the dialog.

protected void dialogInit() Called by the constructors to init


the JDialog properly
JMenuBar getJMenuBar() Returns the menubar of the
component
void setTransferHandler(TransferHandler n ) Sets the transferHandler
property, which is a mechanism
to support transfer of data into
this component.
protected void setRootPaneCheckingEnabled(boolean Sets whether calls to add and
enabled) setLayout are forwarded to the
contentPane.
Transfer Handler getTransferHandler() Gets the transfer handler property.
void repaint(long time, int x, int y, int width, Repaints the specified rectangle
int height) of this component within time
milliseconds.
void setGlassPane(Component glass) Sets the glass pane property of
Dialog.
protected void addImpl(Component co, Object c, int i) Adds the specified child
Component to the dialog.

Examples:
Example1:Write a program in Java to create simple JDialog Box.
Program
package Employe;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author Abuzar sheikh
*/
class test extends JFrame implements ActionListener {
static JFrame frame;
public static void main(String[] args)
{
frame = new JFrame("JFrame");
test t = new test();
JPanel panel = new JPanel();
JButton button = new JButton("click here to see dialog box");
button.addActionListener(t);
panel.add(button);
frame.add(panel);
frame.setSize(400, 400);
frame.show();
}
@Override
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("click here to see dialog box")) {
JDialog dialog = new JDialog(frame, "JDialog Box");
JLabel lab = new JLabel("This is a dialog box inside frame..");
dialog.add(lab);
dialog.setSize(300, 300);
dialog.setVisible(true);}
}

Output

Example2:Write a program which can create a Jdialog box within


another Dialog.
Program
// java Program to create a dialog within a dialog
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solve extends JFrame implements ActionListener {

// frame
static JFrame f;

// dialog
static JDialog d, d1;

// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("frame");

// create a object
solve s = new solve();

// create a panel
JPanel p = new JPanel();

JButton b = new JButton("click");

// add actionlistener to button


b.addActionListener(s);

// add button to panel


p.add(b);

f.add(p);

// set the size of frame


f.setSize(400, 400);

f.show();
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("click")) {
// create a dialog Box
d = new JDialog(f, "dialog Box");

// create a label
JLabel l = new JLabel("this is first dialog box");

// create a button
JButton b = new JButton("click me");

// add Action Listener


b.addActionListener(this);

// create a panel
JPanel p = new JPanel();

p.add(b);
p.add(l);

// add panel to dialog


d.add(p);

// setsize of dialog
d.setSize(200, 200);

// set visibility of dialog


d.setVisible(true);
}
else { // create a dialog Box
d1 = new JDialog(d, "dialog Box");

// create a label
JLabel l = new JLabel("this is second dialog box");

d1.add(l);

// setsize of dialog
d1.setSize(200, 200);

// set location of dialog


d1.setLocation(200, 200);

// set visibility of dialog


d1.setVisible(true);
}
}
}

Output
Example3:Write a program which can add components to Jdialog
box.

Program
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Employe;
// java Program to create a dialog within a dialog
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Label;
/*from w ww.j a v a2 s . c o m*/
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;

public class Main {

private final JDialog dialog = new JDialog();


private final String colorNames[] = { "Red: ", "Green: ", "Blue: " };
private final Label labels[] = new Label[3];
private final JSlider slider[] = new JSlider[3];
private final Label lb;

public Main() {
JPanel sliderPanel = new JPanel();
sliderPanel.setLayout(new GridLayout(0, 1));
for (int i = 0; i < slider.length; i++) {
labels[i] = new Label(colorNames[i] + 255);
sliderPanel.add(labels[i]);
slider[i] = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 255);
slider[i].setMinorTickSpacing(10);
slider[i].setMajorTickSpacing(50);
slider[i].setPaintTicks(true);
slider[i].setPaintLabels(true);
sliderPanel.add(slider[i]);
}
lb = new Label("Colour");
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setModal(true);
dialog.add(sliderPanel, BorderLayout.CENTER);
dialog.add(lb, BorderLayout.SOUTH);
dialog.pack();
dialog.setLocation(200, 200);
dialog.setTitle("Colour Dialog");
dialog.setVisible(true);
}

public static void main(String args[]) {


Main colourDialog = new Main();
}}

Output
Example4:Write a program to create a dynamic fit table with Edit
Text Event in Jdialog box.

Code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Employe;
// java Program to create a dialog within a dialog
import java.awt.Dimension;
import java.awt.GridLayout;
/*www. j a v a 2 s . co m*/
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Main {


static int ROWS = 3;

Main() {
Object[][] data = { { "A", "B", "Snowboarding", 5},
{ "C", "D", "Pool", 10} };
Object[] columnNames = { "firstname", "lastname", "age" };
final JTable table = new JTable(data, columnNames) {
@Override
public Dimension getPreferredScrollableViewportSize() {
Dimension d = getPreferredSize();
int n = getRowHeight();
return new Dimension(d.width, (n * ROWS));
}
};
JPanel jPanel = new JPanel();
jPanel.setLayout(new GridLayout());
JScrollPane sp = new JScrollPane(table);
jPanel.add(sp);
JDialog jdialog = new JDialog();
jdialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jdialog.setContentPane(jPanel);

jdialog.pack();
jdialog.setVisible(true);
}

public static void main(String[] args) {


Main main = new Main();
}
}

Output

Example5:Write a program to create a Dialog box with some text in


it and it gets a value From dialog without closing it.
Code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Employe;
// java Program to create a dialog within a dialog
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class Main extends Box {


JFrame frame;
JTextArea text = new JTextArea("this is a test");

public Main(JFrame frame) {


super(BoxLayout.Y_AXIS);
this.frame = frame;
add(text);
JButton button = new JButton("Click Me");
button.addActionListener(e -> launchDialog());
add(button);
}

public void launchDialog() {


JButton findButton = new JButton("Find");
findButton.addActionListener(e -> {
int start = text.getText().indexOf("is");
int end = start + "is".length();
if (start != -1) {
text.requestFocus();
text.select(start, end);
}
});
JButton cancelButton = new JButton("Cancel");

JOptionPane optionPane = new JOptionPane("Do you understand?",


JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION, null, new Object[] { findButton,
cancelButton });

JDialog dialog = new JDialog(frame, "Click a button", false);


cancelButton.addActionListener(e -> dialog.setVisible(false));
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.setLocation(100, 100);
dialog.pack();
dialog.setVisible(true);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new Main(frame));
frame.pack();
frame.setVisible(true);
}
}

Output

Exercise Questions
Q.1: What is difference between JFrame and JDialog?
Q.2: Create a program for JDialog in which you list the items and can add new
records/items at runtime.
Q.3: Write a program in which you can add a question with multiple choice
options using JDialog.
Q.4: Can you create Dialog box with Text Field in it and your image inside it?
Answer this question with your code.
Q.5: Create a Dialog Box with JDialog class in which you create a reCAPTCHA
image system and if filled wrong then display an error message.
Thank You!
The End

You might also like