This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2017/11/8

JFrame not showing

divinity divinity

2017/11/8

#
hi can someone tell me why my jframe is not showing. i have it as set visible but it is still not showing. havent started doing any coding as yet except for the exit button
package AttendanceRecordSystem;

import javax.swing.*;

public class StudentRegistrationForm extends javax.swing.JFrame {

   
    public StudentRegistrationForm() {
        
        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() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        stdIDlbl = new javax.swing.JLabel();
        jtxtstdID = new javax.swing.JTextField();
        lblfName = new javax.swing.JLabel();
        jtxtfname = new javax.swing.JTextField();
        lblLname = new javax.swing.JLabel();
        jtxtlnname = new javax.swing.JTextField();
        lblstdage = new javax.swing.JLabel();
        jtxtage = new javax.swing.JTextField();
        jbtncancel = new javax.swing.JButton();
        jbtnsave = new javax.swing.JButton();
        jbtnexit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        jPanel1.setBackground(new java.awt.Color(0, 153, 153));
        jPanel1.setLayout(null);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/AttendanceRecordSystem/costaatt.jpg"))); // NOI18N
        jPanel1.add(jLabel2);
        jLabel2.setBounds(10, 10, 170, 170);

        stdIDlbl.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        stdIDlbl.setText("Student ID");
        jPanel1.add(stdIDlbl);
        stdIDlbl.setBounds(210, 30, 100, 15);

        jtxtstdID.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        jPanel1.add(jtxtstdID);
        jtxtstdID.setBounds(360, 30, 140, 21);

        lblfName.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        lblfName.setText("First Name");
        jPanel1.add(lblfName);
        lblfName.setBounds(210, 70, 70, 15);

        jtxtfname.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        jPanel1.add(jtxtfname);
        jtxtfname.setBounds(360, 70, 140, 21);

        lblLname.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        lblLname.setText("Last Name");
        jPanel1.add(lblLname);
        lblLname.setBounds(210, 110, 80, 15);

        jtxtlnname.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        jPanel1.add(jtxtlnname);
        jtxtlnname.setBounds(360, 110, 140, 21);

        lblstdage.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        lblstdage.setText("Age");
        jPanel1.add(lblstdage);
        lblstdage.setBounds(210, 160, 80, 15);

        jtxtage.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        jPanel1.add(jtxtage);
        jtxtage.setBounds(360, 160, 140, 21);

        jbtncancel.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        jbtncancel.setText("Cancel");
        jPanel1.add(jbtncancel);
        jbtncancel.setBounds(110, 403, 120, 40);

        jbtnsave.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        jbtnsave.setText("Save");
        jPanel1.add(jbtnsave);
        jbtnsave.setBounds(250, 403, 110, 40);

        jbtnexit.setFont(new java.awt.Font("Cambria", 3, 12)); // NOI18N
        jbtnexit.setText("Exit");
        jbtnexit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnexitActionPerformed(evt);
            }
        });
        jPanel1.add(jbtnexit);
        jbtnexit.setBounds(390, 400, 110, 40);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 0, 540, 460);

        pack();
    }// </editor-fold>                        
private JFrame frame;
    private void jbtnexitActionPerformed(java.awt.event.ActionEvent evt) {                                         
        frame = new JFrame("Exit");
        if(JOptionPane.showConfirmDialog(frame, "Confirm if you want to exit", "Clear TextField System",
                JOptionPane.YES_NO_OPTION)== JOptionPane.YES_NO_OPTION);
                System.exit(0);
                
        
    }                                        

    /**
     * @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(StudentRegistrationForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StudentRegistrationForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StudentRegistrationForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StudentRegistrationForm.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() {
                new StudentRegistrationForm().setVisible(true);
            }
        });
    }
danpost danpost

2017/11/8

#
For testing purposes, insert the following at line 152:
System.out.println("Set runnable");
and see if you get terminal output. btw, I was able to produce the JFrame object -- however, I did comment out line 42 and the main method during my test. That is the reason for my suggestion above.
divinity divinity

2017/11/8

#
hi danpost i added it and this is the result
Set runnable
and the jframe is still not showing from line 46 to line 144, those are the hidden codes
danpost danpost

2017/11/8

#
divinity wrote...
i added it and this is the result
Set runnable
Okay, you can remove the test output line. This leads me to believe that whatever is supposed to invoke the runnable is at fault.
nccb nccb

2017/11/8

#
That code looks like it should work, although in Greenfoot we don't provide any guarantees that GUI code like this will work, because it is outside the intended use of Greenfoot. If you don't have any other Greenfoot-specific code, you may be interested in switching to BlueJ: bluej.org Exactly the same editor, debugger and so on as Greenfoot, but doesn't have the Greenfoot GUI which it seems may just be getting in the way, and Swing code is definitely supported there.
divinity divinity

2017/11/12

#
hi danpost I do over the whole thing and still getting the same problem with the new one, this one is the login jframe, it is not showing.
package ElectronicRole;


public class Login extends javax.swing.JFrame {

    
    public Login() {
        initComponents();
    }

  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

    
            

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Login().setVisible(true);
            }
        });
    }
danpost danpost

2017/11/12

#
I really do not believe that you should make your JFrame runnable. Runnable allows your object to run its own code; but, it really does not have any of its own to run. It is constructed; it acts upon actions performed on it (clicking a button, entering data, etc) and provides some desired result. The actions taken by the user causes some specific code to execute; but there is no behind-the-scene stuff that would have your JFrame do something without some action by the user. Specific to the code above, 'initComponents' is called when a Login object is created. It does not make any sense to create a runnable that creates a Login object in that method. I would begin with the following template when building a JFrame type class:
import javax.swing.*;

public class Frame extends JFrame()
{
    static Frame frame;

    public static JFrame getFrame()
    {
        if (frame == null) frame = new Frame();
        return frame;
    }

    public Frame()
    {
        // build JFrame here
    }

    // other methods here (actionPerformed methods, listener methods, etc.)
}
The code elsewhere in your package that "invokes" the object can use 'Frame.getFrame().setVisible(true)' to launch it.
You need to login to post a reply.