Barra de progresso

4 respostas
T

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 :(

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");
                }
            }

            }
        }

4 Respostas

jeanmalvessi

O que faz o método xCarregando() ?

RiQuInHo_

olha esse pequeno exemplo é o que vc quer cara!!

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();
	}
}
Phelps

Cadê a condição de incrementação do for?

Se o erro continuar, manda o método xCarregando ai…

T

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.

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("[email removido]", "xxxxxxxxxxx");
                             }
                        });

            /** Ativa Debug para sessão */
            session.setDebug(false);
      
            try {

                  Message message = new MimeMessage(session);
                  message.setFrom(new InternetAddress("[email removido]")); //Remetente

                  message.setRecipients(Message.RecipientType.TO, 
                                    InternetAddress.parse("[email removido]")); //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);
             
    }
Criado 2 de outubro de 2012
Ultima resposta 3 de out. de 2012
Respostas 4
Participantes 4