elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Security Series.XSS. [Cross Site Scripting]


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Duda sobre JProcessBar
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Duda sobre JProcessBar  (Leído 2,765 veces)
MC.cover

Desconectado Desconectado

Mensajes: 45


Ver Perfil
Duda sobre JProcessBar
« en: 15 Febrero 2016, 08:01 am »

Hola muy buenas tardes,días o noches donde quiera que estén.
Tengo una duda sobre JProcessBar el cual es si puedo hacer que se "llene" la barra pasados 5 minutos (por ejemplo). Y como seria para hacerlo aumentar segundo a segundo el proceso de carga. Espero me puedan ayudar desde ya muchas gracias :D ::)


En línea

Tronos154

Desconectado Desconectado

Mensajes: 79


Invictus maneo


Ver Perfil
Re: Duda sobre JProcessBar
« Respuesta #1 en: 15 Febrero 2016, 14:30 pm »

No conozco muy bien la librería swing,pero para el tema del tiempo puedes ayudarte del método System.currentTimeMillis() el cual junto a una simple resta puedes determinar el tiempo que lleva tu programa en funcionamiento y asi poder cambiar la barra cuando pase un determinado tiempo.


En línea

sodark

Desconectado Desconectado

Mensajes: 81


Ver Perfil WWW
Re: Duda sobre JProcessBar
« Respuesta #2 en: 15 Febrero 2016, 16:43 pm »

Puedes usar un timertask

http://totaki.com/poesiabinaria/2014/01/intro-timertask-java/

En teoria funciona como una interrupcion, por lo que no realiza espera activa (busy waitting)
En línea

0xFer


Desconectado Desconectado

Mensajes: 400



Ver Perfil
Re: Duda sobre JProcessBar
« Respuesta #3 en: 16 Febrero 2016, 03:53 am »

Te dejo un ejemplo:

Código
  1. import java.awt.EventQueue;
  2. import java.awt.FlowLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import javax.swing.JButton;
  6. import javax.swing.JFrame;
  7. import javax.swing.JOptionPane;
  8. import javax.swing.JProgressBar;
  9. import javax.swing.Timer;
  10. import javax.swing.event.ChangeEvent;
  11. import javax.swing.event.ChangeListener;
  12.  
  13. @SuppressWarnings("serial")
  14. public class ProgressBar extends JFrame
  15. {
  16.    private int c = 0;
  17.    private Timer t;
  18.    private JButton btn;
  19.    private JProgressBar pBar;
  20.  
  21.    // 5 minutos en milisegundos
  22.    private final int waitingTime = 5 * 60 * 1000;// 900000 milliseconds
  23.  
  24.    private final int delay = waitingTime / 100;// 9000 == 9 seconds
  25.  
  26.    public ProgressBar()
  27.    {
  28.        setLayout(new FlowLayout());
  29.  
  30.        // delay in milliseconds
  31.        // inside the internal anonymous class we write the code we want the timer to execute each delay
  32.        // Instead of 50 milliseconds, you just have to write delay
  33.        t = new Timer(delay, new ActionListener() {
  34.  
  35.            @Override
  36.            public void actionPerformed(ActionEvent e)
  37.            {
  38.                if(c <= 100) pBar.setValue(++c);
  39.            }
  40.        });
  41.  
  42.        btn = new JButton("WAIT");
  43.  
  44.        // Adding an action listener: when you click the button, the timer starts
  45.        btn.addActionListener(new ActionListener() {
  46.  
  47.            @Override
  48.            public void actionPerformed(ActionEvent e)
  49.            {
  50.                // starting the timer, which causes it to sending ActionEvents to its Listeners
  51.                t.start();
  52.            }
  53.        });
  54.  
  55.        add(btn);
  56.  
  57.        pBar = new JProgressBar(0, 100);
  58.  
  59.        // Setting the initial value of the progress bar
  60.        pBar.setValue(c);// c == 0
  61.  
  62.        // Showing a string progress bar
  63.        pBar.setStringPainted(true);
  64.  
  65.        // adding a changeListener to the progress bar
  66.        pBar.addChangeListener(new ChangeListener() {
  67.  
  68.            @Override
  69.            public void stateChanged(ChangeEvent e)
  70.            {
  71.                if(pBar.getValue() == 100)
  72.                {
  73.                    t.stop();
  74.                    JOptionPane.showMessageDialog(null, "Your order is ready!");
  75.  
  76.                    c = 0;
  77.                    pBar.setValue(c);// set the initial value of the progress bar to 0 again
  78.                }
  79.            }
  80.        });
  81.  
  82.        add(pBar);
  83.  
  84.        pack();
  85.        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  86.        setVisible(true);
  87.        setLocationRelativeTo(null);
  88.    }
  89.  
  90.    public static void main(String[] args)
  91.    {
  92.        EventQueue.invokeLater(new Runnable() {
  93.            public void run()
  94.            {
  95.                try
  96.                {
  97.                    new ProgressBar();
  98.                }
  99.                catch(Exception ex)
  100.                {
  101.                    JOptionPane.showMessageDialog(null, "Fatal Error.");
  102.                }
  103.            }
  104.        });
  105.    }
  106.  
  107. }

Tienes que presionar el botón para que los 5 minutos empiecen a contar.

El código lo copié y modifiqué de stackoverflow.

Saludos.
« Última modificación: 16 Febrero 2016, 03:57 am por 0xFer » En línea

Código
  1. int getRandomNumber(){
  2.    return 4; //chosen by fair dice roll
  3.              //guaranteed to be random
  4. }
MC.cover

Desconectado Desconectado

Mensajes: 45


Ver Perfil
Re: Duda sobre JProcessBar
« Respuesta #4 en: 17 Febrero 2016, 07:08 am »

Hola muchísimas gracias a todos por ayudarme con esta duda :) .
Gracias :)
.
En línea

0xFer


Desconectado Desconectado

Mensajes: 400



Ver Perfil
Re: Duda sobre JProcessBar
« Respuesta #5 en: 19 Febrero 2016, 00:47 am »

De nada  ::)
En línea

Código
  1. int getRandomNumber(){
  2.    return 4; //chosen by fair dice roll
  3.              //guaranteed to be random
  4. }
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines