9 Replies - 204 Views - Last Post: 17 March 2018 - 07:37 AM Rate Topic: -----

#1 brotherofninth   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 14-March 16

Adding JPanel to JFrame but nothing change

Posted 17 March 2018 - 05:28 AM

This is the JFrame

public class MainContainer extends javax.swing.JFrame {

    private static final MainContainer NEW_MAIN = new MainContainer();
    private static final AudioListScreen ALS = new AudioListScreen();
    private static final RecordingScreen RCS = new RecordingScreen();
    private static final RunningScreen RS = new RunningScreen();
    private static final StartingScreen SS = new StartingScreen();
    
    /**
     * Creates new form Main
     */
    public MainContainer() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainContainer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainContainer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainContainer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainContainer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                
                MainContainer.createStartingScreen();
                NEW_MAIN.invalidate();
                NEW_MAIN.validate();
 
            }
        });
    }

    public static void createStartingScreen(){

        NEW_MAIN.setTitle("Tool");
        NEW_MAIN.setResizable(false);
        NEW_MAIN.setLocationRelativeTo(null);
        NEW_MAIN.setVisible(true);
        
        NEW_MAIN.getContentPane().add(SS);
    
    }    

}



No matter what I do, the window remains the same (Blank...).
It supposes replacing/adding the components of other JPanel to the JFrame.
I am so clueless about this, please let me if I do anything wrong.
Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Adding JPanel to JFrame but nothing change

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 708
  • View blog
  • Posts: 5,409
  • Joined: 25-December 13

Re: Adding JPanel to JFrame but nothing change

Posted 17 March 2018 - 06:25 AM

I see several problems with the code:
1) too many static variables - remove the static modifierr
2) setVisible called before end of building GUI - move after all adds
3) don't build the GUI in the main method
Was This Post Helpful? 0
  • +
  • -

#3 brotherofninth   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 14-March 16

Re: Adding JPanel to JFrame but nothing change

Posted 17 March 2018 - 06:48 AM

public class MainContainer extends javax.swing.JFrame {

    //remove the static modifierr
    private  final AudioListScreen ALS = new AudioListScreen();
    private  final RecordingScreen RCS = new RecordingScreen();
    private  final RunningScreen RS = new RunningScreen();
    //private  final StartingScreen SS = new StartingScreen();
    
    /**
     * Creates new form Main
     */
    public MainContainer() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainContainer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainContainer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainContainer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainContainer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        MainContainer NEW_MAIN = new MainContainer();
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                
                createStartingScreen(NEW_MAIN);

            }
        });
    }

    public static void createStartingScreen(MainContainer NEW_MAIN){

        NEW_MAIN.setTitle("tool");
        NEW_MAIN.setResizable(false);
        NEW_MAIN.setLocationRelativeTo(null);

        NEW_MAIN.getContentPane().add(new StartingScreen());
        
        NEW_MAIN.invalidate();
        NEW_MAIN.validate();
        
        
        // move after all adds
        NEW_MAIN.setVisible(true);
    
    }    

}




Thanks for your reply. I did some changes to my code with your advice, it still can't create other components of the jpanel.

This post has been edited by ndc85430: 17 March 2018 - 06:55 AM
Reason for edit:: Removed quote of previous post. Please just pres "Reply" in future.

Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 708
  • View blog
  • Posts: 5,409
  • Joined: 25-December 13

Re: Adding JPanel to JFrame but nothing change

Posted 17 March 2018 - 06:54 AM

Can you remove all the extra class references and just use java se classes for testing.
I can not compile and execute the posted code because of all of the missing classes.
Do not post the other classes, they are not needed to solve the problem.
Use a JLabel instead of StartingScreen for something to show.

This post has been edited by NormR: 17 March 2018 - 07:04 AM

Was This Post Helpful? 0
  • +
  • -

#5 brotherofninth   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 14-March 16

Re: Adding JPanel to JFrame but nothing change

Posted 17 March 2018 - 07:10 AM

public class MainContainer extends javax.swing.JFrame {

    //remove the static modifierr
//    private  final AudioListScreen ALS = new AudioListScreen();
//    private  final RecordingScreen RCS = new RecordingScreen();
//    private  final RunningScreen RS = new RunningScreen();
    //private  final StartingScreen SS = new StartingScreen();
    
    /**
     * Creates new form Main
     */
    public MainContainer() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainContainer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainContainer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainContainer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainContainer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        MainContainer NEW_MAIN = new MainContainer();
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                
                createStartingScreen(NEW_MAIN);

            }
        });
    }

    public static void createStartingScreen(MainContainer NEW_MAIN){

        NEW_MAIN.setTitle("tool");
        NEW_MAIN.setResizable(false);
        NEW_MAIN.setLocationRelativeTo(null);

        //testing class
        
        JPanel test = new JPanel();
        JLabel label = new JLabel("testing label");
        label.setBounds(10, 20, WIDTH, HEIGHT);
        test.add(label);
        
        NEW_MAIN.getContentPane().add(test);
        
        NEW_MAIN.invalidate();
        NEW_MAIN.validate();
        
        
        // move after all adds
        NEW_MAIN.setVisible(true);
    
    }    
}




Sorry about that. I think it is better now.

This post has been edited by ndc85430: 17 March 2018 - 07:11 AM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#6 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 888
  • View blog
  • Posts: 3,587
  • Joined: 13-June 14

Re: Adding JPanel to JFrame but nothing change

Posted 17 March 2018 - 07:12 AM

brotherofninth, there's no need to quote the previous post. Please just use the "Reply" button.
Was This Post Helpful? 0
  • +
  • -

#7 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 708
  • View blog
  • Posts: 5,409
  • Joined: 25-December 13

Re: Adding JPanel to JFrame but nothing change

Posted 17 March 2018 - 07:15 AM

Quote

it is better now.

Does that mean the code is working?
Was This Post Helpful? 0
  • +
  • -

#8 brotherofninth   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 14-March 16

Re: Adding JPanel to JFrame but nothing change

Posted 17 March 2018 - 07:27 AM

It remains the same, bro

This post has been edited by ndc85430: 17 March 2018 - 07:29 AM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#9 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 708
  • View blog
  • Posts: 5,409
  • Joined: 25-December 13

Re: Adding JPanel to JFrame but nothing change

Posted 17 March 2018 - 07:30 AM

I got it to work by commenting out the call to initComponents() and setting the size manually.

It looks like you are trying to mix IDE generated code with your code. That can be very challenging.
Was This Post Helpful? 1
  • +
  • -

#10 brotherofninth   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 14-March 16

Re: Adding JPanel to JFrame but nothing change

Posted 17 March 2018 - 07:37 AM

This means a lot to me, maybe I can modify its initComponents() to get it works. thanks :)/>

This post has been edited by ndc85430: 17 March 2018 - 07:37 AM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1