Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

PADMAWATI ACADEMY

CHATPER – 8
GUI DIALOGS AND TABLES
DIALOG
A Dialog is a small box separate sub window that appears to either provide or request information to
the
user. Java support following types of Dialogs.
1- JDialog
2- JOptionPane
3- JFileChooser
4- JColorChooser
1- JDialog It provides default window closing behavior with minimize, maximize and close icons. The
JDialog
creates a standard pop-window for displaying desired information related to your application.
2- JOptinPane It is very useful swing control. It allows to create popup windows (such as alerts or message
boxes or input box) with varied content. The dialogs created by JOptionPane are made up
of four basic elements.
(a) Icon (b) Message (c) Input area (d) Buttons
Using JOptionPane, four types of predefined dialogs can be created.
(i) Input dialog:- It displays a message and waits for input from the user.
(ii) Confirm dialog:- It displays a message and waits for the user response through button
selection.
(iii) Message dialog:- It displays a message and waits until the user acknowledges it by
clicking OK.
(iv) Option dialog:- It displays a message and waits for user response from a group of
options.
TABLES
Sometime related information is best represented through tabular data then we can use JTable component of
Swing API of Java. It enable you display data in a table and manipulate it in many ways. The most commonly
used property of JTable is model. JTable renders the data as a table while table-model organizes and manages
the source data for the JTable. When we use JTable in Java program then we have to add following line at the
top of the source editor.
Import javax.swing.table.*;

PROGRAMS
Q-1 Design an application that obtain names and marks of students via input dialogs and displays the obtained details in a
text area. After obtaining one student’s details, the details should be append to text area and then the application
should confirm whether to continue the process through a confirm dialog. This process should repeat as long as the
user presses YES button from the three choices: YES, NO and CANCEL of the confirm dialog. If user presses NO
then the details of the highest scorer should displayed through a message dialog and if user presses CANCEL button
then nothing should be displayed.

Note: Set Frame’s focusable window state property – true.


Ans:-

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public void process()


{
int ans=0;
String name="", hname="";
double perc=0.0, hperc=0.0;
do
{
name = JOptionPane.showInputDialog("Enter Student Name :");
perc = Double.parseDouble(JOptionPane.showInputDialog("Enter Percentage Marks :"));
jTextArea1.append((name+ "\t" + perc + "\n"));
if(hperc<perc)
{
hperc = perc;
hname = name;
}
ans = JOptionPane.showConfirmDialog(null,"More Students info??");
}
while(ans == JOptionPane.YES_OPTION);
if(ans == JOptionPane.NO_OPTION)
{
JOptionPane.showMessageDialog(null,"Highest scorer is" + hname + "with" + hperc + "marks.");
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
JoptionPaneDialog j1 = new JoptionPaneDialog();
j1.setVisible(true);
j1.process();
j1.setVisible(false);

}
});

}
Q-2 Create a GUI application that obtains details (rollno, name,class and marks) of a student through text fields and adds
them in a table as a row. The application should also offer options to display total count of records in the table : count
of records class-wise and to exit from the application.
Ans:-

import javax.swing.table.*;
import javax.swing.JOptionPane;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)


