title: How do I convert a JLabel into a String & vice versa?
body: <p>I am trying to build a TicTacToe game which takes the players input through the use of a JDialog box <code>dialogBox</code> &amp; a JTextField <code>textField</code>. Taking the input &amp; giving names to each player in the game after clicking the submit button. However, the problem I have is that I am not able to convert a JLabel into a string &amp; vice versa. </p>
  
  <p>I want to get the text input from <code>textField</code> and assign it to <code>jlblStatus</code> in order to display the user's name. I've created <code>makeString ()</code> that uses XML Encoder and ByteArrayOutputStream to change the <code>jlblStatus</code> into a string so that I can then assign the input from the <code>textField</code> to <code>jlblStatus</code>. Then I can convert <code>jlblStatus</code> back into a JLabel using the <code>makeLabel()</code> using the XML Decoder and ByteArrayInputStream. </p>
  
  <p>However, I am getting compilation errors <code>incompatible types: String cannot be converted to JLabel</code> &amp; <code>incompatible types: JLabel cannot be converted to String</code> which means that <code>makeString()</code> was not able to convert <code>jlblStatus</code> into a string. </p>
  
  <p><strong><em>The class which have the makeString() &amp; makeLabel()</em></strong></p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import javax.swing.border.LineBorder;
  import java.io.ByteArrayOutputStream;
  import java.io.ByteArrayInputStream;
  import java.beans.XMLDecoder;
  import java.beans.XMLEncoder;
  
  
  public class TicTacToeFrame extends JFrame {
     public char whoseTurn = 'X';   
  
     public Cell [] [] cells = new Cell [3] [3];
  
     JLabel jlblStatus = new JLabel ("X's turn to play");
  
     public boolean gameOver = false;
  
     public String players;
  
     public JPanel panel;
  
     JDialog dialogBox;
  
     JTextField textField;
  
     public TicTacToeFrame () {
       panel = new JPanel (new GridLayout (3, 3, 0, 0));
       for (int i = 0; i &lt; 3; i++) {
          for (int j = 0; j &lt; 3; j++) {
              panel.add (cells [i] [j] = new Cell(this));
          }
      }
  
      panel.setBorder (new LineBorder (Color.red, 1));
      jlblStatus.setBorder (new LineBorder (Color.yellow, 1));
  
      add (panel, BorderLayout.CENTER);
      add (jlblStatus, BorderLayout.SOUTH);
    }
  
    public class miniFrame implements ActionListener {
      public miniFrame () {
        JFrame Frame = new JFrame ();
        dialogBox = new JDialog (Frame, "TicTacToe User Input", true);
        dialogBox.setLayout (new FlowLayout ());
        textField = new JTextField ("ENTER YOUR NAME", 20);
        JButton Submit = new JButton ("Submit");
        JLabel Player_X = new JLabel ("Player X: ");
        Submit.addActionListener (this);
  
        dialogBox.add (Player_X);
        dialogBox.add (textField);
        dialogBox.add (Submit);
  
        dialogBox.setSize (400, 150);
        dialogBox.setVisible (true);
     }
  
      public void actionPerformed (ActionEvent e) {
        String s = e.getActionCommand ();
        if (s.equals ("Submit")) {
           playerOne = textField.getText () + "'s turn to play";
           makeString (jlblStatus);
           jlblStatus = playerOne;
           makeLabel (playerOne);
        }
     }
   }
  
   String makeString (JLabel jl) {//This is the method which converts the Jlabel into a String
     ByteArrayOutputStream baos = new ByteArrayOutputStream ();
     XMLEncoder e = new XMLEncoder (baos);
     e.writeObject (jl);
     e.close ();
     return new String (baos.toByteArray ());
   }//CLOSE TOSTRING METHOD
  
   JLabel makeLabel (String str) {//This is the method which converts the String into a JLabel
     XMLDecoder d = new XMLDecoder (new ByteArrayInputStream (str.getBytes ()));
     JLabel label = (JLabel) d.readObject ();
     d.close ();
     return label;
   }//CLOSE FROMSTRING METHOD
  
   //ISFULL METHOD
   public boolean isFull () {
      for (int i = 0; i &lt; 3; i++) {
          for (int j = 0; j &lt; 3; j++) {
              if (cells [i] [j].getToken () == ' ') {
                return false;//FALSE BOOLEAN IS RETURNED 
              }
          }
      }
      return true;
   }
  
   public boolean isWon (char token) {
     for (int i = 0; i &lt; 3; i++) {
        if ((cells [i] [0].getToken () == token) &amp;&amp; (cells [i] [1].getToken () == token) &amp;&amp; (cells [i] [2].getToken () == token)) {
            return true;
        }
     }
  
     for (int j = 0; j &lt; 3; j++) {
        if ((cells [0] [j].getToken () == token) &amp;&amp; (cells [1] [j].getToken () == token) &amp;&amp; (cells [2] [j].getToken () == token)) {
           return true;
        }
     }
  
     if ((cells [0] [0].getToken() == token) &amp;&amp; (cells [1] [1].getToken () == token) &amp;&amp; (cells [2] [2].getToken () == token)) {
         return true;
     }
  
     if ((cells [0] [2].getToken() == token) &amp;&amp; (cells [1] [1].getToken () == token) &amp;&amp; (cells [2] [0].getToken () == token)) {
        return true;
     }
  
     return false;
   }
  }
  </code></pre>
  
  <p><strong><em>The complilation errors</em></strong></p>
  
  <pre><code>TicTacToeFrame.java:70: error: incompatible types: String cannot be converted to JLabel
        jlblStatus = playerOne;
                     ^
  TicTacToeFrame.java:71: error: incompatible types: JLabel cannot be converted to String
        makeLabel (jlblStatus);
                   ^
  Note: Some messages have been simplified; recompile with - 
  Xdiags:verbose to get full output
  2 errors
  </code></pre>
  
  <p>How would I solve this problem and convert a Jlabel into a String &amp; vice versa?</p>