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 void | addAnswerListener(AnswerListener l)Register an action listener to be notified when a button is pressed
listeners.add(l);
|
public void | fireEvent(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 Alignment | getAlignment() return alignment;
|
public java.awt.Font | getButtonFont() return yes.getFont();
|
public java.lang.String | getCancelLabel() return cancelLabel;
|
public java.awt.Color | getMessageColor() return message.getForeground();
|
public java.awt.Font | getMessageFont() return message.getFont();
|
public java.lang.String | getMessageText() return messageText;
|
public java.lang.String | getNoLabel() return noLabel;
|
public java.lang.String | getYesLabel() return yesLabel;
|
public static void | main(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 void | removeAnswerListener(AnswerListener l)Remove an Answer listener from our list of interested listeners listeners.remove(l);
|
public void | setAlignment(Alignment alignment)
this.alignment = alignment;
message.setAlignment(alignment);
|
public void | setButtonFont(java.awt.Font f)
yes.setFont(f);
no.setFont(f);
cancel.setFont(f);
|
public void | setCancelLabel(java.lang.String l)
cancelLabel = l;
cancel.setText(l);
cancel.setVisible((l != null) && (l.length() > 0));
|
public void | setMessageColor(java.awt.Color c)
message.setForeground(c);
|
public void | setMessageFont(java.awt.Font f)
message.setFont(f);
|
public void | setMessageText(java.lang.String messageText)
this.messageText = messageText;
message.setLabel(messageText);
|
public void | setNoLabel(java.lang.String l)
noLabel = l;
no.setText(l);
no.setVisible((l != null) && (l.length() > 0));
|
public void | setYesLabel(java.lang.String l)
yesLabel = l;
yes.setText(l);
yes.setVisible((l != null) && (l.length() > 0));
|