Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
OptionPanes provide a simple mechanism for providing dialog messages, as shown in the example below:
Example OptionPane (Run Example Code)
import java.awt.event.*; import javax.swing.*; public class OptionPaneDemo extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel implements ActionListener { private final JButton message = new JButton("Message"); private final JButton confirm = new JButton("Confirm"); private final JButton input = new JButton("Input"); public View() { super(); this.add(this.message); this.add(this.confirm); this.add(this.input); this.message.addActionListener(this); this.confirm.addActionListener(this); this.input.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { final Object source = e.getSource(); if (source == this.message) { messageDialog(); } else if (source == this.confirm) { confirmDialog(); } else if (source == this.input) { inputDialog(); } } // helper methods void messageDialog() { JOptionPane.showMessageDialog(null, "A message"); } void confirmDialog() { final int option = JOptionPane.showConfirmDialog(null, "Confirm message"); if (option == JOptionPane.YES_OPTION) { JOptionPane.showMessageDialog(null, "YES selected"); } else if (option == JOptionPane.NO_OPTION) { JOptionPane.showMessageDialog(null, "NO selected"); } else if (option == JOptionPane.CANCEL_OPTION) { JOptionPane.showMessageDialog(null, "CANCEL selected"); } else if (option == JOptionPane.CLOSED_OPTION) { JOptionPane.showMessageDialog(null, "CLOSED selected"); } } void inputDialog() { final String message = JOptionPane.showInputDialog("Allow user input"); if (message != null) { JOptionPane.showMessageDialog(null, message); } } } }
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.