java gui 11

Curso de Java Netbeans Completo☕ [11.- Barra de progreso (JProgressBar) ]

En este tutorial vamos a aprender a usar una barra de progreso y ademas vamos a manejar hilos

Java swing: https://es.wikipedia.org/wiki/Swing_(biblioteca_gr%C3%A1fica)
Documentación de java: https://docs.oracle.com/javase/8/docs/api/

Codigo: https://github.com/programadornovato/JavaGUI/commit/2e99fe12a2d12f1b8addacd74fc096c945a2941c

package com.programadornovato.javagui;

import static java.lang.Thread.sleep;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author eugenio
 */
public class BarraProgreso extends javax.swing.JFrame {

    /**
     * Creates new form BarraProgreso
     */
    public BarraProgreso() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bar = new javax.swing.JProgressBar();
        btnCargar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        bar.setStringPainted(true);

        btnCargar.setText("Cargar");
        btnCargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCargarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCargar)
                    .addComponent(bar, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(49, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(68, Short.MAX_VALUE)
                .addComponent(btnCargar)
                .addGap(31, 31, 31)
                .addComponent(bar, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(132, 132, 132))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCargarActionPerformed
        Thread hilo=new Thread(){
            @Override
            public void run(){
                for(int i=1;i<=100;i++){
                    try {
                        sleep(50);
                        bar.setValue(i);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(BarraProgreso.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }                
            }
        };
        hilo.start();
        
    }//GEN-LAST:event_btnCargarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BarraProgreso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BarraProgreso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BarraProgreso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BarraProgreso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BarraProgreso().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar bar;
    private javax.swing.JButton btnCargar;
    // End of variables declaration//GEN-END:variables
}

[Curso] Java Netbeans GUI Completo☕: https://www.youtube.com/watch?v=S0XKc_MJCS0&list=PLCTD_CpMeEKThfXo8D-RXOGu5FarO7_qv

? Instalar Netbeans 11 en Windows 10: https://www.youtube.com/watch?v=EouitrKS6Cw
? Instalar Netbeans 11 en Ubuntu con Snap [Más fácil que en windows?]: https://www.youtube.com/watch?v=LllPPV9SMzQ
? Descargar e Instalar Netbeans 11 en ubuntu 18 04: https://www.youtube.com/watch?v=tWiX3Z5t5kQ
? Netbeans Sublime Theme ?: https://www.youtube.com/watch?v=oAF2Q7mTZZM
? Editar CSS directamenete en Chrome con Netbeans: https://www.youtube.com/watch?v=HlQs0a7R2cY
Pagina de netbeans: https://netbeans.org/

Instalar MySQL? en Ubuntu 18.04: https://www.youtube.com/watch?v=hx-nGKFVixA
Instalar PhpMyAdmin en Windows 10 ⛵: https://youtu.be/36ritPuyd00
Gracias por apoyar este canal: https://www.patreon.com/programadornovato?fan_landing=true

? Facebook: https://facebook.com/ProgramadorNovatoOficial
? Twitter: https://twitter.com/programadornova
? Linkedin: https://www.linkedin.com/in/programadornovato/
? Instagram: https://www.instagram.com/programadornovato/

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *