Programs Chapter 1
A Simple Framed Window
import [Link].*;
import [Link].*;
public class SwingTest {s
public static void main(String[] args) {
JFrame frame = new JFrame("Test Frame");
[Link](new Dimension(300,200));
[Link](100,100);
[Link](true);
}
}
Changing Background Color
import [Link].*;
import [Link].*;
public class SwingTest {
public static void main(String[] args) {
JFrame frame = new JFrame("Test Frame");
[Link](new Dimension(300,200));
[Link](100,100);
Container contentPane = [Link]();
[Link]([Link]);
[Link](true);
}
}
Adding Check Box and Slider
public class SwingTest {
public static void main(String[] args) {
JFrame frame = new JFrame("Test Frame");
[Link](new Dimension(400,200));
[Link](100,100);
Container contentPane = [Link]();
JLabel label = new JLabel("HERE IS A LABEL");
[Link](label, [Link]);
JButton button = new JButton("BUTTON");
[Link](button, [Link]);
String[] options = {"Option 1", "Option 2", "Option 3"};
JList list = new JList(options);
[Link](list, [Link]);
JCheckBox cbox = new JCheckBox("Check");
[Link](cbox, [Link]);
JSlider slider = new JSlider();
[Link](slider, [Link]);
[Link](true);
}
}
Displaying a String:
import [Link].*;
import [Link].*;
class MyComponent extends
JComponent{ public void
paint(Graphics g) {
//setting font color
[Link]([Link]);
//settting font
[Link](new Font("Verdana",[Link],22));
//displaying string
[Link]("Hello World!", 100, 50);
}
}
public class ShapesEx {
public static void main(String[] args)
{ JFrame jf=new JFrame("My Frame");
[Link](400, 300);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
MyComponent comp=new MyComponent();
[Link](comp);
[Link](true);
}
}
Working with 2D Shapes:
Drawing a line
import [Link].*;
import [Link].*;
class MyComponent extends
JComponent{ public void paint(Graphics
g) {
//casting graphics to graphics 2d object
//needed for more effects...on shapes
Graphics2D g2=(Graphics2D)g; //setting color
[Link]([Link]);
//changing width
[Link](new BasicStroke(10));
//drawing a line
[Link](120, 30, 50, 140); //x1,y1,x2,y2
}
}
public class MyTest {
public static void main(String[] args) {
JFrame jf=new JFrame("Drawing Line");
[Link](300, 200); //width,height
[Link](JFrame.EXIT_ON_CLOSE);
/*MyComponent draw=new MyComponent();
[Link](draw);*/
[Link](new MyComponent());
[Link](true);
}
}
Drawing a Rectangle
import [Link].*;
import [Link].*;
class MyComponent extends
JComponent{ public void paint(Graphics
g) {
//casting graphics to graphics 2d object
//needed for more effects...on shapes
Graphics2D g2=(Graphics2D)g; //changing
width
[Link](new BasicStroke(10));
//setting background color
[Link]([Link]);
[Link](30, 40, 120, 60);
//drawing a rectangle
[Link]([Link]);
[Link](30, 40, 120, 60); //x,y,width,height
}
}
public class MyTest {
public static void main(String[] args) {
JFrame jf=new JFrame("Drawing Rectangle");
[Link](300, 200); //width,height
[Link](JFrame.EXIT_ON_CLOSE);
/*MyComponent draw=new MyComponent();
[Link](draw);*/
[Link](new MyComponent());
[Link](true);
}
}
Drawing a Square:
[Link](30, 40, 120, 120); //x,y,width,height
//height & width Same
Drawing a Circle:
[Link](30, 40, 120, 120); //x,y,width,height
//height & width Same
Drawing a Ellipse:
[Link](30, 40, 150, 80); //x,y,width,height
//height & width Different
Drawing an Arc:
public void paint(Graphics g)
{ Graphics2D g2=(Graphics2D)g;
[Link]([Link]);
[Link](new BasicStroke(10));
//[Link](x, y, width, height, startAngle, arcAngle);
[Link](30, 50, 100, 140, 130, 150);
}
Drawing a Polygon
public void paint(Graphics g)
{ Graphics2D g2=(Graphics2D)g;
[Link]([Link]);
[Link](new BasicStroke(5));
//drawing a polygon
int xPoly[] = {100, 150, 200, 175, 250}; int yPoly[] =
{150, 100, 125, 225, 100}; [Link](xPoly,
yPoly, 5); //[Link](xPoints, yPoints,
nPoints);
}
Using Special Font and Color for Text:
JLabel lbl=new JLabel();
[Link]("This is a text!");
[Link](100, 50);
//setting color
[Link]([Link]);
//setting font
[Link](new Font("Verdana",[Link],22));
Displaying Images:
import [Link];
import [Link].*;
class SwingEx{
SwingEx(){
JFrame jf=new JFrame("My Frame");
[Link](350, 350);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
[Link](null);
ImageIcon image=new ImageIcon
("/Users/Desktop/[Link]");
Image newimg=[Link]().getScaledInstance(200, 200,
Image.SCALE_SMOOTH);
ImageIcon newImg=new
ImageIcon(newimg); JLabel lbl=new
JLabel(newImg); [Link](60, 50);
[Link](200, 200); [Link](lbl);
[Link](true);
}
}
public class Example {
public static void main(String[] args) { new
SwingEx();
}
}
Mouse Events:
import [Link].*;
import [Link].*;
class SwingEx{
SwingEx(){
JFrame jf=new JFrame("My Frame");
[Link](350, 350);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
[Link](null);
JButton btn=new JButton("Click Me!");
[Link](100, 50); [Link](100,
50); [Link](btn);
JLabel lbl=new JLabel("Result");
[Link](100, 50);
[Link](100, 100);
[Link](lbl);
//adding mouse click event
[Link](new MouseListener() {
public void mouseClicked(MouseEvent e)
{ [Link]("Mouse Clicked!");
}
public void mousePressed(MouseEvent e)
{ [Link]("Mouse Pressed!");
}
public void mouseReleased(MouseEvent e)
{ [Link]("Mouse Released!");
}
public void mouseEntered(MouseEvent e)
{ [Link]("Mouse Entered!");
}
public void mouseExited(MouseEvent e)
{ [Link]("Mouse Exited!");
}
});
[Link](true);
}
}
public class Example {
public static void main(String[] args) { new
SwingEx();
}
}
Key Events:
import [Link].*;
import [Link].*;
class SwingEx{
SwingEx(){
JFrame jf=new JFrame("My Frame");
[Link](350, 350);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
[Link](null);
JTextField txt=new JTextField();
[Link](200, 50);
[Link](60, 100);
[Link](txt);
JLabel lbl=new JLabel("Event Result");
[Link](100, 50); [Link](100,
150); [Link](lbl);
//adding Key Event
[Link](new KeyListener() {
public void keyTyped(KeyEvent e) {
[Link]("Key Typed!");
}
public void keyPressed(KeyEvent e)
{ [Link]("Key Pressed!");
}
public void keyReleased(KeyEvent e)
{ [Link]("Key Released!");
}
});
[Link](true);
}
}
public class Example {
public static void main(String[] args)
{ new SwingEx();
}
}
Example of ItemListener:
Event triggered on Combo Box item selection.
import [Link].*;
import [Link].*;
import [Link].*;
class SwingEx{
SwingEx(){
JFrame jf=new JFrame("My Frame");
[Link](400,350);
[Link](null);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
JComboBox cmb=new JComboBox();
[Link]("BCA");
[Link]("BBA");
[Link]("MCA");
[Link]("MBA");
[Link](120, 60);
[Link](100,60);
[Link](false);
[Link](cmb);
[Link](new ItemListener() {
public void itemStateChanged(ItemEvent e)
{ if([Link]()==[Link]) {
//getting selected Item
String item=[Link]()
.toString();
//displaying
[Link]("Selected Item: "+item);
}
}
});
[Link](true);
}
}
public class Example {
public static void main(String[] args) { new
SwingEx();
}
}
Using Mouse Adapter Class
import [Link].*;
import [Link].*;
import [Link].*;
class SwingEx{
SwingEx(){
JFrame jf=new JFrame("My Frame");
[Link](400,350);
[Link](null);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
JButton btn=new JButton("Click Me!");
[Link](120, 50); [Link](100,
50); [Link](btn);
//Using mouse adapter class
[Link](new MouseAdapter() {
//we don't need to implement all methods of MouseListener
//we can use method as per the requirement
public void mouseEntered(MouseEvent e) {
[Link]("Mouse Entered!");
}
});
[Link](true);
}
}
public class ExampleA {
public static void main(String[] args) { new
SwingEx();
}
}
Using Key Adapter Class
import [Link].*;
import [Link].*;
import [Link].*;
class SwingEx{
SwingEx(){
JFrame jf=new JFrame("My Frame");
[Link](400,350);
[Link](null);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
JTextField txt=new JTextField();
[Link](120, 50);
[Link](100, 50);
[Link](txt);
[Link](new KeyAdapter()
{ public void keyTyped(KeyEvent e) {
[Link](null,"Key Typed!");
}
});
[Link](true);
}
}
public class Example {
public static void main(String[] args) { new
SwingEx();
}
}
Using Window Adapter Class
import [Link].*;
import [Link].*;
import [Link].*;
class SwingEx{
SwingEx(){
JFrame jf=new JFrame("My Frame");
[Link](300,250);
[Link](null);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
//adding window event to jframe
[Link](new WindowAdapter() {
public void windowOpened(WindowEvent e) {
[Link]("Frame Opened!");
}
});
[Link](true);
}
}
public class Example {
public static void main(String[] args) { new
SwingEx();
}
}
MVC design Pattern
3 classes for model, view and controller required + 1 class for startup.
So, in total 4 classes are required.
Source Code
[Link]
import [Link].*;
public class StudentView {
public JLabel lbl1,lbl2,lbl3;
public JTextField txt1,txt2;
public JButton btn1,btn2;
public StudentView() {
JFrame jf=new JFrame("Student Form");
[Link](400, 300);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
[Link](null);
lbl1=new JLabel("Enter Sid:");
[Link](100, 30);
[Link](20, 20);
[Link](lbl1);
txt1=new JTextField();
[Link](120, 30);
[Link](100, 20);
[Link](txt1);
lbl2=new JLabel("Enter Name:");
[Link](100, 30);
[Link](20, 60);
[Link](lbl2);
txt2=new JTextField();
[Link](120, 30);
[Link](100, 60);
[Link](txt2);
btn1=new JButton("Save");
[Link](100, 20);
[Link](50, 110);
[Link](btn1);
btn2=new JButton("Display");
[Link](100, 20);
[Link](160, 110);
[Link](btn2);
lbl3=new JLabel("Result:");
[Link](200, 30);
[Link](20, 140);
[Link](lbl3);
[Link](true);
}
}
[Link]
public class StudentModel
{ private int sid;
private String name;
public void setId(int sid) {
[Link]=sid;
}
public int getId()
{ return sid;
}
public void setName(String name) {
[Link]=name;
}
public String getName()
{ return name;
}
}
[Link]
import [Link].*;
public class StudentController
{ StudentView v;
StudentModel m;
public void initController() {
//initializing view
v=new StudentView();
//initializing model
m=new StudentModel();
//registering events
[Link](e->saveClicked());
[Link](e->displayClicked());
}
public void saveClicked() {
int sid=[Link]([Link]());
String name=[Link]();
[Link](sid);
[Link](name);
[Link](null, "Saved Successfully!");
}
public void displayClicked() {
[Link]("Sid: "+[Link]()+" Name: "+[Link]());
}
}
And finally,
Driver Class…
[Link]
//Driver Class
public class Example {
public static void main(String[] args) { StudentController
cont=new StudentController(); [Link]();
}
}
(Displaying Multiple Rows using JList)
[Link]
import [Link].*;
public class StudentView {
public JLabel lbl1,lbl2,lbl3;
public JTextField txt1,txt2;
public JButton btn1,btn2;
//required for creating a empty list
DefaultListModel lmodel;
public StudentView() {
JFrame jf=new JFrame("Student Form");
[Link](400, 300);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
[Link](null);
lbl1=new JLabel("Enter Sid:");
[Link](100, 30);
[Link](20, 20);
[Link](lbl1);
txt1=new JTextField();
[Link](120, 30);
[Link](100, 20);
[Link](txt1);
lbl2=new JLabel("Enter Name:");
[Link](100, 30);
[Link](20, 60);
[Link](lbl2);
txt2=new JTextField();
[Link](120, 30);
[Link](100, 60);
[Link](txt2);
btn1=new JButton("Save");
[Link](100, 20);
[Link](50, 110);
[Link](btn1);
btn2=new JButton("Display");
[Link](100, 20);
[Link](160, 110);
[Link](btn2);
//creating a JList
lmodel=new DefaultListModel();
JList jl=new JList(lmodel);
[Link](200, 100);
[Link](20, 140);
[Link](jl);
[Link](true);
}
}
[Link]
public class StudentModel
{ private int sid;
private String name;
public void setId(int sid) {
[Link]=sid;
}
public int getId()
{ return sid;
}
public void setName(String name) {
[Link]=name;
}
public String getName()
{ return name;
}
}
[Link]
import [Link].*;
import [Link].*;
public class StudentController {
StudentView v;
//for storing multiple data
ArrayList<StudentModel> data;
public void initController() {
//model initialization not required & will be initialized
in add button clicked
//initializing view
v=new StudentView();
//initializing ArrayList
data=new ArrayList<>();
//registering events
[Link](e->saveClicked());
[Link](e->displayClicked());
}
public void saveClicked() {
int sid=[Link]([Link]()); String
name=[Link](); //transferring to model
StudentModel m=new StudentModel();
[Link](sid);
[Link](name);
//adding model to ArrayList
[Link](m);
[Link](null, "Saved Successfully!");
}
public void displayClicked() {
//clearing previous data'
[Link]();
//getting data from ArrayList
for(StudentModel st:data) {
//[Link]([Link]()+" "+[Link]());
[Link]([Link]()+" "+[Link]());
}
}
}
[Link]
//Driver Class
public class Example {
public static void main(String[] args)
{ StudentController cont=new
StudentController(); [Link]();
}
}
(Using JTable for Displaying Multiple Rows)
[Link]
import [Link].*;
import [Link].*;
public class StudentView {
public JLabel lbl1,lbl2,lbl3;
public JTextField txt1,txt2;
public JButton btn1,btn2;
//required for creating a empty list
DefaultTableModel tmodel;
public StudentView() {
JFrame jf=new JFrame("Student Form");
[Link](400, 300);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
[Link](null);
lbl1=new JLabel("Enter Sid:");
[Link](100, 30);
[Link](20, 20);
[Link](lbl1);
txt1=new JTextField();
[Link](120, 30);
[Link](100, 20);
[Link](txt1);
lbl2=new JLabel("Enter Name:");
[Link](100, 30);
[Link](20, 60);
[Link](lbl2);
txt2=new JTextField();
[Link](120, 30);
[Link](100, 60);
[Link](txt2);
btn1=new JButton("Save");
[Link](100, 20);
[Link](50, 110);
[Link](btn1);
btn2=new JButton("Display");
[Link](100, 20);
[Link](160, 110);
[Link](btn2);
//creating empty table with default table model
String cols[]= {"Sid","Name"};
tmodel=new DefaultTableModel(cols,0); //0 rows
JTable jt=new JTable(tmodel);
JScrollPane jp=new JScrollPane(jt);
[Link](50, 150); [Link](200,
100); [Link](jp);
[Link](true);
}
}
[Link]
public class StudentModel
{ private int sid;
private String name;
public void setId(int sid) {
[Link]=sid;
}
public int getId()
{ return sid;
}
public void setName(String name) {
[Link]=name;
}
public String getName()
{ return name;
}
}
[Link]
import [Link].*;
import [Link].*;
public class StudentController {
StudentView v;
//for storing multiple data
ArrayList<StudentModel> data;
public void initController() {
//initializing ArrayList
data=new ArrayList<>();
//initializing view
v=new StudentView();
//registering events
[Link](e->saveClicked());
[Link](e->displayClicked());
}
public void saveClicked() {
int sid=[Link]([Link]()); String
name=[Link](); //transferring to model
StudentModel m=new StudentModel();
[Link](sid);
[Link](name);
//adding model to ArrayList
[Link](m);
[Link](null, "Saved Successfully!");
}
public void displayClicked() {
//clearing all rows
[Link](0);
//getting data from ArrayList
for(StudentModel st:data) {
//putting data in Object
Object[] obj= {[Link](),[Link]()};
[Link](obj);
}
}
}
[Link]
//Driver Class
public class Example {
public static void main(String[] args) { StudentController
cont=new StudentController(); [Link]();
}
}
Designing a JTable
import [Link].*;
import [Link].*;
import [Link].*;
//Driver Class
public class Example {
public static void main(String[] args) {
JFrame jf=new JFrame("Student Form");
[Link](400, 300);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
[Link](null);
//creating a JTable
String cols[]= {"Sid","Name","Address"}; DefaultTableModel
tmodel=new DefaultTableModel(cols,0); JTable jt=new
JTable(tmodel);
[Link](new Object[] {101,"Ram","Btm"});
[Link](new Object[] {102,"Shyam","Btm"});
[Link](new Object[] {103,"Hari","Btm"});
[Link](new Object[] {104,"Sita","Ktm"});
//designing JTable
//designing headings
[Link]().setBackground(Color.DARK_GRAY);
[Link]().setForeground([Link]);
[Link]().setFont(new Font(null,[Link],16));
//designing rows
[Link]([Link]);
[Link](new Font("Consolas",[Link],15));
JScrollPane jp=new JScrollPane(jt);
[Link](300, 150);
[Link](50, 50);
[Link](jp);
[Link](true);
}
}
Getting a Selected Row From JTable
import [Link].*;
import [Link].*;
import [Link].*;
//Driver Class
public class Example {
public static void main(String[] args) {
JFrame jf=new JFrame("Student Form");
[Link](400, 300);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
[Link](null);
//creating a JTable
String cols[]= {"Sid","Name","Address"}; DefaultTableModel
tmodel=new DefaultTableModel(cols,0);
JTable jt=new JTable(tmodel);
[Link](new Object[] {101,"Ram","Btm"});
[Link](new Object[] {102,"Shyam","Btm"});
[Link](new Object[] {103,"Hari","Btm"});
[Link](new Object[] {104,"Sita","Ktm"});
//designing JTable
//designing headings
[Link]().setBackground(Color.DARK_GRAY);
[Link]().setForeground([Link]);
[Link]().setFont(new Font(null,[Link],16));
//designing rows
[Link]([Link]);
[Link](new Font("Consolas",[Link],15));
JScrollPane jp=new JScrollPane(jt);
[Link](300, 150);
[Link](50, 50);
[Link](jp);
JButton btn=new JButton("Click Me");
[Link](100, 30); [Link](50,
200); [Link](btn);
[Link](e->{
//getting row number
int row=[Link]();
if(row==-1) {
[Link](null, "Please Select
Row!");
}else {
//getting data
int sid=[Link]([Link](row, 0)
.toString());
String name=[Link](row, 1).toString();
String address=[Link](row, 2).toString();
[Link](null, sid+" "+name+"
"+address);
}
});
[Link](true);
}
}
Getting a Selected Row from JList
import [Link].*;
import [Link].*;
//Driver Class
public class Example {
public static void main(String[] args) {
JFrame jf=new JFrame("Student Form");
[Link](400, 300);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
[Link](null);
DefaultListModel lmodel=new DefaultListModel();
JList jl=new JList(lmodel);
[Link]("101 Ram");
[Link]("102 Shyam");
[Link]("103 Hari");
[Link](200, 100);
[Link](50, 50);
[Link](jl);
JButton btn=new JButton("Click Me");
[Link](100, 30); [Link](50,
200); [Link](btn);
[Link](e->{
int index=[Link]();
if(index==-1) {
[Link](null, "Please select item!");
}else {
String data=[Link]().toString();
[Link](null, data);
}});
[Link](true);
}
}
demonstrate FlowLayout
import [Link].*;
import [Link].*;
public class FlowLayoutDemo
{
JFrame f;
FlowLayoutDemo ()
{
f = new JFrame ();
JLabel l1 = new JLabel ("Enter Name");
JTextField tf1 = new JTextField (10);
JButton b1 = new JButton ("SUBMIT");
[Link] (l1);
[Link] (tf1);
[Link] (b1);
[Link] (new FlowLayout ([Link]));
//setting flow layout of right alignment
[Link] (300, 300); [Link] (true);
}
public static void main (String[]args)
{
new FlowLayoutDemo ();
}
}
Border Layout
import [Link].*;
public class BorderLayoutDemo
{
public static void main (String[]args)
{
Frame f1 = new Frame ();
[Link] (250, 250);
Button b1 = new Button ("Button1");
Button b2 = new Button ("Button2");
Button b3 = new Button ("Button3");
Button b4 = new Button ("Button4");
Button b5 = new Button ("Button5");
[Link] (b1, [Link]);
[Link] (b2, [Link]);
[Link] (b3, [Link]);
[Link] (b4, [Link]);
[Link] (b5);
[Link] (true);
}
}
Grid Layout
import [Link].*;
import [Link].*;
public class GridLayoutDemo
{
public static void main (String[]args)
{
Frame f1 = new Frame ();
[Link] (250, 250);
GridLayout ob = new GridLayout (2, 2);
[Link] (ob);
Panel p1 = new Panel ();
Label l1 = new Label ("Enter name");
TextField tf = new TextField (10);
Button b1 = new Button ("Submit");
[Link] (l1);
[Link] (tf);
[Link] (b1);
[Link] (p1);
Panel p2 = new Panel ();
[Link] (p2);
Panel p3 = new Panel ();
[Link] (p3);
Label l2 = new Label ("Welcome to Java");
[Link] (l2);
[Link] (true);
}
}
Card Layout
import [Link].*;
import [Link].*;
//Driver Class
public class Example {
public static void main(String[] args)
{ JFrame jf=new JFrame("My
Frame"); [Link](400, 300);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
[Link](null);
//creating jpanel to demonstrate cardlayout
JPanel jp=new JPanel();
[Link](200, 80);
[Link](80, 40);
[Link]([Link]);
//adding cardlayout
CardLayout card=new CardLayout(40,30);
[Link](card);
JLabel lbl1=new JLabel("Label 1");
JLabel lbl2=new JLabel("Label 2");
JLabel lbl3=new JLabel("Label 3");
//adding level to cards
[Link]("card1",lbl1);
[Link]("card2",lbl2);
[Link]("card3",lbl3);
//creating another JPanel for buttons
JPanel jp1=new JPanel();
[Link](300, 100);
[Link](40, 120);
[Link](new FlowLayout());
JButton prev=new JButton("Previous");
JButton next=new JButton("Next");
JButton first=new JButton("First");
JButton last=new JButton("Last");
JButton show=new JButton("Show Card");
[Link](prev);
[Link](next);
[Link](first);
[Link](last);
[Link](show);
//adding panels to frame
[Link](jp);
[Link](jp1);
//adding click event to buttons
[Link](e->{
//previous
[Link](jp);
});
[Link](e->{
//next
[Link](jp);
});
[Link](e-
>{ [Link](jp);
});
[Link](e-
>{ [Link](jp);
});
[Link](e->{
[Link](jp, "card2");
});
[Link](true);
}
}
Spring Layout:
import [Link].*;
import [Link].*;
//Driver Class
public class Example {
public static void main(String[] args) {
JFrame jf = new JFrame("Spring Layout Example");
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
Container cp = [Link]();
//adding spring layout
SpringLayout spl=new SpringLayout();
[Link](spl);
//creating buttons
JButton btn1=new JButton("Button 1");
JButton btn2=new JButton("Button 2");
JButton btn3=new JButton("Button 3");
[Link](btn1);
[Link](btn2);
[Link](btn3);
//adding constraints
//for button 1
[Link]([Link], btn1,
24, [Link], cp);
[Link]([Link], btn1,
9, [Link], cp);
//for button 2
[Link]([Link], btn2,
49, [Link], cp);
[Link]([Link], btn2,
10, [Link], btn1);
//for button 3
[Link]([Link], btn3,
45, [Link], btn1);
[Link]([Link], btn3,
9, [Link], cp);
[Link](true);
}
}
Message Dialog
A message dialog shows information with the OK button. The method does
not return any value.
import [Link].*;
public class Example {
public static void main(String[] args) {
[Link](null, "Test Message",
"Title",JOptionPane.WARNING_MESSAGE);
}
}
Confirm Dialog
We can display a confirmation dialog box by using the showConfirmDialog()
method. The user's response is indicated by the return value.
import [Link].*;
public class Example {
public static void main(String[] args) {
int res = [Link](null,
"Save the file?",
"Title Message",
JOptionPane.YES_NO_OPTION,
//JOptionPane.YES_NO_OPTION
//JOptionPane.OK_CANCEL_OPTION
JOptionPane.QUESTION_MESSAGE);
[Link](res);
//yes/ok=0, no=1, cancel=2
}
}
Input Dialog
import [Link].*;
public class Example {
public static void main(String[] args) {
String res=[Link](null,
"Enter any value");
[Link](res);
}
}
Example 2:
import [Link].*;
public class Example {
public static void main(String[] args) {
/*Object res = [Link](parentComponent,
message,
title,
messageType,
icon,
selectionValues,
initialSelectionValue);
*/
Object res = [Link](null,
"Select a Value:",
"Title",
JOptionPane.INFORMATION_MESSAGE,
null,
new String[] {"BCA","BBA","MCA"},
"BBA");
[Link](res);
}
}
Option Dialog
import [Link].*;
public class Example {
public static void main(String[] args) {
int res = [Link](null,
"Do you want to Exit?", "Title",
JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
new String[] {"YES","NO"},
"NO");
[Link](res);
}
}
JFileChooser Dialog
A JFileChooser lets the user select a file from the file system. We can create
an object of the JFileChooser class. It allows the user to choose only files,
only directories, or both.
Example:
import [Link].*;
import [Link].*;
public class Example {
public static void main(String[] args) {
//display file chooser
JFileChooser fch=new JFileChooser();
[Link]("Open a picture file");
int res = [Link](null,"Attach");
if (res == JFileChooser.APPROVE_OPTION) { File selectedFile
= [Link](); [Link]("we selected:
" + selectedFile);
}
}
}
JColorChooser Dialog
A JColorChooser is a Swing component that lets we choose a color graphically
in a JDialog.
import [Link].*;
import [Link].*;
public class Example {
public static void main(String[] args) { //
Display a color chooser dialog
Color color = [Link](null,
"Select a color", [Link]);
//parent,message,initial color
[Link]("Selected Color: "+color);
}
}
Custom JDialog
We can create custom JDialog as per our requirement and place different
components on it as follows:
JDialog dialog=new JDialog();
Example:
import [Link].*;
public class Example {
public static void main(String[] args)
{ JDialog jd=new JDialog();
[Link](200, 150);
[Link](true);
[Link](“Custom Dialog”);
JLabel lbl=new JLabel("I am Label");
[Link](lbl);
[Link](true);
}
}