{
Object [] oary = {jTextField1.getText(), jTextField2.getText(), jTextField3.getText(), jTextField4.getText()};
DefaultTableModel tm = (DefaultTableModel)jTable1.getModel();
tm.addRow(oary);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{
JOptionPane.showMessageDialog(null,"Total Records are="+ jTable1.getRowCount());
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{
int r = jTable1.getRowCount();
Object first = jTable1.getValueAt(0, 2);
Object val = new Object();
int cnt = 1;
String msg = new String();
int i;
for(i=1; i<r; i++)
{
val = jTable1.getValueAt(i, 2);
if(val.equals(first))
cnt++;
else
{
msg = msg + "Class "+ jTable1.getValueAt(i-1,2)+"has "+ cnt+ "students.\n";
cnt = 1;
first = jTable1.getValueAt(i, 2);
}
}

msg = msg+"Class "+ jTable1.getValueAt(i-1,2)+"has "+ cnt+ "students.";


JOptionPane.showMessageDialog(null, msg);
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)
{
System.exit(0);
}

Q-3 Create a Java GUI application that lets you create an address book. The details to be added in Address Book are :
S.No, Name, Email ID, Phone.
Ans:-

import javax.swing.table.*;
import javax.swing.JOptionPane;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)


{
Object[] oary = {jTextField1.getText(), jTextField2.getText(), jTextField3.getText(), jTextField4.getText()};
DefaultTableModel tm = (DefaultTableModel)jTable1.getModel();
tm.addRow(oary);
int sn = Integer.parseInt(jTextField1.getText());
jTextField1.setText(""+(++sn));
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)


{
int r = jTable1.getRowCount();
JOptionPane.showMessageDialog(null, "Total Records in Address Book : "+r);
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)
{
System.exit(0);
}

Q-4 Add the search facility in the above program. That is ask the user which column he/she wants to search in and then
obtain search value. If found in the address book then report it otherwise report “not found”.
Ans:-

import javax.swing.table.*;
import javax.swing.JOptionPane;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)


{
Object[] oary = {jTextField1.getText(), jTextField2.getText(), jTextField3.getText(), jTextField4.getText()};
DefaultTableModel tm = (DefaultTableModel)jTable1.getModel();
tm.addRow(oary);
int sn = Integer.parseInt(jTextField1.getText());
jTextField1.setText(""+(++sn));
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)


{
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{
int r = jTable1.getRowCount();
JOptionPane.showMessageDialog(null, "Total Records in Address Book : "+r);
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)
{
System.exit(0);
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt)
{
int c = Integer.parseInt(jTextField5.getText());
int r = jTable1.getRowCount();
Object first = jTextField6.getText();
Object val = new Object();
int i;
for(i=0; i<r; i++)
{
val = jTable1.getValueAt(0, c);
if(val.equals(first))
{
JOptionPane.showMessageDialog(rootPane, "Exist in Record "+(i+1));
break;
}
}
if(i == r)
JOptionPane.showMessageDialog(rootPane, "Record with this value does not exist.");
}

Q-5 Design an application that does not have any text field or any other control to obtain input or show output, it just has a
button, yet it can obtain number of days from the user and display equivalent years, months and days.
Ans:-
import javax.swing.JOptionPane;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)


{
String days = JOptionPane.showInputDialog("Enter number of days");
int ds = Integer.parseInt(days);
int d = 0, m = 0, y = 0;
y = ds/365;
m = (ds % 365) / 30;
d = (ds % 365) % 30;
String msg = ""+ds+ "days are equivalent to \n" + y + "years" + m + "months" + d + "days approx.";
JOptionPane.showMessageDialog(null, msg);
}

Q-6 Design an application that has a table showing all records. User should be able to enter a record number and that one
record should be shown in another table in transposed manner, each column value should become a row.
Ans:-

import javax.swing.JOptionPane;
import javax.swing.table.*;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)


{
int rec = Integer.parseInt(jTextField1.getText());
int totrec = jTable1.getRowCount();
if(rec > totrec)
JOptionPane.showMessageDialog(null, "There are only" + totrec+ "records in the table");
else if(rec <= 0)
JOptionPane.showMessageDialog(null, "Record number cannot be zero or negative");
else
{
DefaultTableModel dm = (DefaultTableModel) jTable2.getModel();
int count = dm.getRowCount();
while(dm.getRowCount() > 0)
{
dm.removeRow(0);
}
int ind = rec - 1;
String value;
{
value = (String)jTable1.getValueAt(ind, 0);
Object [] newrow = {"Name", value};
dm.addRow(newrow);
}
{
value = (String)jTable1.getValueAt(ind, 1);
Object [] newrow = {"Age", value};
dm.addRow(newrow);
}
{
value = (String) jTable1.getValueAt(ind, 2);
Object [] newrow = {"Class", value};
dm.addRow(newrow);
}
{
value = (String)jTable1.getValueAt(ind, 3);
Object [] newrow = {"Grade", value};
dm.addRow(newrow);
}
}

You might also like