Assignment description.

Language: Java.

Level: Basic.

Price: 40$

Programming Assignment.

Modify the 'MessageBoxes' applet that is attached to this assignment so that it uses action listners for each button.

see attached example also.

Example.txt

// Demonstrates JoptionPane.

// <applet code=MessageBoxes2 width=200 height=150</applet>

// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002

// See copyright notice in CopyRight.txt.

// modified by Dan Creagan to support 402 homework example

// The following shows how to add one action listener into the applet

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class MessageBoxes2 extends JApplet {

public JButton Mybutt=new JButton("Click me");

public MessageBoxes2() // constructor

{

Mybutt.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

JOptionPane.showMessageDialog(null,

"There's a bug on you!", "Hey!",

JOptionPane.ERROR_MESSAGE);

}

}

);

}

public void init() {

Container cp = getContentPane();

cp.setLayout(new FlowLayout());

cp.add(Mybutt);

}

}

MessageBoxes.html

<html<center>

<applet code=MessageBoxes height=150 width =200>

</applet>

</center>

</html>

MessageBoxes.java

//: c14:MessageBoxes.java

// Demonstrates JoptionPane.

// <applet code=MessageBoxes width=200 height=150</applet>

// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002

// See copyright notice in CopyRight.txt.

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class MessageBoxes extends JApplet {

private JButton[] b = {

new JButton("Alert"), new JButton("Yes/No"),

new JButton("Color"), new JButton("Input"),

new JButton("3 Vals")

};

private JTextField txt = new JTextField(15);

private ActionListener al = new ActionListener() {

public void actionPerformed(ActionEvent e) {

String id = ((JButton)e.getSource()).getText();

if(id.equals("Alert"))

JOptionPane.showMessageDialog(null,

"There's a bug on you!", "Hey!",

JOptionPane.ERROR_MESSAGE);

else if(id.equals("Yes/No"))

JOptionPane.showConfirmDialog(null,

"or no", "choose yes",

JOptionPane.YES_NO_OPTION);

else if(id.equals("Color")) {

Object[] options = { "Red", "Green" };

int sel = JOptionPane.showOptionDialog(

null, "Choose a Color!", "Warning",

JOptionPane.DEFAULT_OPTION,

JOptionPane.WARNING_MESSAGE, null,

options, options[0]);

if(sel != JOptionPane.CLOSED_OPTION)

txt.setText("Color Selected: " + options[sel]);

} else if(id.equals("Input")) {

String val = JOptionPane.showInputDialog(

"How many fingers do you see?");

txt.setText(val);

} else if(id.equals("3 Vals")) {

Object[] selections = {"First", "Second", "Third"};

Object val = JOptionPane.showInputDialog(

null, "Choose one", "Input",

JOptionPane.INFORMATION_MESSAGE,

null, selections, selections[0]);

if(val != null)

txt.setText(val.toString());

}

}

};

public void init() {

Container cp = getContentPane();

cp.setLayout(new FlowLayout());

for(int i = 0; i < b.length; i++) {

b[i].addActionListener(al);

cp.add(b[i]);

}

cp.add(txt);

}

public static void main(String[] args) {

MessageBoxes m=new MessageBoxes();

}

} ///:~

Solution.

MessageBoxes.java

//: c14:MessageBoxes.java

// Demonstrates JoptionPane.

// <applet code=MessageBoxes width=200 height=150</applet>

// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002

// See copyright notice in CopyRight.txt.

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class MessageBoxes extends JApplet {

JButton alert = new JButton("Alert");

JButton yesno = new JButton( "Yes/No" );

JButton color = new JButton( "Color" );

JButton input = new JButton( "Input" );

JButton vals = new JButton( "3 Vals" );

private JButton[] b = { alert, yesno, color, input, vals };

private JTextField txt = new JTextField(15);

public void init() {

// add action listeners for each control

alert.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

JOptionPane.showMessageDialog(null, "There's a bug on you!", "Hey!", JOptionPane.ERROR_MESSAGE);

}

}

);

yesno.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e) {

JOptionPane.showConfirmDialog(null, "or no", "choose yes", JOptionPane.YES_NO_OPTION);

}

});

color.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e) {

Object[] options = {"Red", "Green"};

int sel = JOptionPane.showOptionDialog(null, "Choose a Color!", "Warning",

JOptionPane.DEFAULT_OPTION,

JOptionPane.WARNING_MESSAGE, null,

options, options[0]);

if (sel != JOptionPane.CLOSED_OPTION)

txt.setText("Color Selected: " + options[sel]);

}

});

input.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e) {

String val = JOptionPane.showInputDialog("How many fingers do you see?");

txt.setText(val);

}

});

vals.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e) {

Object[] selections = {"First", "Second", "Third"};

Object val = JOptionPane.showInputDialog(null, "Choose one", "Input",

JOptionPane.INFORMATION_MESSAGE,

null, selections, selections[0]);

if (val != null)

txt.setText(val.toString());

}

});

Container cp = getContentPane();

cp.setLayout(new FlowLayout());

for (int i = 0; i < b.length; i++) {

cp.add(b[i]);

}

cp.add(txt);

}

public static void main(String[] args) {

new MessageBoxes();

}

} ///:~