Add to Favorites    Make Home Page 17331 Online  
 Language Categories  
 Our Services  

Home » Java Home » GUI Home » JOptionPaneDemo - creating dialog boxes in Java.

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title JOptionPaneDemo - creating dialog boxes in Java.
Author Shambhu Soman
Author Email shambhu_79 [at] yahoo.com
Description I am submitting this code so that many aspirants can get some
idea of creating dialog boxes in Java.
Category Java » GUI
Hits 369027
Code Select and Copy the Code
// 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); } } }; };

Related Source Codes

Script Name Author
Sending mail Using JavaMail to Yahoo and Gmail accounts sai prasad
Simple Program in Java to Implement Multithreading Satish.K
Simple Calculator in Java Using Remote Method Invocation Satish.K
Guest Book Application Using Servlets Satish.K
String Manipulation Using Stringification Satish.K
String Manipulation Using Stringification Satish.K
Moving Ball Application Using Java Beans Satish.K
Rapid Roll game subrahmanyeswararao
student mgm arpan
Sourav Datta
Download Manager Sagar
Address Book in Java Rahul Chouhan
address book using java database connectivity(jdbc-msaccess) shekhar bansal
sun Steganography B.Rajavel
Connecting Java with MS-Access - Inserting data in Aseem

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=595


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review