Results 1 to 11 of 11
Thread: InputStream TextField
- 12-13-2012, 10:32 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
InputStream TextField
Hi everyone,
I want to show a variable type InputStream in a TextField
my code :
Traffic is a textfield;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);
readbuffer is a Thread;
- 12-13-2012, 11:49 PM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: InputStream TextField
Great, go on.
I like likes!.gif)
- 12-14-2012, 11:47 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
Re: InputStream TextField
lol thx but it dosn't work ! can u help me pls
- 12-15-2012, 12:14 AM #4
Re: InputStream 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
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-15-2012, 11:02 AM #5
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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!.gif)
- 12-15-2012, 03:06 PM #6
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
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(); } }
- 12-16-2012, 06:09 PM #7
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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!.gif)
- 12-18-2012, 08:45 AM #8
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
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 09:01 AM.
- 12-18-2012, 12:11 PM #9
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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!.gif)
- 12-19-2012, 09:09 AM #10
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
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:
Thanks again & till next time.Java Code:TrafficSerialPort.Traffic.setText(tr);
Last edited by taptaptill; 12-19-2012 at 09:13 AM.
- 12-19-2012, 10:34 AM #11
Similar Threads
-
mark in InputStream
By jessJ2EE in forum AndroidReplies: 17Last Post: 06-03-2012, 06:23 PM -
Who did the job for the InputStream
By neosky11 in forum New To JavaReplies: 9Last Post: 04-15-2012, 03:05 AM -
Creating an InputStream
By c26354 in forum New To JavaReplies: 10Last Post: 07-12-2011, 04:48 AM -
InputStream problem
By javabarn in forum New To JavaReplies: 10Last Post: 06-29-2010, 04:09 PM -
Java InputStream
By Bill88 in forum New To JavaReplies: 10Last Post: 09-21-2009, 02:40 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks