title: How to turn a main Java class into a constructor?
body: <p>I have a hard time understanding how to convert an app into a callable class and vice versa in Java. Lets say I wrote a tabe that works correctly as main class. It generates the frame (window) with a panel, which is a table. Correct me if I am wrong.</p>
  
  <pre><code>    import java.awt.BorderLayout;
      import javax.swing.JFrame;
      import javax.swing.JScrollPane;
      import javax.swing.JTable;
  
      public class DisplayPanel {
        public static void main(String args[]) {
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3" },
              { "Row2-Column1", "Row2-Column2", "Row2-Column3" } };
          Object columnNames[] = { "Column One", "Column Two", "Column Three" };
          JTable table = new JTable(rowData, columnNames);
          JScrollPane scrollPane = new JScrollPane(table);
          frame.add(scrollPane, BorderLayout.CENTER);
          frame.setSize(300, 150);
          frame.setVisible(true);
        }
      }
  </code></pre>
  
  <p>I have a call
      JPanel p = new DisplayPanel();</p>
  
  <p>That should simply return a panel, generated from the code above. So, how can I change the code to have a callable DisplayPanel() class that returns a Jpanel? What is the name of this transformation (so that I can google it)?   </p>
  
  <hr>
  
  <p>Let me rephrase the question. I have two separate projects: a sandBox (shown above with main and working as expected) and a largeProject that should call a class (returning a function)  and display it within largeProject's own frame. My largeProject has:</p>
  
  <pre><code> JPanel p1 = new DisplayPanel1();
   add(p1, BorderLayout.NORTH);
  </code></pre>
  
  <p>I need to turn it into</p>
  
  <pre><code> JPanel p1 = new DisplayPanel1(); //this one is implemented, but the panel is very diffeernt, hence not reusable
   JPanel p2 = new DisplayPanel2();
   add(p1, BorderLayout.NORTH);
   add(p2, BorderLayout.SOUTH); 
  </code></pre>
  
  <p>So, turning DisplayPanel2(); into a frame doesn't really help. Also, just helping with this particular question is less useful than explaining the general principles. What is the logic behind this conversion.</p>
  
  <p>Thanks for pointing out the difference between DisplayPanel and JPanel.</p>