Results 1 to 5 of 5
  1. #1
    joatmon is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Basic JFrame Usage

    Hi, I'm totally new to Java, but have a fair amount of programming experience (C++, C, Scala, VBA). I'm trying to replicate something that I did recently in Excel VBA using userforms, which are roughly equivalent to JFrames in Java. I'm having trouble with setting up the JFrame events. I need some really basic help with where I place code to make this thing work.

    I have created a JFrame using NetBeans and can get the JFrame to load with the startup of the application. The next thing that I want to do is to simply make the "close" button work. I think that I have the code for this, but I'm not sure where to place this. Does it go in the class for the JFrame?

    Here is the relvant code thus far showing both the main method and the code behind the JFrame. As you can see, I have a WindowListener in the main method, but I doubt if this is where it belongs. I've omitted the NetBeans-generated code that sets up much of the JFrame to keep this somewhat concise.

    So, my question boils down to "How to I modify this to add a click event to btnClose to make the application exit". Thanks for any help.

    Java Code:
    package kidmath;
    
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    public class KidMath {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            JFrameParam settingsFrame = new JFrameParam();
            settingsFrame.setVisible(true);
            
            settingsFrame.addWindowListener(new WindowAdapter() {
                public void WindowClosing(WindowEvent we) {
                    System.exit(0);
                }
            });
        }
    }
    Java Code:
    package kidmath;
    
    public class JFrameParam extends javax.swing.JFrame {
    
        /**
         * Creates new form JFrameParam
         */
        public JFrameParam() {
            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() {
    
            lblTitle = new javax.swing.JLabel();
            jLabel1 = new javax.swing.JLabel();
            cboChallenge = new javax.swing.JComboBox();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            cboLow = new javax.swing.JComboBox();
            cboOperator = new javax.swing.JComboBox();
            jLabel5 = new javax.swing.JLabel();
            cboHigh1 = new javax.swing.JComboBox();
            btnStart = new javax.swing.JButton();
            btnClose = new javax.swing.JButton();
            btnPrint = new javax.swing.JButton();
    
            \* lots of code omitted here for brevity */
    
            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(JFrameParam.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(JFrameParam.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(JFrameParam.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(JFrameParam.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 JFrameParam().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton btnClose;
        private javax.swing.JButton btnPrint;
        private javax.swing.JButton btnStart;
        private javax.swing.JComboBox cboChallenge;
        private javax.swing.JComboBox cboHigh1;
        private javax.swing.JComboBox cboLow;
        private javax.swing.JComboBox cboOperator;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JLabel lblTitle;
        // End of variables declaration                   
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Basic JFrame Usage

    Are you just looking for an ActionListener on a JButton?

    You could just set the default close operation of the JFrame instead of setting up a listener.

    Word of advice: ditch the gui editor and stick with coding things by hand, at least until you have a better understanding of what's going on under the hood. You'll save yourself some headaches.

    I'm moving this thread to the Swing forum for closer topic alignment.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    joatmon is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: Basic JFrame Usage

    From my own research, I concluded that I needed an ActionListener on a JButton. All I am trying to program at the moment is that when the user clicks the JButton called btnClose, the JFrame closes and the application exits. Couldn't be more simple (conceptuallly, that is). But, if there is a better way than using the ActionListener, please advise. Like I mentioned, I'm totally new to Java.

    Thanks!

  4. #4
    joatmon is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: Basic JFrame Usage

    From my own research, I concluded that I needed an ActionListener on a JButton. All I am trying to program at the moment is that when the user clicks the JButton called btnClose, the JFrame closes and the application exits. Couldn't be more simple (conceptuallly, that is). But, if there is a better way than using the ActionListener, please advise. Like I mentioned, I'm totally new to Java.

    Thanks!

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Basic JFrame Usage

    ActionListener is the way to go. I suggest trying it out and posting an SSCCE if you get stuck.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. CPU usage without JNI
    By Eranga in forum Advanced Java
    Replies: 2
    Last Post: 12-13-2011, 02:25 AM
  2. Replies: 3
    Last Post: 11-27-2011, 05:18 AM
  3. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  4. Replies: 4
    Last Post: 04-22-2009, 11:02 PM
  5. Comm API Usage
    By hobbyist in forum New To Java
    Replies: 0
    Last Post: 11-16-2007, 04:59 PM

Posting Permissions

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