We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Best way to get text from a Jtext area and send it in a DataGram packet

mitchguy
mitchguy asked
on
Medium Priority
260 Views
Last Modified: 2010-03-31
I can't seem to get past a compile error of
 incompatible types
 found byte[]
required java.lang.Byte[]
Maybe I need to rework everything, I feel like I'm doing too many conversions of the text
from a string to bytes to char, there must be a better way, although I guess DatagramPacket() is defined as
DatagramPacket(byte[] buf, int length, InetAddress address, int port)  so I do need the text converted to a byte[]
Here's my code:
I have a Jtext area and made a dataStructure to hold the data from there and also a few integers, which
the values will be generated depending on what radio buttons are selected on another part of the GUI.

My data structure to hold the jText text and radio button selections
public class guiPacket
{
  public char [] text = new char[256];
  RadioData [] radioData = new RadioData[25];
  public class guiPacket(){}
   public class RadioData
   {
    int radioSelected;
    int radioLevel;
    public RadioData(){}
   }
  public byte [] packageText()
  {
    try
    {
      ByteArrayOutputStream textArray = new ByteArrayOutputStream();
      DataOutputStream out = new DataOutputStream(textArray);
      out.write(new String(text).getBytes("UTF-8"));
      return textArray.toByteArray();
    }
   catch(IOException e)
    {
      return null;
    }
}

I have a send Jbutton below my text area, which is supposed to trigger the UDP packet to be
filled with textArea data and sent to another program, my callback function for the JButton

public void sendTextAreaDataActionPerformed(java.awt.event.ActionEvent evt)
{
  String textAreaString = textArea.getText();
  guiPacket.text = textAreaString.toCharArray();
  newTextPacket = true;
//newTextPacket is a boolean declared in my top level app class and is used in a server thread to                           //package and send the UDP packet when when updated to true
}

my run function for my server thread

public void run( )
{
  while(true)
   {
       if(newTextPacket)
       {
         Byte [] byteArray = guiPacket.packageText();
//THIS IS WHERE MY ERROR HAPPENS!!! on the call to guiPacket.packageText();
//the function is defined to return byte [] via    return textArray.toByteArray();
// incompatible types
// found byte[]
//required java.lang.Byte[]


         dataPacket = new DatagramPacket(byteArray,byteArray.length,address,serverPort);
         socket.send(dataPacket);
       }
   }

}










Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2004

Commented:
change

         Byte [] byteArray = guiPacket.packageText();

to

         byte [] byteArray = guiPacket.packageText();
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Byte is a wrapper class for byte - you cannot directly type-cast a byte[] to a Byte[]

Author

Commented:
I was just about to update that I changed Byte to byte and got it to compile
writing out everything pointed out the obvious to me, But I still feel like I'm doing things the hard way,
maybe doing too many steps, is there a better way to do what I'm doing?
CERTIFIED EXPERT
Top Expert 2004

Commented:
Instead of having a global (static?) boolean to flag when there's some text to be sent, why not just add the bytearray to a Queue, and have the Datagram sending code sending data when there's something in the queue?

It really depends on wht you are doing and how you are doing it...

You need a byte array, and you are converting the string into one in the best way I can think of...

Tim
Mayank SPrincipal Technologist
CERTIFIED EXPERT
Commented:
>> public char [] text = new char[256];

- is public. It should be private and you should expose it through an accessor (getter) method.

>> RadioData [] radioData = new RadioData[25];

can also be made private similarly.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
>>Tim
I'm always interested in writing better code, I definitely would like to learn
what your method is for replacing the global boolean flag, can you give me
a code example of what you mean and an explanation of why it's a better way to do it?
CERTIFIED EXPERT
Top Expert 2004
Commented:
basically, you'd be looking at implementing a Queue (or a java.util.List if you are not on Java 5)

And then you'd be looking at the producer/consumer model

http://java.sun.com/docs/books/tutorial/essential/threads/synchronization.html

So your Swing app would be the producer and it would add stuff to the list, and your Datagram thread would be the consumer, which would send these out as they arrived in the queue

Tim
CERTIFIED EXPERT
Top Expert 2004

Commented:
As I said though, this might be totally unneccessary complication of your project though...

The only concern I have with your code currently is what happens if the user enters two packets of data in quick succession before the Datagram run() method gets a chance to fire?

I guess one of the packets will be lost?

Tim

Author

Commented:
In this particular instance, I think I'm ok, because the user has to type in the text and then
hit send so i don't think there's much of a chance of them typing fast enough to send another
one before the first one is processed, but I do see how easily this could be a problem, so I think I'll implement it for the sake of learning it, besides better safe than sorry, maybe it is applicable to this
program.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.