Results 1 to 6 of 6

Thread: Form does not appear even after this.setVisible(true) [JAVA]

  1. #1
    Join Date
    May 2011
    Beans
    143
    Distro
    Ubuntu 12.04 Precise Pangolin

    Form does not appear even after this.setVisible(true) [JAVA]

    Hello, I recently started GUI programming in Java with Swing. I am using NetBeans IDE and have attached my complete project with this thread.

    I have two classes.
    Login -> Is the GUI part with all labels, buttons ... (Without main method)
    Main -> Having main method.

    Code:
    /*   Main.java   */
    package main;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {
            Login win = new Login();
            win.setVisible(true);
        }
    }
    Code:
    package main;
    
    import javax.swing.UIManager;
    
    public class Login extends javax.swing.JPanel {
    
        public Login() throws Exception
        {
            UIManager.setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
            initComponents();
        }   
        
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            cUser = new javax.swing.JTextField();
            cPass = new javax.swing.JTextField();
            cLogin = new javax.swing.JButton();
            cOut = new javax.swing.JLabel();
    
            jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 16)); // NOI18N
            jLabel1.setText("Login Window");
    
            jLabel2.setText("Username");
    
            jLabel3.setText("Password");
    
            cUser.setName("");
    
            cLogin.setLabel("Login");
            cLogin.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    cLoginMouseClicked(evt);
                }
            });
    
            cOut.setForeground(new java.awt.Color(255, 0, 0));
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(27, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGap(83, 83, 83)
                            .addComponent(cLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(81, 81, 81))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGap(59, 59, 59)
                            .addComponent(jLabel1)
                            .addGap(66, 66, 66))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2)
                                .addComponent(jLabel3))
                            .addGap(52, 52, 52)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(cOut)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(cUser)
                                    .addComponent(cPass, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGap(20, 20, 20))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jLabel1)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(cUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(cPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(cOut)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(cLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 28, Short.MAX_VALUE))
            );
        }// </editor-fold>                        
    
        private void cLoginMouseClicked(java.awt.event.MouseEvent evt) {                                    
            String password = cPass.getText();
            String username = cUser.getText();
            if("pass".equals(password) && "admin".equals(username))
                cOut.setText("Logged In");
            else
                cOut.setText("Invalid Login");
        }                                   
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton cLogin;
        private javax.swing.JLabel cOut;
        private javax.swing.JTextField cPass;
        private javax.swing.JTextField cUser;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        // End of variables declaration                   
    }
    Attached Files Attached Files

  2. #2
    Join Date
    Jul 2008
    Location
    England
    Beans
    866

    Re: Form does not appear even after this.setVisible(true) [JAVA]

    Your Login class is a JPanel, which is not a window. You want a window to be displayed. So you either need to change your Login class to inherit from JFrame, so that it is a window that can be displayed, or create a JFrame and put the Login JPanel in it.

    From looking at your post it looks like you are using a designer to set up your Login class, so I would suggest creating a JFrame and adding your panel to it.

    Code:
    Login login = new ........
    
    JFrame frame = new JFrame();
    frame.setSize(300, 300); // Set the size of the window
    frame.add(login);
    frame.setVisible(true);
    Paul
    My current project: http://apps.facebook.com/beatthetexan - Creating an artificial poker player using neural networks and genetic algorithms.
    My blog: http://pm-gaming.blogspot.com

  3. #3
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Form does not appear even after this.setVisible(true) [JAVA]

    The problem is that the way you have written Main.main, Swing doesn't start to run. You need to do it like this.

  4. #4
    Join Date
    Nov 2012
    Location
    Halloween Town
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: Form does not appear even after this.setVisible(true) [JAVA]

    Another problem you have with your code is that you setting the look and feel of your form with the SeaGlassLookAndFeel but you are not importing it to your project. You have to add the SeaGlassLookAndFeel jar's to your project libraries.

  5. #5
    Join Date
    May 2011
    Beans
    143
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Form does not appear even after this.setVisible(true) [JAVA]

    how noob i am ... Thanks a LOT !!!!

  6. #6
    Join Date
    May 2011
    Beans
    143
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Form does not appear even after this.setVisible(true) [JAVA]

    Quote Originally Posted by slickymaster View Post
    not importing it to your project
    I added it !!!

Bookmarks

Posting Permissions

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