Results 1 to 10 of 10
  1. #1
    cattox is offline Member
    Join Date
    Mar 2009
    Posts
    8
    Rep Power
    0

    Default Panel components don't show

    Hi all,

    I'm fairly new to swing, so I believe this may be something simple. Hope some of you can give me a hand.
    This is part of a more complex scenario but I managed to put this in a simple example.

    I'm using Netbeans and I'm trying to create a Frame containing a panel with some checkboxes (added dynamically by the frame).

    If I use the visual editor to create the entire structure with native components it works well, but the thing is I need a custom panel and this is where it breaks.

    When I execute the application, I only see a blank frame with no content.

    Code follows

    Frame:

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package contacorrente;
    
    import java.util.HashSet;
    import java.util.Set;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    
    /**
     *
     * @author 
     */
    public class Test extends javax.swing.JFrame {
    
        /**
         * Creates new form Test
         */
        public Test() {
            initComponents();
            this.testPanel1.add(new JCheckBox("ergcdf"));
            this.testPanel1.add(new JCheckBox("ergcdf1"));
            this.testPanel1.add(new JCheckBox("ergcdf2"));
            this.testPanel1.add(new JCheckBox("ergcdf3"));
            this.testPanel1.add(new JCheckBox("ergcdf4"));
            this.testPanel1.validate();
            this.validate();
        }
    
        /**
         * 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() {
    
            testPanel1 = new contacorrente.TestPanel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            testPanel1.setName("testPanel1"); // NOI18N
    
            javax.swing.GroupLayout testPanel1Layout = new javax.swing.GroupLayout(testPanel1);
            testPanel1.setLayout(testPanel1Layout);
            testPanel1Layout.setHorizontalGroup(
                testPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
            );
            testPanel1Layout.setVerticalGroup(
                testPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE)
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(testPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(testPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, 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(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Test().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private contacorrente.TestPanel testPanel1;
        // End of variables declaration
    }
    I'll not paste the panel code since it is simply an empty panel (no custom code)

    Help please..

    Thanks in advance

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,389
    Rep Power
    5

    Default Re: Panel components don't show

    Well, I normally advise folks to stay away from GUI creators and visual editors (at least until they understand what is going on). But to get to your question, here is snippet to do what you want (note: doesn't take into account other components and their placement).

    Java Code:
    JFrame frame = new JFrame();
    JPanel panel = new JPane();
    frame.add(panel);  
    frame.setVisible(true);
    Check out The Java™ Tutorial for more info.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    cattox is offline Member
    Join Date
    Mar 2009
    Posts
    8
    Rep Power
    0

    Default Re: Panel components don't show

    Hi Jim,

    and thanks for the quick response.
    Maybe I wasn't able to express my problem properly. My issue is not showing the panel, this is showing right (I put some background color on panel and frame just to be sure). The issue here is that the checkboxes inside the panel are not showing.. :(

    cattox

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,389
    Rep Power
    5

    Default Re: Panel components don't show

    My apologies. I didn't read it carefully enough. In the example I provide, you can add your components to the panel which was added to the JFrame. This typical use of panels. Quite often, more than one panel is added to a frame and then other components added to the panel(s);

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    cattox is offline Member
    Join Date
    Mar 2009
    Posts
    8
    Rep Power
    0

    Default Re: Panel components don't show

    I'm aware of that and I'm just trying to do that, but my code doesn't seem to be working.
    I need to add the components from the frame class. This should be possible, but I must be missing something. Can you help me with that?

    Thanks again,

    cattox

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,389
    Rep Power
    5

    Default Re: Panel components don't show

    In the snippet of your code below:

    Java Code:
     public Test() {        initComponents();
            this.testPanel1.add(new JCheckBox("ergcdf"));
            this.testPanel1.add(new JCheckBox("ergcdf1"));
            this.testPanel1.add(new JCheckBox("ergcdf2"));
            this.testPanel1.add(new JCheckBox("ergcdf3"));
            this.testPanel1.add(new JCheckBox("ergcdf4"));
            this.testPanel1.validate();
            this.validate();
        }
    Where is initComponents and where is testPanel1 created. If testPanel1 is a JPanel it should be added to the JFrame.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    cattox is offline Member
    Join Date
    Mar 2009
    Posts
    8
    Rep Power
    0

    Default Re: Panel components don't show

    The initComponents is the netbeans auto generated method that you can find in my first post.
    testPanel1 is a simple subclass of JPanel.

    If testPanel1 is a JPanel it should be added to the JFrame.
    This is being done inside the auto generated method..

    :( any thoughts?

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Panel components don't show

    Quote Originally Posted by cattox View Post
    :( any thoughts?
    Yes. Dump the visual designer, and code the GUI by hand. A visual designer is a useful, productivity-enhancing tool for specialized professionals, not a beginner's crutch. Hand coding is tons easier.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    cattox is offline Member
    Join Date
    Mar 2009
    Posts
    8
    Rep Power
    0

    Default Re: Panel components don't show

    I had the luck that someone in other forum was actually willing to help me.

  10. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: Panel components don't show

    testPanel uses a GroupLayout. You can't just add components to the panel. You also need to specify all the constraints that the GroupLayout requires.

    I have no idea how GroupLayout works. People rarely use the GroupLayout for manual coding. Only IDE's seem to use it because it is rather complex. If you feel like learning how to use it then check out How to Use GroupLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container).

    Its either that or code the GUI manually which is what most people do.

Similar Threads

  1. show panel when button clicked
    By Aggror in forum New To Java
    Replies: 6
    Last Post: 10-14-2010, 04:05 PM
  2. Window needs to be re-sized to show components?
    By mbutler755 in forum AWT / Swing
    Replies: 2
    Last Post: 10-09-2010, 01:18 AM
  3. Replies: 4
    Last Post: 08-11-2010, 12:47 PM
  4. Adding components to a panel
    By jboy in forum New To Java
    Replies: 1
    Last Post: 10-10-2009, 01:02 PM
  5. How to add more components to main panel
    By aneesahamedaa in forum AWT / Swing
    Replies: 4
    Last Post: 08-21-2008, 06:31 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •