quinta-feira, 30 de dezembro de 2010

Feliz Ano Novo a todos.

Desejo Feliz Ano Novo a todos, felicidades e boas festas e que 2011 seja repleto de felicidades, paz, harmonia, só alegria e etc,etc,etc,etc,etc.
E muito mais posts!kkkk

terça-feira, 28 de dezembro de 2010

Criando um Preloader - Java

Criando um preloader em java.
Basta adequar para sua nescessidade.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
import java.util.Random;

public class Preloader extends JPanel
implements ActionListener,
PropertyChangeListener {

private JProgressBar progressBar;
private JButton startButton;
private Task task;
private String status;

class Task extends SwingWorker"<"Void, Void">" {
/*
* Main task. Executed in background thread.
*/
@Override
public Void doInBackground() {
Random random = new Random();
int progress = 0;
//Initialize progress property.
setProgress(0);
while (progress "<" 100) {
//Sleep for up to one second.
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException ignore) {}
//Make random progress.
progress += random.nextInt(10);
setProgress(Math.min(progress, 100));
}
return null;
}

/*
* Executed in event dispatching thread
*/
@Override
public void done() {
Toolkit.getDefaultToolkit().beep();
startButton.setEnabled(true);
setCursor(null); //turn off the wait cursor
//taskOutput.append("Done!\n");
progressBar.setString("Terminou!");
}
}

public Preloader() {
super(new BorderLayout());

//Create the demo's UI.
startButton = new JButton("Iniciar");
startButton.setActionCommand("start");
startButton.addActionListener(this);

progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);

JPanel panel = new JPanel();
panel.add(startButton);
panel.add(progressBar);

add(panel, BorderLayout.PAGE_START);
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

}

/**
* Invoked when the user presses the start button.
*/
public void actionPerformed(ActionEvent evt) {
startButton.setEnabled(false);
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
//Instances of javax.swing.SwingWorker are not reusuable, so
//we create new instances as needed.
task = new Task();
task.addPropertyChangeListener(this);
task.execute();
}

/**
* Invoked when task's progress property changes.
*/
public void propertyChange(PropertyChangeEvent evt) {
if ("progress" == evt.getPropertyName()) {
int progress = (Integer) evt.getNewValue();
progressBar.setString(String.format(Progresso(task.getProgress()) + " %d%%", progress));

}
}

public String Progresso(int progresso){

if(progresso "<"= 10){
status = "Processando...\n";
}
if((progresso ">"= 10) & (progresso "<"= 30)){
status = "Aguarde...\n";
}
if((progresso ">"= 30) & (progresso "<"= 50)){
status = "Calculando...\n";
}
if((progresso ">"= 50) & (progresso "<"= 70)){
status = "Verificando...\n";
}
if((progresso ">"= 70) & (progresso "<"= 80)){
status = "Processando...\n";
}
if((progresso ">"= 80) & (progresso "<"= 100)){
status = "Finalizando...\n";
}

return status;
}

/**
* Create the GUI and show it. As with all GUI code, this must run
* on the event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Preloader");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
JComponent newContentPane = new Preloader();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

Obs: Nos sinais de "<>" foram colocados aspas "" por causa do Blogger! basta retira-los para funcionar normal!

Referência:
http://download.oracle.com/javase/tutorial/uiswing/components/progress.html#contentsapi