How to use JDialog in Java, Lecture notes of Object Oriented Programming

Its my self made tutorial pdf for students help

Typology: Lecture notes

2020/2021

Uploaded on 07/01/2021

soul-recto
soul-recto 🇵🇰

5

(1)

6 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download How to use JDialog in Java and more Lecture notes Object Oriented Programming in PDF only on Docsity!

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,

boolean modal, GraphicsConfiguration gc)

This creates a new dialog with

an 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

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

enabled)

Sets whether calls to add

and setLayout are

forwarded to the

contentPane.

Transfer

Handler

getTransferHandler() Gets the transfer handler

property.

void repaint(long time, int x, int y, int width,

int height)

Repaints the specified

rectangle 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

// 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

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

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