JOptionPane com Timer
Crie uma mensagem de diálogo com timer, de modo que se o usuário não fazer determinada escolha em um tempo que você estipular a mensagem automaticamente some e você decide o que fazer.
Segue o exemplo:
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.WindowConstants;
/**
* A message dialog that closes after a timeout.
*/
public class TimedOptionPane {
protected static int UPDATE_PERIOD = 1000;
/**
* Show a dialogBox.
*
* @param f
* the owner
* @param message
* the message to display
* @param timeout
* in milliseconds
* @param title
* title of the dialog box
* @param timeoutMessage
* text showing remaining seconds
* @return {@link JOptionPane#YES_OPTION}, when YEs is clicked, {@link JOptionPane#NO_OPTION}
* if NO is clicked, or {@link JOptionPane#CANCEL_OPTION} on timeout or window closed
* event.
*/
public final static int showTimedOptionPane(Window f, String message, String title,String timeoutMessage, final long timeout,int messageType,int options) {
final JDialog dialog = new JDialog(f, title, Dialog.ModalityType.APPLICATION_MODAL);
final Message messageComponent = new Message(message, timeout, timeoutMessage);
final JOptionPane pane = new JOptionPane(messageComponent, messageType,options);
dialog.setContentPane(pane);
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Timer timer = new Timer(UPDATE_PERIOD, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
messageComponent.addEllapsedMilliseconds(UPDATE_PERIOD);
if (messageComponent.isOver()) {
dialog.dispose();
}
}
});
timer.start();
pane.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (dialog.isVisible() && (e.getSource() == pane)
&& (JOptionPane.VALUE_PROPERTY.equals(prop))) {
dialog.setVisible(false);
}
}
});
dialog.pack();
dialog.setLocationRelativeTo(f);
dialog.setVisible(true);
String valueString = pane.getValue().toString();
if (JOptionPane.UNINITIALIZED_VALUE.equals(valueString)) {
return JOptionPane.CANCEL_OPTION;
}
return ((Integer) pane.getValue()).intValue();
}
public final static int showTimedOptionPane(Window f, String message, String title,String timeoutMessage, final long timeout) {
return showTimedOptionPane( f, message, title, timeoutMessage, timeout,JOptionPane.QUESTION_MESSAGE, -1);
}
public final static int showTimedOptionPane(Window f, String message, String title,String timeoutMessage, final long timeout,int messageType) {
return showTimedOptionPane( f, message, title, timeoutMessage, timeout,messageType, -1);
}
/**
* Test...
*
* @param args
*/
public static void main(String args[]) {
int response = showTimedOptionPane(null, "Did you choose ?", "Please Choose",
"Remaining : ", 8000);
System.out.println("Choosen :" + response);
System.exit(0);
}
/**
* Content of the {@link JOptionPane}.
*
* @author clap
*
*/
static class Message extends JPanel {
private String RS;
JLabel message;
JLabel remaining;
private long timeout;
private long ellapsed = 0;
/**
* Build content panel.
*
* @param message
* the message to show
* @param milliseconds
* timeout in milliseconds
* @param timeoutMessage
* text showing remaining seconds
*/
protected Message(String message, long milliseconds, String timeoutMessage) {
super(new BorderLayout());
this.RS = timeoutMessage;
this.timeout = milliseconds;
this.message= new JLabel(message, SwingConstants.LEFT);
this.message.setHorizontalTextPosition(SwingConstants.LEFT);
add(this.message, BorderLayout.NORTH);
this.remaining = new JLabel(formatRemainingSeconds(milliseconds));
add(this.remaining, BorderLayout.SOUTH);
}
private String formatRemainingSeconds(long ms) {
return RS + (ms / 1000) + "s.";
}
/**
*
* @return true if the timeout has been reached.
*/
protected boolean isOver() {
return ellapsed >= timeout;
}
/**
* Indicates that some milliseconds has occured.
*
* @param milliseconds
*/
protected void addEllapsedMilliseconds(long milliseconds) {
ellapsed += milliseconds;
remaining.setText(formatRemainingSeconds(timeout - ellapsed));
repaint();
}
}
}
Fonte: Forum Sun
Últimos 5 artigos de Eduardo Costa
Sem Comentários »
RSS feed para os comentários deste artigo.

