Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default KeyListener - Is this what I need?

    I have an application in netbeans, has a help key on it, I want it so that if a user presses f1 at any point in the running of the app that the help screen comes up which works if you press the button.

    Is a keylistener what I need for this?

    Thanks in advance

  2. #2
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question I have tried,

    I have tried a couple of sets of code

    Java Code:
    public void keyPressed(KeyEvent e){
            int F1;
    
            if (e.getKeyCode() == e.VK_F1);
                    {
                     optPane1.showMessageDialog(this,
                        " You can use your mouse or tab key to navigate\n"+
                        " our body mass calculator.\n\n"+
                        " New button, this will overwrite all previous\n" +
                        " files\n\n"+
                        " Add Record, enables you to add a new member\n" +
                        " to the records only after creating new file.\n\n"+
                        " Search button, enter your membership number \n" +
                        " click search to find member details.\n\n"+
                        " Update Button, Search for the appropriate record\n"+
                        " by membership number, amend the details\n"+
                        " then click the update button\n\n"+
                        " Select Imperial or Metric for the measurements,\n" +
                        " enter the height and weight then click convert\n\n"+
                        " The default measurements are metric, if you \n"+
                        " select imperial using the radio button you \n"+
                        " will be advised."+
                        " \n","User Guide",optPane1.INFORMATION_MESSAGE);}
    
        }
    From the code at the top I have tried the getkey and keyListener both but neither seems to work, could someone just advise as to which method i should be following to produce the effect that I am wanting

    which is on press of f1 the help screen opens up

    Thanks in advance

  3. #3
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    You could use a keylistener to trigger the display of your help menu, but key binding might be better... Reason being is that key bindings override the current action of the key, while a keylistener lets you know when the key is pressed, released, etc... The current action of the key is still preformed...

    Check out:
    How to Use Key Bindings (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Other Swing Features)

    Let me know if you get stuck...
    Who Cares... As Long As It Works...

  4. #4
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    Thanks for the info, I am just taking a look now....I am sure at some point I will be back at least to clarify something..

  5. #5
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question

    Ok, so far I have

    Java Code:
    if component.getInputMap().put(KeyStroke("F1"));
    The code may not be right at the moment but the question I have is, can this be put in the code for the button or does it have to go in public?

    Java Code:
    private void cmdHelpActionPerformed(java.awt.event.ActionEvent evt) {                                        
                     // Instructions on how to use this application
            
            if component.getInputMap().put(KeyStroke("F1"));
                  //if(evt.getSource()==cmdHelp);
                    { 
                     optPane1.showMessageDialog(this,                        
                        " You can use your mouse or tab key to navigate\n"+
                        " our body mass calculator.\n\n"+
                        " New button, this will overwrite all previous\n" +
                        " files\n\n"+
                        " Add Record, enables you to add a new member\n" +
                        " to the records only after creating new file.\n\n"+
                        " Search button, enter your membership number \n" +
                        " click search to find member details.\n\n"+
                        " Update Button, Search for the appropriate record\n"+
                        " by membership number, amend the details\n"+
                        " then click the update button\n\n"+                    
                        " Select Imperial or Metric for the measurements,\n" +
                        " enter the height and weight then click convert\n\n"+
                        " The default measurements are metric, if you \n"+
                        " select imperial using the radio button you \n"+
                        " will be advised."+
                        " \n","User Guide",optPane1.INFORMATION_MESSAGE);}
        }
    I dont need the answer to the final solution yet, I am hopeing that I can figure it out but could you answer me as to whether it has to be public or not?

    Thanks in advance

  6. #6
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    You can make it private...
    Who Cares... As Long As It Works...

  7. #7
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    But can I have it working within the Help button code or do I need to seperate it?

    I have had a look at several examples of scripts with the event, I am trying to validate it at the moment in the help button but I have the feeling that this will never work, is this the case?

  8. #8
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    You can have your program do just about anything you want... You just need to have enough patience...

    For your button... All you would need to do is:

    Java Code:
           JButton b = new JButton();
    
            b.addActionListener(new ActionListener(){
    
                public void actionPerformed(ActionEvent e) {
                    
                    
                    //Display HelpMenu...
                    
                }
                
                
                
                
            });


    Then you want to use key bindings to override the action of the F1 key...
    Who Cares... As Long As It Works...

  9. #9
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    Well I am surrounded by books, googled myself to death but I have not been able to make working sense of this, I know the answer when I eventually get there will probably only be 4 or 5 lines of code but currently I have been jargoned to death.

    I need a little more help!!

    Thanks in advance

  10. #10
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    No Prob...

    Java Code:
           this.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("F1"), f1Action.getValue(Action.NAME));
    
            this.getActionMap().put(f1Action.getValue(Action.NAME), f1Action);
    
    
    
    
         public Action f1Action = new AbstractAction("DisplayHelpMenu") {
    
            public void actionPerformed(ActionEvent e) {
    
                DisplayHelpMenu();
            }
    
    
        };

    That should do the trick...
    Who Cares... As Long As It Works...

  11. #11
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Talking

    well I hate to admit it but no matter where I put this I get errors, I know it is going to be me but I have tried placing the code in the public class at the start of my code which gives me an error on the line below

    Java Code:
    this.getActionMap().put(f1Action.getValue(Action.NAME), f1Action);
    the error I am getting is 'illegal start of expression'

    Thanks for the help so far but can I ask for more..... :D

  12. #12
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    You dont have to ask...

    Replace this with the component that you want the f1Action to work... For instance if I have a JFrame with a JTable in it... I want to do this:


    Java Code:
           JTable jt = new JTable();
    
           jt.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("F1"), f1Action.getValue(Action.NAME));
    
            jt.getActionMap().put(f1Action.getValue(Action.NAME), f1Action);

    Now if the JTable (jt) has focus then when I press F1 the help menu will pop up...

    Post your code and lets take a look...
    Who Cares... As Long As It Works...

  13. #13
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    Well I will post the code but its a very large amount, is there a particular part you want or do u want it all?

    This application is nearly complete, in total there are 1537 lines of code bearing in mind a lot of it is netbeans.

  14. #14
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    I see... Well it cant hurt... Post it all and if we need to reduce it to a particular part we can do it at that time...
    Last edited by markw8500; 04-18-2009 at 03:53 AM.
    Who Cares... As Long As It Works...

  15. #15
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    It wont let me post all the code, tried to attach a .txt, was told file exceeds limit so have to cut it into sections

    part 1

    Java Code:
    import java.io.*; // Input output libraries, used for file
    import java.text.*; // number format
    import java.awt.*; // whole library for creating interfaces, used for images and graphics
    //import java.awt.event.KeyEvent; // used for radio buttons, enables metric as default after reset form
    import java.awt.event.*; // for exit confirmation and keyevent
    
    /*
     *
     * <applet code="Assessment3Bmi" WIDTH=683 HEIGHT=482 >
     * </applet>
     */
    
    public class frmBmi extends javax.swing.JFrame {
    
        ObjectOutputStream output;
        ObjectInputStream input;
    
        File file = new File(new File(getClass().getResource("mbrshipFiles").getFile()),"mship.txt"); 
        /** Creates new form frmBmi */
        public frmBmi() {
            initComponents();
    
            setTitle("Fatbusters BMI Members Readings"); // puts title on main window
    
            Toolkit toolkit = getToolkit();
            Dimension size = toolkit.getScreenSize();
            setLocation(size.width/2 - getWidth()/2,
    		size.height/2 - getHeight()/2); // centers window when opening
    
    
        }
    
        /** 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() {
    
            optPane1 = new javax.swing.JOptionPane();
            buttonGroup1 = new javax.swing.ButtonGroup();
            jPanel1 = new javax.swing.JPanel();
            txtMembership = new javax.swing.JTextField();
            txtJdate = new javax.swing.JTextField();
            txtAge = new javax.swing.JTextField();
            txtGender = new javax.swing.JTextField();
            cmdNew = new javax.swing.JButton();
            cmdAdd = new javax.swing.JButton();
            cmdSearch = new javax.swing.JButton();
            cmdFirst = new javax.swing.JButton();
            cmdHelp = new javax.swing.JButton();
            cmdExit = new javax.swing.JButton();
            cmdUpdate = new javax.swing.JButton();
            cmdReset = new javax.swing.JButton();
            JheightWeightPanel = new javax.swing.JPanel();
            txtHeight = new javax.swing.JTextField();
            txtConvHeight = new javax.swing.JTextField();
            txtWeight = new javax.swing.JTextField();
            txtConvWeight = new javax.swing.JTextField();
            cmdConvert = new javax.swing.JButton();
            jLabel5 = new javax.swing.JLabel();
            jLabel7 = new javax.swing.JLabel();
            jRmetric = new javax.swing.JRadioButton();
            jRimp = new javax.swing.JRadioButton();
            jBmiPanel = new javax.swing.JPanel();
            txtBmi = new javax.swing.JTextField();
            txtResults = new javax.swing.JTextField();
            jLabel8 = new javax.swing.JLabel();
            jLabel9 = new javax.swing.JLabel();
            cmdCal = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            Limage = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Fatbusters");
            setBackground(new java.awt.Color(204, 255, 255));
            setMaximizedBounds(new java.awt.Rectangle(0, 0, 0, 0));
    
            jPanel1.setBackground(new java.awt.Color(204, 204, 255));
            jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
    
            txtMembership.setToolTipText("Enter your membership number in this field");
            txtMembership.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    txtMembershipMouseClicked(evt);
                }
            });
            txtMembership.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    txtMembershipActionPerformed(evt);
                }
            });
            txtMembership.addFocusListener(new java.awt.event.FocusAdapter() {
                public void focusGained(java.awt.event.FocusEvent evt) {
                    txtMembershipFocusGained(evt);
                }
            });
    
            txtJdate.setToolTipText("Enter the date you joined in a format of dd/mm/yy");
            txtJdate.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    txtJdateMouseClicked(evt);
                }
            });
            txtJdate.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    txtJdateActionPerformed(evt);
                }
            });
            txtJdate.addFocusListener(new java.awt.event.FocusAdapter() {
                public void focusGained(java.awt.event.FocusEvent evt) {
                    txtJdateFocusGained(evt);
                }
            });
    
            txtAge.setToolTipText("Enter your age in years");
            txtAge.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    txtAgeMouseClicked(evt);
                }
            });
            txtAge.addFocusListener(new java.awt.event.FocusAdapter() {
                public void focusGained(java.awt.event.FocusEvent evt) {
                    txtAgeFocusGained(evt);
                }
            });
    
            txtGender.setToolTipText("Enter your gender Male or Female");
            txtGender.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    txtGenderMouseClicked(evt);
                }
            });
            txtGender.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    txtGenderActionPerformed(evt);
                }
            });
            txtGender.addFocusListener(new java.awt.event.FocusAdapter() {
                public void focusGained(java.awt.event.FocusEvent evt) {
                    txtGenderFocusGained(evt);
                }
            });
    
            cmdNew.setText("New File");
            cmdNew.setToolTipText("Creates new record overwriting old text file");
            cmdNew.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    cmdNewActionPerformed(evt);
                }
            });
    
            cmdAdd.setText("Add Record");
            cmdAdd.setToolTipText("Add's a record to the file");
            cmdAdd.setEnabled(false);
            cmdAdd.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    cmdAddActionPerformed(evt);
                }
            });
    
            cmdSearch.setText("Search Records");
            cmdSearch.setToolTipText("Enter a membership number in the field and then click search");
            cmdSearch.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    cmdSearchActionPerformed(evt);
                }
            });
    
            cmdFirst.setText("First Record");
            cmdFirst.setToolTipText("Click here to see the first record on file");
            cmdFirst.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    cmdFirstActionPerformed(evt);
                }
            });
    
            cmdHelp.setFont(new java.awt.Font("Tahoma", 1, 18));
            cmdHelp.setText("Help!");
            cmdHelp.setToolTipText("Click here to get help");
            cmdHelp.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    cmdHelpActionPerformed(evt);
                }
            });
            cmdHelp.addFocusListener(new java.awt.event.FocusAdapter() {
                public void focusGained(java.awt.event.FocusEvent evt) {
                    cmdHelpFocusGained(evt);
                }
            });
            cmdHelp.addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyPressed(java.awt.event.KeyEvent evt) {
                    cmdHelpKeyPressed(evt);
                }
            });
    
            cmdExit.setText("Close Application");
            cmdExit.setToolTipText("Closes the application");
            cmdExit.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    cmdExitActionPerformed(evt);
                }
            });
    
            cmdUpdate.setText("Update Records");
            cmdUpdate.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    cmdUpdateActionPerformed(evt);
                }
            });
    
            cmdReset.setText("Reset Form");
            cmdReset.setToolTipText("Clears all fields on the form");
            cmdReset.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    cmdResetActionPerformed(evt);
                }
            });
    
            JheightWeightPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
    
            txtHeight.setToolTipText("Enter your height in Cm's or Inches");
            txtHeight.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    txtHeightMouseClicked(evt);
                }
            });
            txtHeight.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    txtHeightActionPerformed(evt);
                }
            });
            txtHeight.addFocusListener(new java.awt.event.FocusAdapter() {
                public void focusGained(java.awt.event.FocusEvent evt) {
                    txtHeightFocusGained(evt);
                }
            });
    
            txtConvHeight.setToolTipText("Height Conversion");
            txtConvHeight.setEnabled(false);
            txtConvHeight.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    txtConvHeightActionPerformed(evt);
                }
            });
    
            txtWeight.setToolTipText("Enter your weight");
            txtWeight.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    txtWeightMouseClicked(evt);
                }
            });
            txtWeight.addFocusListener(new java.awt.event.FocusAdapter() {
                public void focusGained(java.awt.event.FocusEvent evt) {
                    txtWeightFocusGained(evt);
                }
            });
    
            txtConvWeight.setToolTipText("Weight Conversion");
            txtConvWeight.setEnabled(false);
    
            cmdConvert.setText("Convert");
            cmdConvert.setToolTipText("Convert the entries in top fields");
            cmdConvert.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    cmdConvertActionPerformed(evt);
                }
            });
    
            jLabel5.setText("Height");
    
            jLabel7.setText("Weight");
    
            buttonGroup1.add(jRmetric);
            jRmetric.setSelected(true);
            jRmetric.setText("Metric");
            jRmetric.setToolTipText("Enter Metric Measurements into the Top Textfield");
            jRmetric.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRmetricActionPerformed(evt);
                }
            });
    
            buttonGroup1.add(jRimp);
            jRimp.setText("Imperial");
            jRimp.setToolTipText("Enter Imperial Measurements into the Top Textfield");
            jRimp.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRimpActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout JheightWeightPanelLayout = new javax.swing.GroupLayout(JheightWeightPanel);
            JheightWeightPanel.setLayout(JheightWeightPanelLayout);
            JheightWeightPanelLayout.setHorizontalGroup(
                JheightWeightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(JheightWeightPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(JheightWeightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(JheightWeightPanelLayout.createSequentialGroup()
                            .addGroup(JheightWeightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(JheightWeightPanelLayout.createSequentialGroup()
                                    .addComponent(jRmetric)
                                    .addGap(28, 28, 28)
                                    .addComponent(jLabel5))
                                .addComponent(jRimp))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(JheightWeightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtHeight, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtConvHeight, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE))
                            .addGap(43, 43, 43)
                            .addComponent(jLabel7)
                            .addGap(6, 6, 6)
                            .addGroup(JheightWeightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtWeight, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                                .addComponent(txtConvWeight, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)))
                        .addComponent(cmdConvert))
                    .addContainerGap())
            );
    
            JheightWeightPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txtConvHeight, txtConvWeight, txtHeight, txtWeight});
    
            JheightWeightPanelLayout.setVerticalGroup(
                JheightWeightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(JheightWeightPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(JheightWeightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(txtHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtWeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel7)
                        .addComponent(jRmetric))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(JheightWeightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jRimp)
                        .addComponent(txtConvHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtConvWeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
                    .addComponent(cmdConvert)
                    .addContainerGap())
            );
    
            jBmiPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
    
            txtBmi.setToolTipText("Your Bmi Reading");
    
            txtResults.setFont(new java.awt.Font("Tahoma", 0, 12));
            txtResults.setForeground(new java.awt.Color(51, 51, 51));
            txtResults.setToolTipText("Advises whether you are 'ok', 'underweight' or 'overweight'");
            txtResults.setCaretColor(new java.awt.Color(255, 255, 255));
            txtResults.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    txtResultsActionPerformed(evt);
                }
            });
    
            jLabel8.setText("Bmi");
    
            jLabel9.setText("Results");
    
            cmdCal.setText("Calculate Bmi");
            cmdCal.setToolTipText("Calculates the Body Mass Index");
            cmdCal.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    cmdCalActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout jBmiPanelLayout = new javax.swing.GroupLayout(jBmiPanel);
            jBmiPanel.setLayout(jBmiPanelLayout);
            jBmiPanelLayout.setHorizontalGroup(
                jBmiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jBmiPanelLayout.createSequentialGroup()
                    .addGap(44, 44, 44)
                    .addComponent(cmdCal)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                    .addComponent(jLabel8)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(txtBmi, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jLabel9)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(txtResults, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
            jBmiPanelLayout.setVerticalGroup(
                jBmiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jBmiPanelLayout.createSequentialGroup()
                    .addContainerGap(21, Short.MAX_VALUE)
                    .addGroup(jBmiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtResults, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel9)
                        .addComponent(txtBmi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel8)
                        .addComponent(cmdCal))
                    .addGap(19, 19, 19))
            );
    
            jLabel1.setText("Membership Number");
    
            jLabel2.setText("Joining Date");
    
            jLabel3.setText("Age");
    
            jLabel4.setText("Gender");
    
            Limage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/assessment3bmi/bmi.gif"))); // NOI18N
            Limage.setLabelFor(Limage);
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jBmiPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(JheightWeightPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(cmdSearch)
                                    .addGap(18, 18, 18)
                                    .addComponent(cmdUpdate)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(cmdNew)
                                        .addComponent(cmdFirst)
                                        .addComponent(cmdAdd))
                                    .addComponent(cmdExit, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addComponent(cmdReset)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(Limage)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cmdHelp))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtMembership, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtJdate, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(15, 15, 15)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtAge, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtGender, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addContainerGap())
            );
    
            jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cmdAdd, cmdExit, cmdFirst, cmdNew, cmdReset, cmdSearch, cmdUpdate});
    
            jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {JheightWeightPanel, jBmiPanel});
    
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(Limage, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cmdHelp))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txtMembership, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2)
                        .addComponent(txtJdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3)
                        .addComponent(txtAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4)
                        .addComponent(txtGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addComponent(JheightWeightPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(cmdNew)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(cmdAdd)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(cmdFirst)))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jBmiPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(cmdSearch)
                                .addComponent(cmdUpdate)))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(cmdReset)
                            .addGap(61, 61, 61)
                            .addComponent(cmdExit)))
                    .addGap(19, 19, 19))
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
            int maxarray=250; // sets maximum records
            String stMembership[]=new String[maxarray];
            String stJoinDate[]=new String[maxarray];
            double stHeight[]=new double[maxarray];
            double stWeight[]=new double[maxarray];
            double stBmi[]=new double[maxarray];
            int recnum = 0;
            int NumRecords = 0;  // variable set at 0 for total amount of records (Add Button)

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I recommend that you just extract the code of interest and place it in a separate compilable program without netbeans code. That way we'll have less code to go through and your chances of getting an answer quick will be increased. Have a look at this to see what I'm talking about: SSCCE

  17. #17
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Default

    part 2

    Java Code:
     private void txtMembershipActionPerformed(java.awt.event.ActionEvent evt) {                                              
            // TODO add your handling code here:
    }                                             
    
        private void txtJdateActionPerformed(java.awt.event.ActionEvent evt) {                                         
                      
    }                                        
    
        private void cmdAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // Add button - works tested in stages             
            boolean ok = true;        
            MshipRecord record;
            if (ok == true){ 
                System.out.println(NumRecords + "Member added");
            try {
                record = new MshipRecord (
                        txtMembership.getText(), 
                        txtJdate.getText(),
                        Double.parseDouble(txtHeight.getText()),
                        Double.parseDouble(txtWeight.getText()),
                        Double.parseDouble(txtBmi.getText()));// double so as to use decimal places more acurate than a float
                        output.writeObject(record); // writes the record
                       }
                    catch (IOException e) {
                        optPane1.showMessageDialog(this, "Error adding a record","File Write Error",optPane1.ERROR_MESSAGE);
                        }
                NumRecords = NumRecords +1; // adds one to the Numrecords counter
            if (NumRecords == 11) // number of records file will store b4 warning
                {cmdAdd.setEnabled(false);
                        optPane1.showMessageDialog(this, "We have the maximum amount of members","No more members",optPane1.INFORMATION_MESSAGE); }}
                        //System.out.println("Maximum Capacity");}  }
                        txtMembership.setText("");
                        txtAge.setText("");
                        txtJdate.setText("dd/mm/yy");
                        txtHeight.setText("");
                        txtWeight.setText("");
                        txtGender.setText("");
                        txtHeight.setText("");
                        txtConvHeight.setText("") ;
                        txtConvWeight.setText("");
                        txtBmi.setText("");
                        txtResults.setText("");
                        txtResults.setBackground(Color.WHITE);       
        }                                      
    
        private void cmdNewActionPerformed(java.awt.event.ActionEvent evt) {                                       
                    txtJdate.setText("dd/mm/yy"); // GOES IN DATE FIELD ON PRESS OF NEW FILE BUTTON
                NumRecords = 0;
                System.out.println("Counter reset");           
            try{
                    output = new ObjectOutputStream(new FileOutputStream(file)); }
                    catch (IOException e) {
                    optPane1.showMessageDialog(this, "error with the input file", "File Error", optPane1.ERROR_MESSAGE);
                    }            
                     /* Disables Add before the file has been created */
                    cmdAdd.setEnabled(true);                
        }                                      
    
        private void cmdHelpActionPerformed(java.awt.event.ActionEvent evt) {                                        
                     // Instructions on how to use this application
                  if(evt.getSource()==cmdHelp);
                    {
                     optPane1.showMessageDialog(this,                        
                        " You can use your mouse or tab key to navigate\n"+
                        " our body mass calculator.\n\n"+
                        " New button, this will overwrite all previous\n" +
                        " files\n\n"+
                        " Add Record, enables you to add a new member\n" +
                        " to the records only after creating new file.\n\n"+
                        " Search button, enter your membership number \n" +
                        " click search to find member details.\n\n"+
                        " Update Button, Search for the appropriate record\n"+
                        " by membership number, amend the details\n"+
                        " then click the update button\n\n"+                    
                        " Select Imperial or Metric for the measurements,\n" +
                        " enter the height and weight then click convert\n\n"+
                        " The default measurements are metric, if you \n"+
                        " select imperial using the radio button you \n"+
                        " will be advised."+
                        " \n","User Guide",optPane1.INFORMATION_MESSAGE);}
        }

  18. #18
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    6

    Question

    part 3

    Let me know if you want the rest?

    Java Code:
    private void cmdCalActionPerformed(java.awt.event.ActionEvent evt) {                                       
            String aMembership;
            String aJdate;
            double age = 0;
            String aGender = null;
            String OK;
            boolean ok;
            ok = true;
            double height = 0; // Height variable
            double weight = 0; // Weight variable
            double outputBmi = 0;
            String outbmi;
            double cm =0;
              // Membership Number try catch
                try {
                          aMembership = txtMembership.getText();  //read aMembership from the screen as a string
                    if  ((aMembership.isEmpty()))
                        // if entry is made then set dummy value to OK
                        throw new IllegalArgumentException("Please enter your membership number");
                        //  otherwise trap the error
                    }
                        catch (IllegalArgumentException e)   {   // catch the error
                        optPane1.showMessageDialog(this,e.getMessage(),"You did not enter your Membership Number",optPane1.ERROR_MESSAGE);
                    } // END OF MEMBERSHIP TRY CATCH
                // Join Date try catch       **START**
                try {
                        aJdate = txtJdate.getText();  //read aJdate from the screen as a string
                    if  ((aJdate.isEmpty()))
                    // if no entry is made then throw error
                        throw new IllegalArgumentException("Please enter the date you joined fatbusters");
                    }
                        catch (IllegalArgumentException e)   {   // catch the error
                        optPane1.showMessageDialog(this,e.getMessage(),"You did not enter the date you joined",optPane1.ERROR_MESSAGE);
                    }
                    // Join Date try catch      **END**/
            // if Age blank field                          START
                String Ages = txtAge.getText();
                            if  ((Ages.isEmpty()))
                                optPane1.showMessageDialog(this,"Please enter your age ",
                                     "You did not enter your age",optPane1.ERROR_MESSAGE);
           // if Age blank field                            END       
                try {
                        age = Double.parseDouble(txtAge.getText());
                        //read from screen as integer
                   if (age > 70) throw new IllegalArgumentException("Maximum Age is 70 Years Old");// set a trap to catch Age range 17 > 70
                     else if (age < 17)  throw new IllegalArgumentException("Minimum Age is 17 Years Old");
                     // else if (age = 0) throw new IllegalArgumentException("You must enter an age");
    				 }
                      catch (NumberFormatException e)
                      // will catch the error if the input is not a number
                     {
                      optPane1.showMessageDialog(this,"Age Must Be Between 17 + 70","INFORMATION",optPane1.INFORMATION_MESSAGE);
                      ok=false;
                     }
                      catch (IllegalArgumentException e)
                      //will catch the errors outside of the parameters set
                     {
                      optPane1.showMessageDialog(this,e.getMessage(),"For your information",optPane1.INFORMATION_MESSAGE);
                      ok=false;
                      // e.getMessage will get the message for e as set in the trap
                     }
              
                try {
                        aGender = txtGender.getText();  //read aGender from the screen as a string
                    if  ((aGender.isEmpty()))
                    // if no entry is made then throw error
                        throw new IllegalArgumentException("Please enter your gender");
                    }
                        catch (IllegalArgumentException e)   {   // catch the error
                        optPane1.showMessageDialog(this,e.getMessage(),"You did not enter your gender",optPane1.ERROR_MESSAGE);
                        }        
                try {
                        aGender = txtGender.getText();  //read aGender from the screen as a string
                    if  ((aGender.equals("M")) || (aGender.equals("m")) || (aGender.equals("F")) || (aGender.equals("f")))
                    // if string is M, m, F or f then set dummy value to OK
                    OK = "ok";
               else
                    throw new IllegalArgumentException("code must be M or m for Male or F or f for Female");
                    //  otherwise trap the error
                     }
                        catch (IllegalArgumentException e)   {   // catch the error
                        optPane1.showMessageDialog(this,e.getMessage(),"Please enter your gender",optPane1.INFORMATION_MESSAGE);
                     }
                    
            // height try catch METRIC radio button
            if (jRmetric.isSelected()==true){
                         // if Height is blank field                          START
                    String tall= txtHeight.getText();
                            if  ((tall.isEmpty()))
                                optPane1.showMessageDialog(this,"Please enter your height ",
                                     "You did not enter your height",optPane1.ERROR_MESSAGE);
           // if height blank field                                   end
            try {
                        height = Double.parseDouble(txtHeight.getText());
                        //read from screen as integer
                     if (height > 300) throw new IllegalArgumentException("Maximum height of 300cm's");
                     // set a trap to catch numbers > 300
                     if (height < 100)  throw new IllegalArgumentException("Minimum height 100cm's");
                     // set a trap to catch numbers < 100
                    }
                        catch (NumberFormatException e)
                        // will catch the error if the input is not a number
                    {
                         optPane1.showMessageDialog(this,"Height between 100 + 300cm's","INFORMATION",optPane1.INFORMATION_MESSAGE);
                         ok=false;
                     }
                         catch (IllegalArgumentException e)
                         //will catch the errors outside of the parameters set
                     {
                         optPane1.showMessageDialog(this,e.getMessage(),"Error",optPane1.INFORMATION_MESSAGE);
                         ok=false;
                         // e.getMessage will get the message for e as set in the trap
                     }
            // end of height try catch        
            // if weight is blank                          START
            String heavy= txtWeight.getText();
                            if  ((heavy.isEmpty()))
                                optPane1.showMessageDialog(this,"Please enter your weight ",
                                     "You did not enter your weight",optPane1.ERROR_MESSAGE);
           // if weight blank                             end
            try {
                            weight = Double.parseDouble(txtWeight.getText());
                            //read from screen as integer
                                if (weight > 160) throw new IllegalArgumentException("Maximum Weight of 160Kg's");
                                // set a trap to catch numbers > 12
                                if (weight < 30)  throw new IllegalArgumentException("Minimum Weight 30Kg's");
                                // set a trap to catch numbers < 1
                        }
                        catch (NumberFormatException e)
                        // will catch the error if the input is not a number
                         {
                         optPane1.showMessageDialog(this,"Weight between 30 & 160Kg's","INFORMATION",optPane1.INFORMATION_MESSAGE);
                         ok=false;
                         }
                         catch (IllegalArgumentException e)
                         //will catch the errors as set up in the trap
                         {
                         optPane1.showMessageDialog(this,e.getMessage(),"INFORMATION",optPane1.INFORMATION_MESSAGE);
                         ok=false;}
                         // e.getMessage will get the message for e as set in the trap
                        // End of if statement for Weight METRIC
                         height=Double.parseDouble(txtHeight.getText());
                         weight=Double.parseDouble(txtWeight.getText());
                         outputBmi = clBmiMetric.bmi(height,weight);
                         txtBmi.setText(outbmi=bodymass.format(outputBmi));
            } else if              
      (jRimp.isSelected()==true){ //Imperial bmi - uses imperial bmi class file
                // if Height is blank field                          START
            String tall= txtHeight.getText();
                            if  ((tall.isEmpty()))
                                optPane1.showMessageDialog(this,"Please enter your height ",
                                     "You did not enter your height",optPane1.ERROR_MESSAGE);
           // if height blank field                                   end                                                
                try { // height try catch Imperial
                        height = Double.parseDouble(txtHeight.getText());
                        //read from screen as integer
                     if (height > 118) throw new IllegalArgumentException("Maximum height of 118 Inches's");
                     // set a trap to catch numbers > 118.1 Inches
                     if (height < 39)  throw new IllegalArgumentException("Minimum height 39 Inches's");
                     // set a trap to catch numbers < 100 Inches
                    }
                        catch (NumberFormatException e)
                        // will catch the error if the input is not a number
                    {
                         optPane1.showMessageDialog(this,"Height between 39 + 118 Inches","INFORMATION",optPane1.INFORMATION_MESSAGE);
                         ok=false;
                     }
                         catch (IllegalArgumentException e)
                         //will catch the errors outside of the parameters set
                     {
                         optPane1.showMessageDialog(this,e.getMessage(),"Error",optPane1.INFORMATION_MESSAGE);
                         ok=false;}
                         // e.getMessage will get the message for e as set in the trap
                // TRY CATCH WEIGHT START
                // if weight is blank                          START
            String heavy= txtWeight.getText();
                            if  ((heavy.isEmpty()))
                                optPane1.showMessageDialog(this,"Please enter your weight ",
                                     "You did not enter your weight",optPane1.ERROR_MESSAGE);
           // if weight blank                      end
                      try {
                            weight = Double.parseDouble(txtWeight.getText());
                            //read from screen as integer
                                if (weight > 352) throw new IllegalArgumentException("Maximum Weight of 352lb's");
                                // set a trap to catch numbers > 12
                                if (weight < 66)  throw new IllegalArgumentException("Minimum Weight 66lb's");
                                // set a trap to catch numbers < 66.1
                        }
                        catch (NumberFormatException e)
                        // will catch the error if the input is not a number
                         {
                         optPane1.showMessageDialog(this,"Weight between 66 & 352lb's","INFORMATION",optPane1.INFORMATION_MESSAGE);
                         ok=false;
                         }
                         catch (IllegalArgumentException e)
                         //will catch the errors as set up in the trap
                         {
                         optPane1.showMessageDialog(this,e.getMessage(),"Error",optPane1.INFORMATION_MESSAGE);
                         ok=false;}
                         // e.getMessage will get the message for e as set in the trap
                         // end of height try catch IMPERIAL
                         height=Double.parseDouble(txtHeight.getText());
                         weight=Double.parseDouble(txtWeight.getText());
                         outputBmi = clBmiImperial.bmiImperial(height,weight);
                         txtBmi.setText(outbmi=bodymass.format(outputBmi)); }
            // If Statements for results : OK, OVERWEIGHT & UNDERWEIGHT
            // Female If statements
            if ((outputBmi < 18.5) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 19)) )){
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
            } else 
            if ((outputBmi <= 23) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 19)) )){
                                txtResults.setText("   OK");
                                txtResults.setForeground(Color.GREEN);
                                txtResults.setBackground(Color.BLACK);
            } else 
            if ((outputBmi > 23) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 19)) )){
                                txtResults.setText("   Overweight");
                                txtResults.setForeground(Color.RED);
                                txtResults.setBackground(Color.BLACK);
            } else 
            if ((outputBmi < 19.1) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 29)) )){
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE); // check bmi readings
                                txtResults.setBackground(Color.BLACK);
            } else 
            if ((outputBmi <= 25.7) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 29)) )){
                                txtResults.setText("   OK");
                                txtResults.setForeground(Color.GREEN);
                                txtResults.setBackground(Color.BLACK);
            } else 
            if ((outputBmi > 25.7) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 29)) )){
                                txtResults.setText("   Overweight");
                                txtResults.setForeground(Color.RED);
                                txtResults.setBackground(Color.BLACK);
            } else 
            if ((outputBmi < 24) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 39)) )){
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
            } else 
            if ((outputBmi < 25.9) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 39)) )){
                                txtResults.setText("   OK");
                                txtResults.setForeground(Color.GREEN);
                                txtResults.setBackground(Color.BLACK);
           } else 
           if ((outputBmi > 25.9) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 39)) )){
                                txtResults.setText("   Overweight");
                                txtResults.setForeground(Color.RED);
                                txtResults.setBackground(Color.BLACK);
           } else 
    
           if ((outputBmi < 19.1) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 49)) )){
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
           } else 
           if ((outputBmi <= 27.8) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 49)) )){
                                txtResults.setText("   OK");
                                txtResults.setForeground(Color.GREEN);
                                txtResults.setBackground(Color.BLACK);
           } else 
           if ((outputBmi > 27.8) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 49)) )){
                                txtResults.setText("   Overweight");
                                txtResults.setForeground(Color.RED);
                                txtResults.setBackground(Color.BLACK);
           } else 
           if ((outputBmi < 19.1) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 59)) )){
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
           } else 
           if ((outputBmi <= 27.2) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 59)) )){
                                txtResults.setText("   OK");
                                txtResults.setForeground(Color.GREEN);
                                txtResults.setBackground(Color.BLACK);
           } else 
           if ((outputBmi > 27.2) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 59)) )){
                                txtResults.setText("   Overweight");
                                txtResults.setForeground(Color.RED);
                                txtResults.setBackground(Color.BLACK);
    
           } else 
           if ((outputBmi < 19.1) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 70)) )){
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
           } else 
           if ((outputBmi <= 27.5) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 70)) )){
                                txtResults.setText("   OK");
                                txtResults.setForeground(Color.GREEN);
                                txtResults.setBackground(Color.BLACK);
           } else 
           if ((outputBmi > 27.5) && ((aGender.equals("F" ) || aGender.equals("f")) && ((age <= 70)) )){
                                txtResults.setText("   Overweight");
                                txtResults.setForeground(Color.RED);
                                txtResults.setBackground(Color.BLACK);
           } else 
    // if statements for males
           if ((outputBmi < 18.5) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 19)) )){
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
           } else 
           if ((outputBmi <= 23) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 19)) )){
                                txtResults.setText("   OK");
                                txtResults.setForeground(Color.GREEN);
                                txtResults.setBackground(Color.BLACK);
           } else 
           if ((outputBmi > 23) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 19)) )){
                                txtResults.setText("   Overweight");
                                txtResults.setForeground(Color.RED);
                                txtResults.setBackground(Color.BLACK);
           } else 
           if ((outputBmi < 20.7) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 29)) )){
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE); // check bmi readings
                                txtResults.setBackground(Color.BLACK);
           } else 
           if ((outputBmi <= 24.5) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 29)) )){
                                txtResults.setText("   OK");
                                txtResults.setForeground(Color.GREEN);
                                txtResults.setBackground(Color.BLACK);
           } else 
            if ((outputBmi > 24.5) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 29)) )){
                                txtResults.setText("   Overweight");
                                txtResults.setForeground(Color.RED);
                                txtResults.setBackground(Color.BLACK);
           } else 
            if ((outputBmi < 20.7) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 39)) )){
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
             } else 
             if ((outputBmi < 25.9) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 39)) )){
                                txtResults.setText("   OK");
                                txtResults.setForeground(Color.GREEN);
                                txtResults.setBackground(Color.BLACK);
             } else 
             if ((outputBmi > 25.9) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 39)) )){
                                txtResults.setText("   Overweight");
                                txtResults.setForeground(Color.RED);
                                txtResults.setBackground(Color.BLACK);
             } else 
             if ((outputBmi < 20.7) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 49)) )){
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
             } else 
             if ((outputBmi <= 26.8) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 49)) )){
                                txtResults.setText("   OK");
                                txtResults.setForeground(Color.GREEN);
                                txtResults.setBackground(Color.BLACK);
             } else 
             if ((outputBmi > 26.8) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 49)) )){
                                txtResults.setText("   Overweight");
                                txtResults.setForeground(Color.RED);
                                txtResults.setBackground(Color.BLACK);
    
             } else 
             if ((outputBmi < 20.7) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 59)) )){
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
             } else 
             if ((outputBmi <= 27.2) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 59)) )){
                                txtResults.setText("   OK");
                                txtResults.setForeground(Color.GREEN);
                                txtResults.setBackground(Color.BLACK);
             } else 
             if ((outputBmi > 27.2) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 59)) )){
                                txtResults.setText("   Overweight");
                                txtResults.setForeground(Color.RED);
                                txtResults.setBackground(Color.BLACK);
    
             } else 
             if ((outputBmi < 19.1) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 70)) )){
                                txtResults.setText("   Underweight");
                                txtResults.setForeground(Color.ORANGE);
                                txtResults.setBackground(Color.BLACK);
             } else 
             if ((outputBmi <= 27) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 70)) )){
                                txtResults.setText("   OK");
                                txtResults.setForeground(Color.GREEN);
                                txtResults.setBackground(Color.BLACK);
             } else 
             if ((outputBmi > 27) && ((aGender.equals("m" ) || aGender.equals("M")) && ((age <= 70)) )){
                                txtResults.setText("   Overweight");
                                txtResults.setForeground(Color.RED);
                                txtResults.setBackground(Color.BLACK);
                                     
                                }          
    }

  19. #19
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    I think I am good... Let me check this out...
    Who Cares... As Long As It Works...

  20. #20
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Argh! Machine-generated code of doom.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast

Similar Threads

  1. keyListener isn't working for me
    By lost_in_java in forum AWT / Swing
    Replies: 7
    Last Post: 12-05-2008, 05:24 AM
  2. KeyListener Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:46 PM
  3. How to use KeyListener and ActionListener
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 09:19 PM
  4. how to add a KeyListener
    By leonard in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:44 PM
  5. Help with KeyListener in applet
    By mathias in forum Java Applets
    Replies: 1
    Last Post: 08-06-2007, 03:22 AM

Posting Permissions

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