calling methods


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: calling methods

  1. #1
    Join Date
    Jan 2005
    Posts
    17

    calling methods

    Im using a progress Panel for a typing tutor. When the typing lesson ends I cannot do anything else with the Progress Panel e.g. resetting a lesson. Other less important buttons work e.g. changing the colour and font sizes etc.

    The buttons are created by an action Listener. It all works perfect untill the lesson ends, and the progress panel freezes.

    What is the correct way to call an action Listener method so that it will work when the lesson is completed?

    Here is some of the code im using.

    boolean isCorrectKey = progressPanel.type( charTyped );
    if( progressPanel.atEnd() ) {
    currentLessonLine++;
    switch (lessonNumber) {
    case 1:

    if (currentLessonLine < lesson1.length) {
    progressPanel.setText(lesson1[currentLessonLine]);
    currentLessonLine++;
    break;
    }
    case 2:

    if (currentLessonLine < lesson2.length) {
    progressPanel.setText(lesson2[currentLessonLine]);
    currentLessonLine++;
    break;
    }

    default: // end of lesson

    javax.swing.JOptionPane.showMessageDialog(null, progressPanel.message());
    javax.swing.JOptionPane.showMessageDialog(null, progressPanel.getResults());


    /*
    * Close off lesson - more typing will cause exceptions - exceeding array bounds
    */
    //}
    }
    }

    public void lessonbutton_ActionPerformed(ActionEvent event) {

    if(lessons.getSelectedItem() == "Lesson 1"){
    progressPanel.setText( lesson1[currentLessonLine] );
    mainheading.setText("Lesson 1: The home keys");
    lessonNumber =1;

    }

    else if(lessons.getSelectedItem() == "Lesson 2") {
    progressPanel.setText( lesson2[currentLessonLine] );
    mainheading.setText("Lesson 2: 'g' and 'h' keys");
    lessonNumber =2;
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    You don't actually call an actionListener, what you do it you tell actionGenerators,
    like the buttons, that your panel is willing to receive the actions they generate,
    so your panel is set up as an actionListener (implements it)
    and 'hooks up' to the button like:
    button.addActionListener(panel);
    This tells the button that your panel is an actionslistener that listens with the method
    actionPerformed(ActionEvent, a)
    So normally you never write code in your program that invokes this method, - it is
    called by the buttons, comboboxes etc. when the user does something with them.

    But you can easily do a 'button click' programmatically like:

    panel.actionPerformed(new ActionEvent(button, 0,"Java clicked it"));

    ----

    So, telling what is wrong with your code is impossible as you have used the callback
    method (actionPerformed) to set some components and vars, not how the progress
    panel ticks, and that is where the problem exists.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center