123 Eng

Engineering the engineers™


Latest Jobs   Forum Map

 


Home
Source Codes
Engineering Colleges

Training  Reports
Seminar Reports
Placement Papers

Forums

   Computer Science / IT
   Electronics
   Electrical
   Mechanical
   Chemical
   Civil

   CAT / MBA

   GMAT / Foreign MBA
Latest Jobs

Engineering Jobs / Technical Jobs
Management Jobs

Sitemap
Terms of use

Displaying  Source Code(s)  
 

 
JOptionPaneDemo - creating dialog boxes in Java.

--------------------------------------------------------------------------------

Description : I am submitting this code so that many aspirants can get some
idea of creating dialog boxes in Java.


// code written by Shambhu Soman

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class JOptionPaneDemo extends JFrame implements ActionListener
{
JLabel lblMessage,lblConfirm,lblInput,lblOption;
JButton b1,b2,b3,b4,b5,b6,b7;
JPanel p;
Icon icon;
GridBagLayout gl;
GridBagConstraints gbc;

public JOptionPaneDemo()
{
setTitle("JOptionPane's Demo");
lblMessage=new JLabel("Message:");
lblConfirm=new JLabel("Confirm:");
lblInput=new JLabel("Input:");
lblOption=new JLabel("Option:");

//JOptionPane.showMessageDialog()
b1=new JButton("Question");
b1.addActionListener(this);

b2=new JButton("Error");
b2.addActionListener(this);

b3=new JButton("Information");
b3.addActionListener(this);

b4=new JButton("Warning");
b4.addActionListener(this);

//JOptionPane.showConfirmDialog()
b5=new JButton("Confirm");
b5.addActionListener(this);

b6=new JButton("Input");
b6.addActionListener(this);

b7=new JButton("Option");
b7.addActionListener(this);

addWindowListener(new Demo());

p=new JPanel();
gl=new GridBagLayout();
gbc=new GridBagConstraints();
p.setLayout(gl);

//putting an icon on the dialog box
icon=new ImageIcon("D:\images\cat.gif");

gbc.anchor=GridBagConstraints.WEST;
gbc.insets=new Insets(5,5,5,5);
gbc.gridx=1;
gbc.gridy=1;
gl.setConstraints(lblMessage,gbc);
p.add(lblMessage);

gbc.gridx=2;
gl.setConstraints(b1,gbc);
p.add(b1);

gbc.gridx=3;
gl.setConstraints(b2,gbc);
p.add(b2);

gbc.gridx=4;
gl.setConstraints(b3,gbc);
p.add(b3);

gbc.gridx=5;
gl.setConstraints(b4,gbc);
p.add(b4);

gbc.gridx=1;
gbc.gridy=2;
gl.setConstraints(lblConfirm,gbc);
p.add(lblConfirm);

gbc.gridx=2;
gl.setConstraints(b5,gbc);
p.add(b5);

gbc.gridx=1;
gbc.gridy=3;
gl.setConstraints(lblInput,gbc);
p.add(lblInput);

gbc.gridx=2;
gl.setConstraints(b6,gbc);
p.add(b6);

gbc.gridx=1;
gbc.gridy=4;
gl.setConstraints(lblOption,gbc);
p.add(lblOption);

gbc.gridx=2;
gl.setConstraints(b7,gbc);
p.add(b7);


getContentPane().add(p);
setVisible(true);
setSize(500,500);
}
public static void main(String args[])
{
new JOptionPaneDemo();
}

public int option(int i)
{
return 0;
}

public void actionPerformed(ActionEvent ae)
{
Object src=ae.getSource();
if(src==b1)
{
JOptionPane.showMessageDialog(this,"This is a Question
Dialog","Question",JOptionPane.QUESTION_MESSAGE);
return;
}
else
if(src==b2)
{
JOptionPane.showMessageDialog(this,"This is an error
Message","Error",JOptionPane.ERROR_MESSAGE);
return;
}
else
if(src==b3)
{
JOptionPane.showMessageDialog(this,"Information or Plain
Message","Information",JOptionPane.INFORMATION_MESSAGE,icon);
return;
}
else
if(src==b4)
{
JOptionPane.showMessageDialog(this,"This is a Warning
Message","Warning",JOptionPane.WARNING_MESSAGE);
return;
}
else
if(src==b5)
{
int i=JOptionPane.showConfirmDialog(this,"Confirm
Dialog","Confirm",JOptionPane.YES_NO_OPTION);
if(i==JOptionPane.YES_OPTION)
{
option(i);

}
}
else
if(src==b6)
{
JOptionPane.showInputDialog(this,"Enter your name here:");
return;
}
else
if(src==b7)
{
JOptionPane.showOptionDialog(this,"Sure to
Quit","Options",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESS
AGE,null,null,null);
}
}

class Demo extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
int i=JOptionPane.showConfirmDialog(null,"Sure to
Exit","Exit",JOptionPane.YES_NO_CANCEL_OPTION);

if(i==JOptionPane.YES_OPTION)
{
//System.exit(0);
Runtime.getRuntime().exit(0);
}
else
if(i==JOptionPane.NO_OPTION)
{
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
}
else
if(i==JOptionPane.CANCEL_OPTION)
{
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
}
}
};
};

 

Contribute content or training reports / feedback / Comments
job placement papers
All rights reserved copyright 123ENG