Ver Mensaje Individual
  #1 (permalink)  
Antiguo 02/08/2006, 08:59
nloyola
 
Fecha de Ingreso: julio-2006
Ubicación: Argentina
Mensajes: 14
Antigüedad: 17 años, 10 meses
Puntos: 0
Barra de Progreso

Hola, un saludos para todos.
Tengo una clase que al ejecutarla, tarda mucho tiempo en terminar.
Es por esto que quiero usar una barra de progreso. Leyendo en la Web
encontre un ejemplo que mueve una barra, con un proceso sumarizando
hasta 1000 un contador. No me doy cuenta en que lugar tengo que poner
la clase a ejecutar, que se llama "CalculoProme". Va Ejemplo:

public class EMcorroproc extends JPanel {
Thread hilo;
Object objeto = new Object();
boolean pideParar = false;
JTextField texto;
JProgressBar barra;

public EMcorroproc() {
setLayout( new BorderLayout() );

texto = new JTextField();
add( texto,BorderLayout.NORTH );

JPanel panelInferior = new JPanel();
barra = new JProgressBar();
panelInferior.setLayout( new GridLayout(0,1) );
panelInferior.add( barra );
panelInferior.add( new JLabel( "Presione Arrancar..." ) );

JPanel panelBotones = new JPanel();
JButton botonArranque = new JButton( "Arrancar" );
botonArranque.setBackground( SystemColor.control );
panelBotones.add( botonArranque );
botonArranque.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent evt ) {
iniciaCuenta();
}
} );

JButton botonParar = new JButton( "Parar" );
botonParar.setBackground( SystemColor.control );
panelBotones.add( botonParar );
botonParar.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent evt ) {
detieneCuenta();
}
} );

panelInferior.add( panelBotones );
add( panelInferior,BorderLayout.SOUTH );
}

public void iniciaCuenta() {
if( hilo == null ) {
hilo = new ThreadCarga();
pideParar = false;
hilo.start();
}
}

public void detieneCuenta() {
synchronized( objeto ) {
pideParar = true;
objeto.notify();
}
}


class ThreadCarga extends Thread {
public void run() {
int min = 0;
int max = 100;

barra.setValue( min );
barra.setMinimum( min );
barra.setMaximum( max );

for (int i=min; i <= max; i++ ) {
barra.setValue( i ); ACA MUEVE LA BARRA
texto.setText( ""+i ); MUESTA LA SUMA
synchronized( objeto ) {
if( pideParar )
break;
try {
objeto.wait( 100 );
} catch( InterruptedException e ) {
// Se ignoran las excepciones
}
}
}
hilo = null;
}
}

public static void main( String args[] ) {
JFrame frame = new JFrame( "Tutorial de Java, Swing" );

frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent evt ) {
System.exit( 0 );
}
});

frame.getContentPane().add( new EMcorroproc(),BorderLayout.CENTER );
frame.setSize( 400,150 );
frame.setVisible( true );
}
}

GRACIAS AMIGOS................