• Domingo 12 de Mayo de 2024, 19:10

Autor Tema:  S.O.S actualizar una label etiqueta  (Leído 1387 veces)

Froddo

  • Nuevo Miembro
  • *
  • Mensajes: 11
    • Ver Perfil
S.O.S actualizar una label etiqueta
« en: Jueves 13 de Noviembre de 2008, 00:10 »
0
Hola amigos, les pido ayuda, ya que estoy algo perdido, les cuento:

necesito unir dos programas, estos estan corriendo bajo netbeans,los tengo en el mismo paquete,un programa se encarga de recibir datos y el otro es una ventanita que se encarga de mostrar los datos en la pantalla.
LO IMPORTANTE DEL PROGRAMA ESTA EN LA LINEA 93 Y 98
:programa que recibe los datos:
Código: Text
  1. package paqbascula;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import javax.comm.*;
  6.  
  7. public class Bascula implements Runnable, SerialPortEventListener {
  8. static String messageString = "P";
  9. static SerialPort serialPort;
  10. static OutputStream outputStream;
  11. static CommPortIdentifier portId;
  12. static Enumeration portList;
  13. static InputStream inputStream;
  14. static Thread readThread;
  15.  
  16. public static void main(String[] args) {
  17. portList = CommPortIdentifier.getPortIdentifiers();
  18.  
  19. while (portList.hasMoreElements()) {
  20. portId = (CommPortIdentifier) portList.nextElement();
  21. if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
  22.  
  23. //-------------------ciclo
  24. //-------------------escritura
  25. if (portId.getName().equals("COM1")) {
  26. try {
  27. serialPort = (SerialPort)
  28. portId.open("BasculaApp", 2000);
  29. } catch (PortInUseException e) {}
  30. try {
  31. outputStream = serialPort.getOutputStream();
  32. } catch (IOException e) {}
  33. try {
  34. serialPort.setSerialPortParams(9600,
  35. SerialPort.DATABITS_8,
  36. SerialPort.STOPBITS_1,
  37. SerialPort.PARITY_NONE);
  38. } catch (UnsupportedCommOperationException e) {}
  39. try {
  40. outputStream.write(messageString.getBytes());
  41. } catch (IOException e) {}
  42. //---------------fin escritura
  43. //---------------parametros lectura
  44. Bascula reader = new Bascula();
  45. }
  46. }
  47. }
  48.  
  49. }
  50.  
  51. public Bascula() {
  52. try {
  53. serialPort = (SerialPort) portId.open("BasculaApp", 2000);
  54. } catch (PortInUseException e) {}
  55. try {
  56. inputStream = serialPort.getInputStream();
  57. } catch (IOException e) {}
  58. try {
  59. serialPort.addEventListener(this);
  60. } catch (TooManyListenersException e) {}
  61. serialPort.notifyOnDataAvailable(true);
  62. try {
  63. serialPort.setSerialPortParams(9600,
  64. SerialPort.DATABITS_8,
  65. SerialPort.STOPBITS_1,
  66. SerialPort.PARITY_NONE);
  67. } catch (UnsupportedCommOperationException e) {}
  68. readThread = new Thread(this);
  69. readThread.start();
  70. }
  71.  
  72. public void run() {
  73. try {
  74. Thread.sleep(20000);
  75. } catch (InterruptedException e) {}
  76. }
  77.  
  78. public void serialEvent(SerialPortEvent event) {
  79. switch(event.getEventType()) {
  80. case SerialPortEvent.BI:
  81. case SerialPortEvent.OE:
  82. case SerialPortEvent.FE:
  83. case SerialPortEvent.PE:
  84. case SerialPortEvent.CD:
  85. case SerialPortEvent.CTS:
  86. case SerialPortEvent.DSR:
  87. case SerialPortEvent.RI:
  88. case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
  89. break;
  90. case SerialPortEvent.DATA_AVAILABLE:
  91. byte[] readBuffer = new byte[10];
  92.  
  93. try {   // aca está la clave,es acá donde se guarda los datos recibidos, en int numBytes
  94.  
  95.  
  96. while (inputStream.available() > 0) {
  97. int numBytes = inputStream.read(readBuffer);
  98. //aca se debe colocar un comando que permita ingresar los datos que cambian constantemente en la etiqueta de la ventana
  99.  
  100. }
  101.  
  102. } catch (IOException e) {}
  103. break;
  104. }
  105. }
  106.  
  107. }
  108.  

Para la ventanita tengo este programa
la etiqueta donde va la variable a mostrar se encuentra en la linea 16
Código: Text
  1. paqbascula;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class NewClass {
  7.     public static void main( String args[] ) {
  8.         IHM ihm = new IHM();
  9.         }
  10.     }
  11.  
  12. class IHM {
  13.     public IHM(){
  14.         // Instancia un objeto Label con una cadena para inicializarlo y
  15.        // que aparezca como contenido en el momento de su creación
  16.         Label miEtiqueta = new Label(la varible que se guarda en el programa anterior va acá, (en teoria es numBytes) );
  17.  
  18.        // Coloca la eqtiqueta sobre el objeto Frame
  19.         Frame miFrame = new Frame( "Ventana" );  
  20.         miFrame.setLayout( new FlowLayout() );    
  21.         miFrame.add( miEtiqueta );
  22.         miFrame.setSize( 250,150 );
  23.         miFrame.setVisible( true );
  24.  
  25.         // Instancia y registra un objeto receptor de eventos de ventana
  26.         // para concluir la ejecucion del programa cuando el Frame se
  27.         // cierres por accion del usuario sobre el
  28.         miFrame.addWindowListener( new Conclusion() );
  29.         }
  30.     }
  31.  
  32. class Conclusion extends WindowAdapter {
  33.     public void windowClosing( WindowEvent evt ) {
  34.         // Concluye el programa cuando se cierra la ventana
  35.         System.exit(0);
  36.         }
  37.     }
  38.  
Por favor necesito ayuda estoy desesperado, no se como llamar de una clase a otra dentro del mismo paquete además de los detalles mencionados en el código.
Me despido y muchas gracias de antemano..
Salu2 :D  :D  :D