Search code examples
javaswingjframelayout-managernull-layout-manager

Nothing is display


The code written below only shows this is it in console why is it not showing the UI which i have made i have used windows builder to create the UI.

 import javax.swing.JFrame;

    public class getData extends JFrame {

         JFrame frame = new JFrame();
         JTextArea textArea = new JTextArea();
         JTextArea textArea_1 = new JTextArea();
         JTextArea textArea_2 = new JTextArea();
         JLabel    lblNewLabel = new JLabel("BOX");
         JLabel    lblPlc = new JLabel("PLC");
         JLabel    lblHmi = new JLabel("HMI");
         JButton   btnSubmit = new JButton("SUBMIT");
         JButton   btnExport = new JButton("EXPORT");


        public getData() {
            initGUI();
    }   
        void initGUI(){

            getContentPane().setLayout(null);
            this.textArea.setBounds(28, 50, 105, 162);

            getContentPane().add(this.textArea);
            this.textArea_1.setBounds(183, 50, 105, 162);

            getContentPane().add(this.textArea_1);
            this.textArea_2.setBounds(319, 50, 105, 162);

            getContentPane().add(this.textArea_2);
            this.lblNewLabel.setFont(new Font("Times New Roman", Font.PLAIN, 13));
            this.lblNewLabel.setBounds(28, 25, 46, 14);

            getContentPane().add(this.lblNewLabel);
            this.lblPlc.setFont(new Font("Times New Roman", Font.PLAIN, 13));
            this.lblPlc.setBounds(183, 25, 46, 14);

            getContentPane().add(this.lblPlc);
            this.lblHmi.setFont(new Font("Times New Roman", Font.PLAIN, 13));
            this.lblHmi.setBounds(317, 25, 46, 14);

            getContentPane().add(this.lblHmi);
            this.btnSubmit.setFont(new Font("Times New Roman", Font.PLAIN, 13));
            this.btnSubmit.setBounds(22, 228, 89, 23);

            getContentPane().add(this.btnSubmit);
            this.btnExport.setFont(new Font("Times New Roman", Font.PLAIN, 13));
            this.btnExport.setBounds(300, 228, 89, 23);
            getContentPane().add(this.btnExport);
            frame.setSize(500, 500);
            frame.add(getContentPane());        
            frame.setTitle("Bar Code Scanner");
            frame.setVisible(true);     
    }        
        public static void main(String[] args) {
            System.out.println("This Is It");       
        }   
    }

Also it displays when i switch the tab from source to design. Thanks in Advance.


Solution

  • In intiGUI() method, replace frame with 'this'.

            frame.setSize(500, 500);
            frame.add(getContentPane());        
            frame.setTitle("Bar Code Scanner");
            frame.setVisible(true); 
    

    Use the below code:

            this.setSize(500, 500);
            this.add(getContentPane());        
            this.setTitle("Bar Code Scanner");
            this.setVisible(true); 
    

    Here current class itself is a JFrame and you have added the components to current content pane. hence you need to set size and visibility to the current JFrame i.e. current class.

    public static void main(String[] args) 
    {
    
      System.out.println("This Is It"); 
    
       new getData();     // ** You need to instantiate the class
    
    }