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

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()
      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( )
         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);


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


         Byte [] byteArray = guiPacket.packageText();


         byte [] byteArray = guiPacket.packageText();
Mayank SAssociate Director - Product EngineeringCommented:
Byte is a wrapper class for byte - you cannot directly type-cast a byte[] to a Byte[]
mitchguyAuthor 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?
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

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...

Mayank SAssociate Director - Product EngineeringCommented:
>> 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.
mitchguyAuthor Commented:
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?
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

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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?

mitchguyAuthor 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.