Ver Mensaje Individual
  #3 (permalink)  
Antiguo 14/04/2011, 12:55
fcojose2001
 
Fecha de Ingreso: marzo-2009
Ubicación: Salamanca, España
Mensajes: 141
Antigüedad: 15 años, 1 mes
Puntos: 4
Respuesta: hilos, progressMonitor

Hola Xerelo, estaba justo editando el tema cuando me contestaste xq creo que de esta manera se ve mejor lo que me pasa.

En el ventana principal dispongo de un boton que al pulsarlo inicia un bucle cuyo resultado lo ha de mostrar por consola, a la vez, se debe crear un hilo donde se ejecutara otro bucle y cuyo resultado se muestre en una nueva ventana.

Ambos hilos por separado funcionan perfectamente, pero al juntarlos algo pasa que no corre bien la aplicacion.

Veamos el codigo:

En este caso, una ventana y un boton, al pulsarlo se inicia un bucle y se muestra por consola:

Código Javascript:
Ver original
  1. import java.awt.BorderLayout;
  2. import java.awt.FlowLayout;
  3. import java.awt.GridBagConstraints;
  4. import java.awt.GridBagLayout;
  5. import java.awt.Insets;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import javax.swing.JButton;
  9. import javax.swing.JDialog;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JProgressBar;
  13. import javax.swing.WindowConstants;
  14.  
  15. public class Main2 {
  16.  
  17.     /** La ventana */
  18.     private JFrame v;
  19.  
  20.     /** El botón */
  21.     private JButton b;
  22.  
  23.     public static void main(String[] args) {
  24.         new Main2();
  25.     }
  26.  
  27.     /**
  28.      * Crea la ventana, inicializa todo y la visualiza
  29.      */
  30.     public Main2() {
  31.         // Nueva ventana. Se el pone un FlowLayout para que el botón y campo
  32.         // de texto quede alineados.
  33.         v = new JFrame("Version Beta Transmision");
  34.         GridBagLayout vLayout = new GridBagLayout();
  35.         v.getContentPane().setLayout(vLayout);
  36.         vLayout.rowWeights = new double[] { 0.1, 0.1, 0.1, 0.1, 0.1 };
  37.         vLayout.rowHeights = new int[] { 7, 7, 7, 7, 7 };
  38.         vLayout.columnWeights = new double[] { 0.1, 0.0, 0.1, 0.1 };
  39.         vLayout.columnWidths = new int[] { 7, 129, 7, 7 };
  40.  
  41.         // Se crea el botón y se mete en la ventana
  42.         b = new JButton("Enviar");
  43.         v.getContentPane().add(
  44.                 b,
  45.                 new GridBagConstraints(3, 4, 1, 1, 0.0, 0.0,
  46.                         GridBagConstraints.CENTER, GridBagConstraints.NONE,
  47.                         new Insets(0, 0, 0, 0), 0, 0));
  48.  
  49.         // Se le dice al botón qué tiene que hacer cuando lo pulsemos.
  50.         b.addActionListener(new ActionListener() {
  51.  
  52.             public void actionPerformed(ActionEvent e) {
  53.  
  54.                 int i = 0;
  55.                 while (true) {
  56.                     System.out.println("Valor: " + i);
  57.                     i++;
  58.                 }
  59.  
  60.             }
  61.         });
  62.  
  63.         // Se le dice a la ventana que termine el programa cuando se la cierre
  64.         v.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  65.  
  66.         // Se le da un tamaño automático a la ventana para que quepa todo su
  67.         // contenido.
  68.         v.pack();
  69.         v.setSize(764, 365);
  70.  
  71.         // Se hace visible la ventana
  72.         v.setVisible(true);
  73.     }
  74.  
  75. }

Ahora, al pulsar el boton, se crea un nuevo hilo que muestra un bucle en una nueva ventana:

