Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Calling a Method at run time

Calling a Method at run time

Scheduled Pinned Locked Moved Java
javadatabasecomhelptutorial
7 Posts 2 Posters 23 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MallardsReach
    wrote on last edited by
    #1

    I am using an example code that I found line JavaPoint which works and does what I would want it to do, so I thought I would use it in my Netbeans project. It took a while to figure out what did what as there were no comments and obscure naming eg buttons name is 1. I now have it working and have renamed things there is one problem I can't figure and it's a call to a method called set() The program starts with a question and 4 possible radiobutton answers you select an answer then click button next which shows question 2. For some reason It doesn't show question 1 when the form first loads. Tried various things but none worked for me. The Next Button Code:

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {                                        
        if(check()) // Check answer
            count=count+1;
    	current++;
            set();	//Next question
    if(current==9)
    {
         JOptionPane.showMessageDialog(this,"Correct Answers = "+count);
         btnNext.setEnabled(false);
            //b2.setText("Result");
    }
    }
    

    First section of the set() Method code

    void set()
    {
    rbSetQuestion.setSelected(true);
    if(current==0)
    {
    lblQuestions.setText("Question1: Which one among these is not a datatype");
    rbChoice_1.setText("int");rbChoice_2.setText("Float");rbChoice_3.setText("boolean");rbChoice_4.setText("char");
    }
    if(current==1)
    {
    lblQuestions.setText("Question2: Which class is available to all the class automatically");
    rbChoice_1.setText("Swing");rbChoice_2.setText("Applet");rbChoice_3.setText("Object");rbChoice_4.setText("ActionEvent");
    }

    L 1 Reply Last reply
    0
    • M MallardsReach

      I am using an example code that I found line JavaPoint which works and does what I would want it to do, so I thought I would use it in my Netbeans project. It took a while to figure out what did what as there were no comments and obscure naming eg buttons name is 1. I now have it working and have renamed things there is one problem I can't figure and it's a call to a method called set() The program starts with a question and 4 possible radiobutton answers you select an answer then click button next which shows question 2. For some reason It doesn't show question 1 when the form first loads. Tried various things but none worked for me. The Next Button Code:

      private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {                                        
          if(check()) // Check answer
              count=count+1;
      	current++;
              set();	//Next question
      if(current==9)
      {
           JOptionPane.showMessageDialog(this,"Correct Answers = "+count);
           btnNext.setEnabled(false);
              //b2.setText("Result");
      }
      }
      

      First section of the set() Method code

      void set()
      {
      rbSetQuestion.setSelected(true);
      if(current==0)
      {
      lblQuestions.setText("Question1: Which one among these is not a datatype");
      rbChoice_1.setText("int");rbChoice_2.setText("Float");rbChoice_3.setText("boolean");rbChoice_4.setText("char");
      }
      if(current==1)
      {
      lblQuestions.setText("Question2: Which class is available to all the class automatically");
      rbChoice_1.setText("Swing");rbChoice_2.setText("Applet");rbChoice_3.setText("Object");rbChoice_4.setText("ActionEvent");
      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Where is current initialised? Also, it would be better to put the text of your questions and answers in some sort of List, and pass the current variable to the set method. All set then needs to do is select the values of question and answers based on the List index.

      M 1 Reply Last reply
      0
      • L Lost User

        Where is current initialised? Also, it would be better to put the text of your questions and answers in some sort of List, and pass the current variable to the set method. All set then needs to do is select the values of question and answers based on the List index.

        M Offline
        M Offline
        MallardsReach
        wrote on last edited by
        #3

        Hi Richard, Thanks for taking the time to look and answer. Here is the start of the code. Once I get it to work I will then read up on list. Just wanted to solve why its not working.When running the example in cmd does.

        public PracticeTest() {
            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")
        
                                 
         int count=0,current=0;
        
        /\*\*
         \*
         \* @param s
         \*/
        
        
           set();  //set() on its own gives error, (invalid method declaration; return type required)
         {
           counter = 0
           set()  //set() like this no errors, build successful but doesn't show 
         }
        
        L 1 Reply Last reply
        0
        • M MallardsReach

          Hi Richard, Thanks for taking the time to look and answer. Here is the start of the code. Once I get it to work I will then read up on list. Just wanted to solve why its not working.When running the example in cmd does.

          public PracticeTest() {
              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")
          
                                   
           int count=0,current=0;
          
          /\*\*
           \*
           \* @param s
           \*/
          
          
             set();  //set() on its own gives error, (invalid method declaration; return type required)
           {
             counter = 0
             set()  //set() like this no errors, build successful but doesn't show 
           }
          
          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          The last five lines of that do not make any sense. The first line is (or should be) a method declaration (but it is not), hence the error message. A call to a method cannot exist at class level, it must be inside an executable block. The next four define a method that has no name, so can never be executed.

          M 1 Reply Last reply
          0
          • L Lost User

            The last five lines of that do not make any sense. The first line is (or should be) a method declaration (but it is not), hence the error message. A call to a method cannot exist at class level, it must be inside an executable block. The next four define a method that has no name, so can never be executed.

            M Offline
            M Offline
            MallardsReach
            wrote on last edited by
            #5

            That's done it :) Not sure about the first five lines as I'm using Netbeans and it puts those lines in. But this is the that solved it.

            Quote:

            A call to a method cannot exist at class level, it must be inside an executable block.

            So all I needed was,

            private void rbSetQuestionFocusGained(java.awt.event.FocusEvent evt) {                                          
                    set();
            } 
            

            As rbSetQuestion is selected at the start, the set() method is executed. Now to have a look at lists.

            L 1 Reply Last reply
            0
            • M MallardsReach

              That's done it :) Not sure about the first five lines as I'm using Netbeans and it puts those lines in. But this is the that solved it.

              Quote:

              A call to a method cannot exist at class level, it must be inside an executable block.

              So all I needed was,

              private void rbSetQuestionFocusGained(java.awt.event.FocusEvent evt) {                                          
                      set();
              } 
              

              As rbSetQuestion is selected at the start, the set() method is executed. Now to have a look at lists.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              MallardsReach wrote:

              Not sure about the first five lines as I'm using Netbeans

              Which is a good reason to abandon Netbeans and use a simple editor*. That way you know that all the code is what you have written, rather than something the framework thinks you need. Once you have really mastered Java then go back to Netbeans, but until then it is only getting in your way. I speak from experience of trying to learn Visual C++ and MFC at the same time. *notepad, notepad++, eclipse, or even Visual Studio Code, will all do the job. There are probably also many others that I have never looked at.

              M 1 Reply Last reply
              0
              • L Lost User

                MallardsReach wrote:

                Not sure about the first five lines as I'm using Netbeans

                Which is a good reason to abandon Netbeans and use a simple editor*. That way you know that all the code is what you have written, rather than something the framework thinks you need. Once you have really mastered Java then go back to Netbeans, but until then it is only getting in your way. I speak from experience of trying to learn Visual C++ and MFC at the same time. *notepad, notepad++, eclipse, or even Visual Studio Code, will all do the job. There are probably also many others that I have never looked at.

                M Offline
                M Offline
                MallardsReach
                wrote on last edited by
                #7

                Once again Richard, Thanks for the advice. I guess like most beginners I couldn't understand how writing code in a text editor and running it in cmd made a visual program. I did start with notepad ++ and compiled and run some programs but the result only showed itself in the cmd window, what use is that? then after looking around and gaining a little more understanding, I looked at examples where the code was written, compiled and run. Lo and behold a window popped up with a button on it! I think Netbeans makes you feel like you know but as this thread has pointed out, it's just an illusion. So back to notepad ++ and back to a step at a time. Will also look at the other editors available.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups