NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

problem with a JprogressBar

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Developers
View previous topic :: View next topic  
Author Message
kal



Joined: 25 Jan 2012
Posts: 12
Location: sfax

PostPosted: Sat Feb 04, 2012 9:20 pm    Post subject: problem with a JprogressBar Reply with quote

hi i have a problem with my interface,i want to I want when I hit the button, the progress bar goes off, so I have not found where is the problem in my code is that any one can help me and thank you.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JProgressBar;
public class acceuil1_1 extends javax.swing.JFrame implements ActionListener {
JProgressBar current = new JProgressBar(0, 2000);
int num = 0;

/** Creates new form acceuil1 */
public acceuil1_1() {

initComponents();
current.setValue(0);
}

// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jProgressBar1 = new javax.swing.JProgressBar();
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("start");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jLabel2.setIcon(new javax.swing.ImageIcon("C:\\Documents and Settings\\USER\\Bureau\\images_projet\\images (3).jpg")); // NOI18N

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(38, 38, 3Cool
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 377, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(49, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(117, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(109, 109, 109))
.addGroup(layout.createSequentialGroup()
.addGap(193, 193, 193)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(205, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(130, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(113, 113, 113))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(26, Short.MAX_VALUE)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(11, 11, 11)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 1Cool
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(40, 40, 40))
);

pack();
}// </editor-fold>




private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
while (num < 2000) {
current.setValue(num);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
num += 95;
}

}

/**
* @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(acceuil1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(acceuil1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(acceuil1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(acceuil1.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 acceuil1().setVisible(true);
}
});
}


// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JProgressBar jProgressBar1;
// End of variables declaration

@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}



}
Back to top
bolsover



Joined: 24 Jun 2010
Posts: 185

PostPosted: Sun Feb 05, 2012 3:51 pm    Post subject: Reply with quote

Hi kal

You have quite a few problems here...

1 The JProgressBar actually displayed is 'jProgressBar1' NOT 'current' which is not displayed at all and is referenced in your jButton1ActionPerformed method.

2 The code in your jButton1ActionPerformed does not work correctly because Swing components should only be accessed from the Event Dispatch Thread (you need to do some reading up on this).

3 Your class is named 'acceuil1_1' but you have references to 'acceuil1' elsewhere in the code. This issue will not stop the the program from running (if acceuil1 exists) but does not help with debugging!!

4 As a matter of style, class names should ALWAYS start with an uppercase letter 'Accuil1' - but again this will not stop the program from running.


To get this working, try the following... It is not particulary good code - but works!
You will need to add an import for javax.swing.SwingWorker; and I suggest you read up the Java Docs for that class - very useful when programming Java Swing.

Code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        SwingWorker worker = new SwingWorker() {
            @Override
            protected Object doInBackground() throws Exception {
                for (int i = 0; i < 100; i++) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                    }
                    jProgressBar1.setValue(i);
                }
                return null;
            }

            @Override
            protected void done() {
                jProgressBar1.setValue(0);
            }
        };
        worker.execute();
    }
Back to top
kal



Joined: 25 Jan 2012
Posts: 12
Location: sfax

PostPosted: Sun Feb 05, 2012 5:57 pm    Post subject: Reply with quote

thanks bolsover,now it works Very Happy Very Happy
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Developers All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo