package Lect39GUIs; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingOptionPaneReplacement2 { static synchronized public String showInputDialog(String labelText) { JFrame frame = buildFrame(); JTextField textField = buildGUI(frame, labelText); displayFrame(frame); // This is a fancy way of saying stop execution until frame.notify() is called elsewhere // That happens when the user clicks on the OK button synchronized (frame) { try { frame.wait(); } catch (InterruptedException e) {} } // User has clicked on OK button, so get rid of window and return string frame.hide(); frame.dispose(); return textField.getText(); } static private JFrame buildFrame() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) {} // Create a top-level window to put the picture in JFrame frame; frame = new JFrame("Swing Example"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); return frame; } static private JTextField buildGUI(JFrame frame, String labelText) { Box verBox = Box.createVerticalBox(); Box horBox = Box.createHorizontalBox(); ImageIcon imageIcon = new ImageIcon("Lect39GUIs/questionmark.gif"); JLabel pic = new JLabel(imageIcon); horBox.add(Box.createHorizontalStrut(20)); horBox.add(pic); horBox.add(Box.createHorizontalStrut(20)); Box verBox2 = Box.createVerticalBox(); JLabel label = new JLabel(labelText); verBox2.add(label); verBox2.add(Box.createVerticalStrut(5)); JTextField textField = new JTextField(); textField.setPreferredSize(new Dimension(200, 20)); verBox2.add(textField); horBox.add(verBox2); horBox.add(Box.createHorizontalStrut(10)); verBox.add(Box.createVerticalStrut(10)); verBox.add(horBox); Box horBox2 = Box.createHorizontalBox(); horBox2.add(Box.createHorizontalGlue()); JButton okButton = new JButton("OK"); horBox2.add(okButton); horBox2.add(Box.createHorizontalStrut(10)); JButton cancelButton = new JButton("Cancel"); horBox2.add(cancelButton); horBox2.add(Box.createHorizontalGlue()); verBox.add(Box.createVerticalStrut(10)); verBox.add(horBox2); verBox.add(Box.createVerticalStrut(10)); OptionPaneOKListener okListener = new OptionPaneOKListener(frame); okButton.addActionListener(okListener); textField.addKeyListener(okListener); cancelButton.addActionListener(new OptionPaneCancelListener(frame, textField)); frame.setContentPane(verBox); return textField; } static private void displayFrame(JFrame frame) { frame.setLocation(100, 100); frame.pack(); frame.show(); } static public void main(String[] args) { // JOptionPane.showInputDialog("Enter some text:"); String result = SwingOptionPaneReplacement2.showInputDialog("Enter some text:"); System.out.println("You entered: " + result); } } class OptionPaneCancelListener implements ActionListener { private JFrame frame; private JTextField textField; public OptionPaneCancelListener(JFrame frame, JTextField textField) { this.frame = frame; this.textField = textField; } public void actionPerformed(ActionEvent e) { synchronized (frame) { textField.setText(""); frame.notifyAll(); } } }