Search code examples
javaprogram-entry-point

How to turn a main Java class into a constructor?


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.

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

I have a call JPanel p = new DisplayPanel();

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)?


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:

 JPanel p1 = new DisplayPanel1();
 add(p1, BorderLayout.NORTH);

I need to turn it into

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

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.

Thanks for pointing out the difference between DisplayPanel and JPanel.


Solution

  • Instead of creating a JFrame and make it visible, your DisplayPanel should instead be the JFrame.

    One way to make this transformation is to make your class extend JFrame, and put your code in a constructor, using this instead of a new JFrame:

    public class DisplayPanel extends JFrame {
    
      public DisplayPanel() {
        this.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);
        this.add(scrollPane, BorderLayout.CENTER);
        this.setSize(300, 150);
        // this.setVisible(true); this line should probably be called by the caller class now
      }
    }
    

    Note that you may also pass the columns and rows as constructor arguments:

    public class DisplayPanel extends JFrame {
    
      public DisplayPanel(Object[][] rowData, Object[] columnNames) {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTable table = new JTable(rowData, columnNames);
        JScrollPane scrollPane = new JScrollPane(table);
        this.add(scrollPane, BorderLayout.CENTER);
        this.setSize(300, 150);
        // this.setVisible(true); this line should probably be called by the caller class now
      }
    }