READ BEFORE SOLVING: I\'m lost on this assignment so please COMMENT BEFORE YOU \
ID: 3560522 • Letter: R
Question
READ BEFORE SOLVING:
I'm lost on this assignment so please COMMENT BEFORE YOU "separate the single action listener logic into multiple listeners". Just give me a basic idea of what's going on. That's what I'm ultimately after.
BEST ANSWER AND POINTS GOES TO THE FIRST PERSON WHO CORRECTLY SOLVES THIS WHILE PROVIDING COMMENTS. I ALWAYS PAY AND NEVER SWITCH MY CHOICE FOR BEST ANSWER.
Modify the following 'MessageBoxes' application so it uses a single action listener for each button. This will require you to separate the single action listener logic into multiple listeners, one for each button. Then modify the code to provide additional options to two or more buttons.
/*
* The source code for this assignment started with
* a sample from "Thinking in Java" 3rd ed. page 825
* by Bruce Eckel. I have finished adding the rest of the action logic.
*
* Try to implement one new action listener at a time
* do not try to do all of them and then compile
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MessageBoxes{
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 JFrame frame;
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")){
int val = JOptionPane.showConfirmDialog(null,
"or no", "Chose yes",
JOptionPane.YES_NO_OPTION);
if(val != JOptionPane.CLOSED_OPTION){
if(val == 0){
txt.setText("Yes");
}
else{
txt.setText("No");
}
}
}
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 mant 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 MessageBoxes(){
frame = new JFrame("Title");
frame.setSize(250, 250);
frame.getContentPane().setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for(int i = 0; i < b.length; i++){
frame.getContentPane().add(b[i]);
}
frame.getContentPane().add(txt);
frame.setVisible(true);
}
public void launchJFrame(){
for(int i = 0; i < b.length; i++){
b[i].addActionListener(al);
}
}
public static void main(String [] args){
MessageBoxes messageBoxes = new MessageBoxes();
messageBoxes.launchJFrame();
}
}
Explanation / Answer
/*
* The source code for this assignment started with
* a sample from "Thinking in Java" 3rd ed. page 825
* by Bruce Eckel. I have finished adding the rest of the action logic.
*
* Try to implement one new action listener at a time
* do not try to do all of them and then compile
*/
/*
* When you compile this code you'll see a total of
* 6 class files, 1 for MessageBoxes and
* other 5 for ActionListeners.
*
*
* Creating an instance of a class with use of an interface
* is called using an anonymous inner class.
*
* Feel free to ask if you've any doubts
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MessageBoxes{
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 JFrame frame;
/*
* Since ActionListner is an Interface and not a Class
* if you create an instance with new, you have to
* implement all the methods of this interface
* (which in this case is just one (void actionPerformed(ActionEvent e)))
*
*
*
* In here I've created a separate method to
* add the listeners and the add this method
* in the constructor
*/
private void addListners(){
b[0].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"There's a bug on you!", "Hey!",
JOptionPane.ERROR_MESSAGE);
}
});
b[1].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int val = JOptionPane.showConfirmDialog(null,
"or no", "Chose yes",
JOptionPane.YES_NO_OPTION);
if(val != JOptionPane.CLOSED_OPTION){
if(val == 0){
txt.setText("Yes");
}
else{
txt.setText("No");
}
}
}
});
b[2].addActionListener(new ActionListener() {
@Override
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]);
}
}
});
b[3].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String val = JOptionPane.showInputDialog("How mant fingers do you see?");
txt.setText(val);
}
});
b[4].addActionListener(new ActionListener() {
@Override
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());
}
}
});
}
/*
* I've commented and not removed this method
*/
/*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")){
int val = JOptionPane.showConfirmDialog(null,
"or no", "Chose yes",
JOptionPane.YES_NO_OPTION);
if(val != JOptionPane.CLOSED_OPTION){
if(val == 0){
txt.setText("Yes");
}
else{
txt.setText("No");
}
}
}
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 mant 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 MessageBoxes(){
frame = new JFrame("Title");
frame.setSize(250, 250);
frame.getContentPane().setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for(int i = 0; i < b.length; i++){
frame.getContentPane().add(b[i]);
}
frame.getContentPane().add(txt);
frame.setVisible(true);
}
public void launchJFrame(){
addListners();
}
public static void main(String [] args){
MessageBoxes messageBoxes = new MessageBoxes();
messageBoxes.launchJFrame();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.