FileDocCategorySizeDatePackage
YesNoPanel.javaAPI DocExample6837Sat Jan 24 10:44:38 GMT 2004je3.beans

YesNoPanel

public class YesNoPanel extends JPanel
This JavaBean displays a multi-line message and up to three buttons. It fires an AnswerEvent when the user clicks on one of the buttons

Fields Summary
protected String
messageText
protected Alignment
alignment
protected String
yesLabel
protected String
noLabel
protected String
cancelLabel
protected MultiLineLabel
message
protected JPanel
buttonbox
protected JButton
yes
protected JButton
no
protected JButton
cancel
protected List
listeners
This field holds a list of registered ActionListeners.
Constructors Summary
public YesNoPanel()
The no-argument bean constructor, with default property values

 this("Your\nMessage\nHere"); 
public YesNoPanel(String messageText)

 
	this(messageText, Alignment.LEFT, "Yes", "No", "Cancel");
    
public YesNoPanel(String messageText, Alignment alignment, String yesLabel, String noLabel, String cancelLabel)
A constructor for programmers using this class "by hand"

	// Create the components for this panel
	setLayout(new BorderLayout(15, 15));
	
	// Put the message label in the middle of the window.
	message = new MultiLineLabel(messageText, 20, 20, alignment);
	message.setOpaque(false);  // allow background color to show through
	add(message, BorderLayout.CENTER);
	
	// Create a panel for the Panel buttons and put it at the bottom
	// of the Panel.  Specify a FlowLayout layout manager for it.
	buttonbox = new JPanel();
	buttonbox.setLayout(new FlowLayout(FlowLayout.CENTER, 25, 15));
	buttonbox.setOpaque(false);  // allow background color to show through
	add(buttonbox, BorderLayout.SOUTH);
	
	// Create each specified button, specifying the action listener
	// and action command for each, and adding them to the buttonbox
	yes = new JButton();                   // Create buttons
	no = new JButton();
	cancel = new JButton();
	// Add the buttons to the button box
	buttonbox.add(yes);
	buttonbox.add(no);
	buttonbox.add(cancel);

	// Register listeners for each button
	yes.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    fireEvent(new AnswerEvent(YesNoPanel.this,
					      AnswerEvent.YES));
		}
	    });

	no.addActionListener(new ActionListener() { 
		public void actionPerformed(ActionEvent e) {
		    fireEvent(new AnswerEvent(YesNoPanel.this,
					      AnswerEvent.NO));
			      }
	    });
	cancel.addActionListener(new ActionListener() { 
		public void actionPerformed(ActionEvent e) {
		    fireEvent(new AnswerEvent(YesNoPanel.this,
					      AnswerEvent.CANCEL));
		}
	    });
	
	// Now call property setter methods to set the message and button
	// components to contain the right text
	setMessageText(messageText);
	setAlignment(alignment);
	setYesLabel(yesLabel);
	setNoLabel(noLabel);
	setCancelLabel(cancelLabel);
    
Methods Summary
public voidaddAnswerListener(AnswerListener l)
Register an action listener to be notified when a button is pressed

    
                 
         listeners.add(l); 
public voidfireEvent(AnswerEvent e)
Send an event to all registered listeners

	// Make a copy of the list and fire the events using that copy.
	// This means that listeners can be added or removed from the original
	// list in response to this event.
	Object[] copy = listeners.toArray();
	for(int i = 0; i < copy.length; i++) {
	    AnswerListener listener = (AnswerListener) copy[i];
	    switch(e.getID()) {
	    case AnswerEvent.YES: listener.yes(e); break;
	    case AnswerEvent.NO:  listener.no(e); break;
	    case AnswerEvent.CANCEL: listener.cancel(e); break;
	    }
	}
    
public AlignmentgetAlignment()

 return alignment; 
public java.awt.FontgetButtonFont()

 return yes.getFont(); 
public java.lang.StringgetCancelLabel()

 return cancelLabel; 
public java.awt.ColorgetMessageColor()

 return message.getForeground(); 
public java.awt.FontgetMessageFont()

 return message.getFont(); 
public java.lang.StringgetMessageText()

 return messageText; 
public java.lang.StringgetNoLabel()

 return noLabel; 
public java.lang.StringgetYesLabel()

 return yesLabel; 
public static voidmain(java.lang.String[] args)
A main method that demonstrates the class

	// Create an instance of YesNoPanel, with title and message specified:
	YesNoPanel p = new YesNoPanel("Do you really want to quit?");

	// Register an action listener for the Panel.  This one just prints
	// the results out to the console.
	p.addAnswerListener(new AnswerListener() {
		public void yes(AnswerEvent e) { System.exit(0); }
		public void no(AnswerEvent e) { System.out.println("No"); }
		public void cancel(AnswerEvent e) {
                    System.out.println("Cancel");
                }
	    });
	
	JFrame f = new JFrame();
	f.getContentPane().add(p);
	f.pack();
	f.setVisible(true);
    
public voidremoveAnswerListener(AnswerListener l)
Remove an Answer listener from our list of interested listeners

 listeners.remove(l); 
public voidsetAlignment(Alignment alignment)

	this.alignment = alignment;
	message.setAlignment(alignment);
    
public voidsetButtonFont(java.awt.Font f)

	yes.setFont(f);
	no.setFont(f);
	cancel.setFont(f);
    
public voidsetCancelLabel(java.lang.String l)

	cancelLabel = l;
	cancel.setText(l);
	cancel.setVisible((l != null) && (l.length() > 0));
    
public voidsetMessageColor(java.awt.Color c)

	message.setForeground(c);
    
public voidsetMessageFont(java.awt.Font f)

	message.setFont(f);
    
public voidsetMessageText(java.lang.String messageText)

	this.messageText = messageText;
	message.setLabel(messageText);
    
public voidsetNoLabel(java.lang.String l)

	noLabel = l;
	no.setText(l);
	no.setVisible((l != null) && (l.length() > 0));
    
public voidsetYesLabel(java.lang.String l)

	yesLabel = l;
	yes.setText(l);
	yes.setVisible((l != null) && (l.length() > 0));