Using your solution from assignment 5, instead of using the built in GUI class,
ID: 3560304 • Letter: U
Question
Using your solution from assignment 5, instead of using the built in GUI class, JOptionPane, you are to create and use your own GUI class. In the last assignment, you were to use both methods showInputDialog and showMessageDialog. For this assignment, your GUI class (or 2 classes if you prefer) should be able to create similar GUI objects with similar functionality as these 2 methods of JOptionPane.
Make sure you resubmit all of your classes from assignment 5 in addition to whatever new classes you create for this assignment.
Explanation / Answer
In this project you will first learn how to show simple dialog boxes using the built-in JOptionPane class. You will then create your own dialog box using the JDialog class. The javax.swing.JOptionPane class provides static methods that show simple dialog boxes. It is often desirable to display a message to the user in a popup message box. void JOptionPane.showMessageDialog(JFrame parent, String message) The first argument is a reference to the frame for the program showing the dialog box. It may be null if the frame is not available, as in a console application. The second argument is the message to be displayed. The method does not return until the user dismisses the dialog box by clicking OK. The following method shows a dialog box that asks the user to enter a string. String JOptionPane.showInputDialog(JFrame parent, String prompt) The user enters a string in a text field and clicks OK. The method returns the string entered by the user. If the user clicks Cancel or closes the dialog box, null is returned. 1. Create class Prog7 as shown. This program shows a message box and an input dialog when the "Ask" button is pressed. The string entered by the user is displayed in a text field. Run the program. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Prog7 extends JPanel implements ActionListener { private JFrame frame; private JTextField textbox; private JButton ask; public static void main(String[] args) {new Prog7();} public Prog7() { frame = new JFrame("Dialogs"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(this); this.init(); frame.pack(); frame.setVisible(true); } public void init() { this.setPreferredSize(new Dimension(200, 120)); this.setBackground(Color.WHITE); this.setLayout(null); textbox = new JTextField(); textbox.setEditable(false); textbox.setBounds(40, 40, 120, 25); this.add(textbox); ask = new JButton("Ask"); ask.addActionListener(this); ask.setBounds(70, 80, 60, 25); this.add(ask); } public void actionPerformed(ActionEvent event) { String reply; JOptionPane.showMessageDialog (frame, "The Ask button was pushed."); reply = JOptionPane.showInputDialog (frame, "Enter a string."); textbox.setText(reply); } } You will now learn how to create your own dialog box with a GUI of your own design. The dialog box is created and displayed when the user invokes the static display method. The GUI is created on a content panel which is pasted into a JDialog object and displayed. The "setVisible" method on a modal JDialog does not return until the dialog box is disposed. 2. Create a class InputBox as shown to display an input box. Complete the init and actionPerformed methods as shown by the comments. Replace the call to JOptionPane.showInputDialog in class Prog7 with a call to InputBox.display. Run the program. Hand in listings of all classes and window snapshots of your main panel and your InputBox dialog box. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class InputBox extends JPanel implements ActionListener { private JDialog dialog; private String result; // Widgets for the dialog box GUI. public static String display(JFrame parent, String prompt) { InputBox panel = new InputBox(); // The third argument requests a modal dialog box. JDialog dialog = new JDialog(parent, "Input Box", true); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setContentPane(panel); dialog.setLocation(40, 40); panel.init(dialog, prompt); dialog.pack(); dialog.setVisible(true); // Does not return until the dialog box is disposed. return panel.result; } public void init(JDialog d, String prompt) { dialog = d; // Save a reference to the JDialog object. result = null; // Set the default return value (on cancel). // Create the GUI for your dialog box here. Your GUI // should display the prompt message in a label, should // include a text field for the user to enter the reply, // and should have "OK" and "Cancel" buttons. Register // this panel as the ActionListener for the buttons. } public void actionPerformed (ActionEvent event) { String action = event.getActionCommand(); if (action.equals("OK")) { // The "OK" button has been pressed. Set result // to the string entered by the user. Get this // from a text field. result = } dialog.dispose(); // Close the dialog box. } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.