Código Javascript:
Ver original
  1. import java.awt.BorderLayout;
  2. import java.awt.FlowLayout;
  3. import java.awt.GridBagConstraints;
  4. import java.awt.GridBagLayout;
  5. import java.awt.Insets;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import javax.swing.JButton;
  9. import javax.swing.JDialog;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JProgressBar;
  13. import javax.swing.WindowConstants;
  14.  
  15. public class Main2 {
  16.  
  17.     /** La ventana */
  18.     private JFrame v;
  19.  
  20.     /** El botón */
  21.     private JButton b;
  22.  
  23.     public static void main(String[] args) {
  24.         new Main2();
  25.     }
  26.  
  27.     /**
  28.      * Crea la ventana, inicializa todo y la visualiza
  29.      */
  30.     public Main2() {
  31.         // Nueva ventana. Se el pone un FlowLayout para que el botón y campo
  32.         // de texto quede alineados.
  33.         v = new JFrame("Version Beta Transmision");
  34.         GridBagLayout vLayout = new GridBagLayout();
  35.         v.getContentPane().setLayout(vLayout);
  36.         vLayout.rowWeights = new double[] { 0.1, 0.1, 0.1, 0.1, 0.1 };
  37.         vLayout.rowHeights = new int[] { 7, 7, 7, 7, 7 };
  38.         vLayout.columnWeights = new double[] { 0.1, 0.0, 0.1, 0.1 };
  39.         vLayout.columnWidths = new int[] { 7, 129, 7, 7 };
  40.  
  41.         // Se crea el botón y se mete en la ventana
  42.         b = new JButton("Enviar");
  43.         v.getContentPane().add(
  44.                 b,
  45.                 new GridBagConstraints(3, 4, 1, 1, 0.0, 0.0,
  46.                         GridBagConstraints.CENTER, GridBagConstraints.NONE,
  47.                         new Insets(0, 0, 0, 0), 0, 0));
  48.  
  49.         // Se le dice al botón qué tiene que hacer cuando lo pulsemos.
  50.         b.addActionListener(new ActionListener() {
  51.  
  52.             public void actionPerformed(ActionEvent e) {
  53.  
  54.                 v.setVisible(false);
  55.                 Hilo t = new Hilo();
  56.                 t.start();
  57.  
  58.             }
  59.         });
  60.  
  61.         // Se le dice a la ventana que termine el programa cuando se la cierre
  62.         v.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  63.  
  64.         // Se le da un tamaño automático a la ventana para que quepa todo su
  65.         // contenido.
  66.         v.pack();
  67.         v.setSize(764, 365);
  68.  
  69.         // Se hace visible la ventana
  70.         v.setVisible(true);
  71.     }
  72.  
  73. }

El codigo del hilo es este:
Código Javascript:
Ver original
  1. import java.awt.BorderLayout;
  2.  
  3. import javax.swing.JDialog;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6. import javax.swing.JProgressBar;
  7.  
  8. public class Hilo extends Thread{
  9.    
  10.     public void run() {
  11.       JFrame parentFrame = new JFrame();
  12.             parentFrame.setSize(500, 150);
  13.             JLabel jl = new JLabel();
  14.             jl.setText("Contando : 0");
  15.  
  16.             parentFrame.add(BorderLayout.CENTER, jl);
  17.             parentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.  
  19.             parentFrame.setVisible(true);
  20.  
  21.           int i =0;  
  22.       while(true){
  23.               jl.setText("Contando : " + i);
  24.               i++;
  25.       }
  26.                
  27.     }
  28. }

Y ahora lo que no funcina, las dos cosas a la vez :(
Código Javascript:
Ver original
  1. import java.awt.BorderLayout;
  2. import java.awt.FlowLayout;
  3. import java.awt.GridBagConstraints;
  4. import java.awt.GridBagLayout;
  5. import java.awt.Insets;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import javax.swing.JButton;
  9. import javax.swing.JDialog;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JProgressBar;
  13. import javax.swing.WindowConstants;
  14.  
  15. public class Main2 {
  16.  
  17.     /** La ventana */
  18.     private JFrame v;
  19.  
  20.     /** El botón */
  21.     private JButton b;
  22.  
  23.     public static void main(String[] args) {
  24.         new Main2();
  25.     }
  26.  
  27.     /**
  28.      * Crea la ventana, inicializa todo y la visualiza
  29.      */
  30.     public Main2() {
  31.         // Nueva ventana. Se el pone un FlowLayout para que el botón y campo
  32.         // de texto quede alineados.
  33.         v = new JFrame("Version Beta Transmision");
  34.         GridBagLayout vLayout = new GridBagLayout();
  35.         v.getContentPane().setLayout(vLayout);
  36.         vLayout.rowWeights = new double[] { 0.1, 0.1, 0.1, 0.1, 0.1 };
  37.         vLayout.rowHeights = new int[] { 7, 7, 7, 7, 7 };
  38.         vLayout.columnWeights = new double[] { 0.1, 0.0, 0.1, 0.1 };
  39.         vLayout.columnWidths = new int[] { 7, 129, 7, 7 };
  40.  
  41.         // Se crea el botón y se mete en la ventana
  42.         b = new JButton("Enviar");
  43.         v.getContentPane().add(
  44.                 b,
  45.                 new GridBagConstraints(3, 4, 1, 1, 0.0, 0.0,
  46.                         GridBagConstraints.CENTER, GridBagConstraints.NONE,
  47.                         new Insets(0, 0, 0, 0), 0, 0));
  48.  
  49.         // Se le dice al botón qué tiene que hacer cuando lo pulsemos.
  50.         b.addActionListener(new ActionListener() {
  51.  
  52.             public void actionPerformed(ActionEvent e) {
  53.  
  54.                 v.setVisible(false);
  55.                 Hilo t = new Hilo();
  56.                 t.start();
  57.  
  58.                 int i = 0;
  59.                 while (true) {
  60.                     System.out.println("Valor: " + i);
  61.                     i++;
  62.                 }
  63.  
  64.             }
  65.         });
  66.  
  67.         // Se le dice a la ventana que termine el programa cuando se la cierre
  68.         v.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  69.  
  70.         // Se le da un tamaño automático a la ventana para que quepa todo su
  71.         // contenido.
  72.         v.pack();
  73.         v.setSize(764, 365);
  74.  
  75.         // Se hace visible la ventana
  76.         v.setVisible(true);
  77.     }
  78.  
  79. }