package awt;

import java.awt.*;
import java.awt.event.*;

This class has two possible uses: MODAL: when you call getResult(), blocks all other user input until a choice is made; then returns this choice. NON-MODAL: shows itself when you call getResult(). In this case, getResult() always returns null. The dialog window will close automatically when the user picks a button. If you want to do something when that happens, use the result of addButton() to add an ActionListener. @author Geert Vernaeve
public class SimpleDialog extends Dialog implements ActionListener { private Panel buttonPanel; private Panel textPanel; private String result = null; // Frame and cursor to restore private Frame fr; private Cursor oldCursor; private Exception exc; public SimpleDialog(Component comp,String title,boolean modal) { super(getFrame(comp),title,modal); fr = getFrame(comp); Cursor oldCursor = fr.getCursor(); fr.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); buttonPanel = new Panel(new FlowLayout()); buttonPanel.setBackground(Color.lightGray); add("South",buttonPanel); textPanel = new Panel(new GridLayout(0,1)); add("Center",textPanel); }
Show an exception You cannot add more buttons since it is shown immediately!
public SimpleDialog(Component comp,String title,Exception ex,boolean modal) { this(comp,title,modal); addText(ex.getLocalizedMessage()); addButton("Debug info ..."); addButton("Bummer!"); exc = ex; getResult(); }
Show a one-line message and one button You cannot add more buttons since it is shown immediately!
public SimpleDialog(Component comp,String title,String message,String buttontxt,boolean modal) { this(comp,title,modal); addText(message); addButton(buttontxt); getResult(); } public Button addButton(String text) { Button b = new Button(text); b.addActionListener(this); buttonPanel.add(b); return b; }
Add a custom component
public Component addComp(Component comp) { return buttonPanel.add(comp); } public void addText(String text) { int idx; while ( (idx = text.indexOf('\n')) >= 0) { textPanel.add(new Label(text.substring(0,idx))); text = text.substring(idx+1); } textPanel.add(new Label(text,Label.CENTER)); } public void actionPerformed(ActionEvent e) { result = e.getActionCommand(); dispose(); if (exc == null) return; // Exception dialog if (result.equals("Bummer!")) return; // PENDING: naar een PrintStream of PrintWriter sturen // en dan tonen in een venster System.out.println(exc.getClass().getName()); exc.printStackTrace(); new SimpleDialog(this,"Debug","Debug dumped to stdout","OK",false); }
Helper function
public static Frame getFrame(Component comp) { while (!(comp instanceof Frame) && comp!=null) comp = comp.getParent(); return (Frame)comp; }
If the dialog is modal: Wait until the user has made a choice. Then return result. If the dialog is not modal: return result or null if the user hasn't chosen any button yet. Warning: only call this method once, then buffer the resulting string. Calling this method twice causes the dialog to pop up twice ...
public String getResult() { if (getGraphics() == null) { pack(); try { Point pt = fr.getLocationOnScreen(); Dimension siz = fr.getSize(); Dimension thisSiz = getSize(); int locx = pt.x+(siz.width-thisSiz.width)/2; if (locx < 0) locx = 0; int locy = pt.y+(siz.height-thisSiz.height)/2; if (locy < 0) locy = 0; setLocation(locx,locy); } catch (IllegalComponentStateException exc) { // If fr not visible, the getLocationOnScreen() // throws this exception } show(); } fr.setCursor(oldCursor==null ? Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR) : oldCursor); return result; } }