Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Sierra

Thread: InputStream TextField

  1. #1
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default InputStream TextField

    Hi everyone,
    I want to show a variable type InputStream in a TextField
    my code :


    Java Code:
    byte [] readbuffer = new byte [20];
                                try {
                                    while (inputstream.available () > 0 )
                                    {
                                        int nymBytes = inputstream.read(readbuffer);
                                    }
                                    String tr = new String(readbuffer);
                                    Traffic.setText(tr);
    Traffic is a textfield;
    readbuffer is a Thread;

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: InputStream TextField

    Great, go on.
    DarrylBurke likes this.
    I like likes!

  3. #3
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: InputStream TextField

    lol thx but it dosn't work ! can u help me pls

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: InputStream TextField

    Quote Originally Posted by taptaptill View Post
    Traffic is a textfield
    Learn and respect the Java coding conventions: a variable name should start with a lowercase letter.
    Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: InputStream TextField

    If you do not write a appropriate problem description you won't get help because no one understands what actually your problem is.
    IF you say: "Hey I created a textfield, but it does not show up on my screen" the solution is completely different from "I created a text field, but the text I set to be in there does not appear in it", got it?
    And again: "It does not work" is NOT an adequate problem description... though it may be true in all cases... ;)

    Then if you have that you should post the code that does exactly what you wrote as a problem description - completely! I do not see any containing method above, not I see the class you wrote it in. Both might be vital to understand where the problem sits.

    Furtermore - "readbuffer" is not a Thread, it is an Array, you declared it to be that ... 7 lines above... so as long as I do not see what you really have as code I can't tell you what is wrong.
    I like likes!

  6. #6
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: InputStream TextField

    ya u got it right my bad !.
    so what I mean by it dosn't work is it dosn't show in the textfield & this is my code :
    Java Code:
    package javatool;
    
    import com.sun.comm.Win32Driver;
    import java.io.*;
    import java.util.*;
    import javax.comm.*;
    import javax.swing.*;
    
    
    public class JavaSerialPort extends Thread implements Runnable,SerialPortEventListener {
    
            /**
         * 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.
         */
       
        // Variables declaration - do not modify
        private javax.swing.JComboBox PortList;
        private javax.swing.JToggleButton StartCapture;
        private javax.swing.JTextField Traffic;
        private javax.swing.JFrame JFrame;
        private javax.swing.GroupLayout Layout;
        private CommPortIdentifier portId;
        private SerialPort serialPort;
        private BufferedReader fluxLecture;
        private boolean running;
        Thread readthread;
        InputStream inputstream;
        static String Port;
        
         // End of variables declaration
    	/**
    	 * Constructeur qui récupère l'identifiant du port et lance l'ouverture.
    	 */
    	public JavaSerialPort(String port ) {
                     initComponents();
    		//initialisation du driver
    		Win32Driver w32Driver = new Win32Driver();
    		w32Driver.initialize();
    		//récupération de l'identifiant du port
    		try {
    			portId = CommPortIdentifier.getPortIdentifier(port);
    		} catch (NoSuchPortException e) {
    		}
    		
    		//ouverture du port
    		try {
    			serialPort = (SerialPort) portId.open("Micropixel", 2000);
    		} catch (PortInUseException e) {
    		}
    		//récupération du flux
    		try {
    			inputstream = serialPort.getInputStream();
    		} catch (IOException e) {
    		}
    		//ajout du listener
    		try {
    			serialPort.addEventListener(this);
    		} catch (TooManyListenersException e) {
    		}
    		//paramétrage du port
    		serialPort.notifyOnDataAvailable(true);
    		try {   
    			serialPort.setSerialPortParams(
    				serialPort.getBaudRate(),
    				serialPort.getDataBits(),
    				serialPort. getStopBits(),
    				serialPort.getParity());
    		} catch (UnsupportedCommOperationException e) {
    		}
    		System.out.println("port ouvert, attente de lecture");
    	}
        
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents  () {
    
            PortList = new javax.swing.JComboBox();
            StartCapture = new javax.swing.JToggleButton();
            Traffic = new javax.swing.JTextField();
            JFrame = new javax.swing.JFrame();
            JFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            JFrame.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    
            PortList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "COM1", "COM2" }));
            PortList.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    PortListActionPerformed(evt);
                }
            });
    
            StartCapture.setText("Start Capture");
            StartCapture.setToolTipText("");
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(JFrame.getContentPane());
            JFrame.getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap(46, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(StartCapture)
                            .addGap(95, 95, 95))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(PortList, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(Traffic, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(38, 38, 38))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(122, 122, 122)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(PortList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Traffic, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(88, 88, 88)
                    .addComponent(StartCapture)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            PortList.setSelectedIndex(0);
    
            JFrame.pack();
            
        }// </editor-fold>
    
        private void PortListActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
            PortList.setSelectedIndex(0);
            String SI ; 
                   SI = (String)PortList.getSelectedItem();
                   Port = SI;
        }
    public void TrafficShow (String S)
    {
        
    }
        /**
         * @param args the command line arguments
         */
        
        
        @Override
    	public void run() {
    		running = true;
    		while (running) {
    			try {
    				Thread.sleep(2000);
    			} catch (InterruptedException e) {
    			}
    		}
    		//fermeture du flux et port
    		try {
    			inputstream.close();
    		} catch (IOException e) {
    		}
    		serialPort.close();
    	}
    	/**
    	 * Méthode de gestion des événements.
    	 */
    	public void serialEvent(SerialPortEvent event) {
    		//gestion des événements sur le port :
    		//on ne fait rien sauf quand les données sont disponibles
    		switch (event.getEventType()) {
    			case SerialPortEvent.BI :
    			case SerialPortEvent.OE :
    			case SerialPortEvent.FE :
    			case SerialPortEvent.PE :
    			case SerialPortEvent.CD :
    			case SerialPortEvent.CTS :
    			case SerialPortEvent.DSR :
    			case SerialPortEvent.RI :
    			case SerialPortEvent.OUTPUT_BUFFER_EMPTY :
    				break;
    			case SerialPortEvent.DATA_AVAILABLE :
                                
                                byte [] readbuffer = new byte [20];
                                try {
                                    while (inputstream.available () > 0 )
                                    {
                                        int nymBytes = inputstream.read(readbuffer);
                                    }
                                    String tr = new String(readbuffer);
                                    System.out.println(tr);
                                    Traffic.setText(tr);
                                    
                                }catch (IOException e) {System.out.println(e);}
                                    
    				
    				break;
    		}
    	}
    	/*
    	 * Permet l'arrêt du thread
    	 */
    	public void stopThread() {
                    
    		running = false;
    	}
    	/**
    	 * Méthode principale de l'exemple.
    	 */
    	public static void main(String[] args) {
    		//Récuperation du port en argument
                 java.awt.EventQueue.invokeLater(new Runnable() {
                     @Override
                public void run() {
                    new TrafficSerialPort().setVisible(true);}});
    		
                     
    		//lancement de l'appli
    		JavaSerialPort modeEve=new JavaSerialPort("COM1");
    		modeEve.start();
    		//"interface utilisateur"
    		System.out.println("taper q pour quitter");
    		//construction flux lecture
    		BufferedReader clavier =
    			new BufferedReader(new InputStreamReader(System.in));
    		//lecture sur le flux entrée.
    		try {
    			String lu = clavier.readLine();
    			while (!lu.equals("q")) {
    			}
    		} catch (IOException e) {
    		}
    		modeEve.stopThread();
    	}
    }

  7. #7
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: InputStream TextField

    Are you sure the last thing you get as text is not an empty string from your stream?
    Why do you add "Traffic" two times to groups?
    Please keep to the naming convention - variables start not with caps.
    I like likes!

  8. #8
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: InputStream TextField

    I'm sure that what I'm getting is not an empty string because i tested it without the textfield and the stream shows so I don't know why it dosn't show on the textfield.
    For the add Traffic that code was generated by netbeans so I don't know why either but what i see is all the components are added twice one for the VerticalGroup & one for the HorizontalGroup.

    For what's worth : in the textfield I'm getting "COM1" or "COM2" as I change the Selected Item in the JComboBox.I tryed to find the code that do that but I can't seem to find it.
    Last edited by taptaptill; 12-18-2012 at 10:01 AM.

  9. #9
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: InputStream TextField

    It would be enough if the LAST chunk you have is an empty string, no matter what before. Try placing a static text in the "setText" method and see if that works.
    e.g. if the last character you get in your while loop is a linefeed you wont see anything as you read and so overwrite the buffer.

    Java Code:
    while (inputstream.available () > 0 )
    {
        int nymBytes = inputstream.read(readbuffer);
    }
    String tr = new String(readbuffer);
    System.out.println(tr);
    Traffic.setText("TEST"); // Traffic.setText(tr);
    I like likes!

  10. #10
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: InputStream TextField

    So thank you for the answers, but I call the "Traffic" from another class called TrafficSerialPort because it contains all the charts (Swing) in my program.
    then this line is for anyone interrested:
    Java Code:
    TrafficSerialPort.Traffic.setText(tr);
    Thanks again & till next time.
    Last edited by taptaptill; 12-19-2012 at 10:13 AM.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: InputStream TextField

    Quote Originally Posted by Sierra View Post
    Why do you add "Traffic" two times to groups?
    That's how GroupLayout works. Which is why many of use don't even read the code if it's been generated by a visual designer.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. mark in InputStream
    By jessJ2EE in forum Android
    Replies: 17
    Last Post: 06-03-2012, 07:23 PM
  2. Who did the job for the InputStream
    By neosky11 in forum New To Java
    Replies: 9
    Last Post: 04-15-2012, 04:05 AM
  3. Creating an InputStream
    By c26354 in forum New To Java
    Replies: 10
    Last Post: 07-12-2011, 05:48 AM
  4. InputStream problem
    By javabarn in forum New To Java
    Replies: 10
    Last Post: 06-29-2010, 05:09 PM
  5. Java InputStream
    By Bill88 in forum New To Java
    Replies: 10
    Last Post: 09-21-2009, 03:40 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •