Jdialog Class (Java) : Objectives
Jdialog Class (Java) : Objectives
Jdialog Class (Java) : Objectives
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 the interface provided by the dialog class that allows the
class using the dialog to know what operations a user performed.
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
boolean modal, GraphicsConfiguration gc) owner 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:
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();
f.add(p);
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");
// create a panel
JPanel p = new JPanel();
p.add(b);
p.add(l);
// setsize of dialog
d.setSize(200, 200);
// create a label
JLabel l = new JLabel("this is second dialog box");
d1.add(l);
// setsize of dialog
d1.setSize(200, 200);
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 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);
}
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;
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);
}
Output
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