import javax.swing.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; public class OptionPane extends JFrame { // inherit JFrame private JButton button1 = new JButton("Confirm"); // declare and create a button private JButton button2 = new JButton("Input"); // declare and create a button private JButton button3 = new JButton("Message"); // declare and create a button private JButton button4 = new JButton("Options"); // declare and create a button private JButton button5 = new JButton("FileDialog"); // declare and create a button private JButton button6 = new JButton("FileChooser"); // declare and create a button /** * Constructor sets up the window and creates a toolbar */ public OptionPane() { // // setup the frames banner/title bar // super("My Frame"); // call JFrame's constructor and give it a title setSize(300,200); // set the width of the window to 300 pixels, height to 200. // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit the whole program when this frame is closed addWindowListener(new WindowAdapter() { // Quit the application public void windowClosing(WindowEvent e) { System.exit(0); } }); // // change to a grid layout manager // getContentPane().setLayout(new GridLayout(6,1)); // // Add the buttons to the frame's content pane, for each button, create a // separate inner class object as a listener and tell the button about it. // getContentPane().add(button1); // add button 1 button1.addActionListener(new ConfirmListener()); // create its listener and tell it getContentPane().add(button2); button2.addActionListener(new InputListener()); getContentPane().add(button3); button3.addActionListener(new MessageListener()); getContentPane().add(button4); button4.addActionListener(new OptionListener()); getContentPane().add(button5); button5.addActionListener(new FileDialogListener()); getContentPane().add(button6); button6.addActionListener(new FileChooserListener()); } /** * Inner class to handle button 1 */ class ConfirmListener implements ActionListener{ public void actionPerformed(ActionEvent event) { int answer; answer = JOptionPane.showConfirmDialog(OptionPane.this, "Do you like java?", "Dialog Confirm Example",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE); switch(answer){ case JOptionPane.YES_OPTION : OptionPane.this.setTitle("Yes was pressed"); break; case JOptionPane.NO_OPTION : OptionPane.this.setTitle("No was pressed"); break; case JOptionPane.CANCEL_OPTION : OptionPane.this.setTitle("Cancel was pressed"); break; }// end switch } } /** * Inner class to handle button 2 */ class InputListener implements ActionListener{ public void actionPerformed(ActionEvent event) { String answer; answer = JOptionPane.showInputDialog(OptionPane.this, "What is your quest?", "Dialog Input Example",JOptionPane.QUESTION_MESSAGE); if(answer != null) OptionPane.this.setTitle(answer); else OptionPane.this.setTitle("Cancel was pressed"); } } /** * Inner class to handle button 3 */ class MessageListener implements ActionListener{ public void actionPerformed(ActionEvent event) { JOptionPane.showMessageDialog(OptionPane.this,"You are the Weakest Link!", "Oops",JOptionPane.ERROR_MESSAGE); // popup an error message } } /** * Inner class to handle button 4 */ class OptionListener implements ActionListener{ public void actionPerformed(ActionEvent event) { String[] excuses = {"My dog ate it","Homework? What Homework?","Oh whateveeer"}; int response = JOptionPane.showOptionDialog(OptionPane.this,"What is your excuse?", "Where's your homework?",0,JOptionPane.QUESTION_MESSAGE, null,excuses, excuses[0]); if(response >= 0 && response < excuses.length) OptionPane.this.setTitle(excuses[response]); } } /** * Inner class to handle button 5 */ class FileDialogListener implements ActionListener{ public void actionPerformed(ActionEvent event) { FileDialog dialog = new FileDialog(OptionPane.this, "Open",FileDialog.LOAD); // create a filedialog object for reading dialog.setVisible(true); // set the dialog to be visible if(dialog.getFile() == null) // get the file name selected, if its null then no file was selected, just exit OptionPane.this.setTitle("No file selected"); else OptionPane.this.setTitle(dialog.getDirectory()+dialog.getFile()); } } /** * Inner class to handle button 6 */ class FileChooserListener implements ActionListener{ public void actionPerformed(ActionEvent event) { JFileChooser dialog = new JFileChooser(); // create the chooser object int result = dialog.showOpenDialog(OptionPane.this); // use Open (as opposed to save) if(result == JFileChooser.APPROVE_OPTION) { String filename = dialog.getSelectedFile().getPath(); OptionPane.this.setTitle(filename); // display file name in banner } else OptionPane.this.setTitle("Cancel was pressed"); } } /*********************************************************************** * * Main routine, Note, this routine can be in any class. */ public static void main(String[] arguments){ OptionPane fr = new OptionPane(); // create the frame fr.setVisible(true); // allow it to be seen } }