tibum
Outubro 2, 2012, 9:51pm
#1
boa noite,
gostaria de colocar minha barra de progresso pra funcionar
tenho a classe abaixo, dai quando executo o programa chamando a classe, na barra mostra 0% e pula direto pro 100% sendo q no metodo xCarregando(); tem varios sleep (pra deixar o processo mais lento, na tentativa de ver a barra se atualizando)
e ainda fica loop (repetindo) o metodo xCarregando();
gostaria que a barra interagisse com o metodo… ex.: 0% 15% 16% 20% 25% 80% 100%
e que nao repetisse.
Obs.: li varios topicos referente a barras… mas ainda nao consigo
[code] class ProcessarThread extends Thread {
public void run() {
for (int i = 1; i <= 100; ) {
jProgressBar1.setValue(i);
xCarregando();
try {
Thread.sleep(100);
} catch (Exception e) {
System.out.println("Erro");
}
}
}
}[/code]
O que faz o método xCarregando() ?
olha esse pequeno exemplo é o que vc quer cara!!
[code]public class SwingProgressBar{
final static int interval = 1000;
int i;
JLabel label;
JProgressBar pb;
Timer timer;
JButton button;
public SwingProgressBar() {
JFrame frame = new JFrame("Progress Bar");
button = new JButton("Começar o download");
button.addActionListener(new ButtonListener());
pb = new JProgressBar(0, 20);
pb.setValue(0);
pb.setStringPainted(true);
label = new JLabel("---@-Vitor");
JPanel panel = new JPanel();
panel.add(button);
panel.add(pb);
JPanel panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
panel1.add(panel, BorderLayout.NORTH);
panel1.add(label, BorderLayout.CENTER);
panel1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
frame.setContentPane(panel1);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create a timer.
timer = new Timer(interval, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (i == 20){
Toolkit.getDefaultToolkit().beep();
timer.stop();
button.setEnabled(true);
pb.setValue(0);
String str = "<html>" + "<font color=\"#FF0000\">" + "<b>" + "Downloading completo." + "</b>" + "</font>" + "</html>";
label.setText(str);
}
i = i + 1;
pb.setValue(i);
}
});
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
button.setEnabled(false);
i = 0;
String str = "<html>" + "<font color=\"#008000\">" + "<b>" + "Downloading em processo......." + "</b>" + "</font>" + "</html>";
label.setText(str);
timer.start();
}
}
public static void main(String[] args) {
SwingProgressBar spb = new SwingProgressBar();
}
}
[/code]
Phelps
Outubro 3, 2012, 8:57am
#4
Cadê a condição de incrementação do for?
Se o erro continuar, manda o método xCarregando ai…
tibum
Outubro 3, 2012, 9:01pm
#5
RiQuInHo_$_$ nao entendi mto seu codigo… eh q sou mto “cru” em java… to tentando aprender meio que na raça rsrs…
jeanmalvessi
Phelps
tae o metodo… ele envia um email com o texto digitado na jTextfield.
[code] private void xCarregando(){
Properties props = new Properties();
/** Parâmetros de conexão com servidor Hotmail */
props.put(“mail.transport.protocol”, “smtp”);
props.put(“mail.smtp.host”, “smtp.live.com ”);
props.put(“mail.smtp.socketFactory.port”, “587”);
props.put(“mail.smtp.socketFactory.fallback”, “false”);
props.put(“mail.smtp.starttls.enable”, “true”);
props.put(“mail.smtp.auth”, “true”);
props.put(“mail.smtp.port”, “587”);
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication
getPasswordAuthentication(){
return new javax.mail.PasswordAuthentication("xxxxxxxxxx@hotmail.com", "xxxxxxxxxxx");
}
});
/** Ativa Debug para sessão */
session.setDebug(false);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("xxxxx@hotmail.com")); //Remetente
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("xxxxx@hotmail.com")); //Destinatário(s)
message.setSubject("Email");//Assunto
message.setText( jTextField1.getText());
/**Método para enviar a mensagem criada*/
Transport.send(message);
System.out.println("Feito!!!");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
jButton2.setEnabled(true);
}[/code]