Results 1 to 8 of 8
  1. #1
    towmtdj is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default action event doesn't seem to activate actionListener

    I have a frame showing some textfields and a couple of buttons, the classical "Ok" and "Reset" buttons, which are supposed to confirm the values of the textfields (closing the frame) and reset them respectively.
    I think I thoroughly followed the "delegation method procedure" for managing event, that is , create the button object, define a listener class (implementing the ActionListener interface) , and add the listener class to the button using the "addActionListener" method . But there's no way to make the whole thing work.
    No exception. Only when I click the button the "actionPerformed" method of the listener is simply ignored, as if it didn't exist.
    What is the problem ?

    Here is the code :


    public FrameViewAddEstrazione (String ExtrPath)
    {
    try
    {
    FilePath = ExtrPath;

    F = new JFrame ();
    PP = new JPanel();
    PD = new JPanel(); // pannello data
    PE = new JPanel(); // pannello estrazioni
    PX = new JPanel(); // pannello pulsanti

    F.setLayout (new BoxLayout(F.getContentPane(), BoxLayout.Y_AXIS));
    F.add(PP); F.add(PD); F.add(PE); F.add(PX);

    // Path del file estrazioni
    JLabel LP = new JLabel ();
    TP = new JTextField (ExtrPath);
    TP.setColumns(40);
    PP.add (LP); PP.add(TP);

    // Data
    JLabel LYear = new JLabel ("Year"); TYear = new JTextField("",4) ;
    JLabel LMonth = new JLabel ("Month"); TMonth = new JTextField("",2) ;
    JLabel LDay = new JLabel ("Day"); TDay = new JTextField("",2) ;
    PD.add(LYear); PD.add(TYear); PD.add(LMonth); PD.add(TMonth); PD.add(LDay); PD.add(TDay);

    // Estrazione
    JLabel EL = new JLabel("Estrazioni");
    E1 = new JTextField ("",2);
    E2 = new JTextField ("",2);
    E3 = new JTextField ("",2);
    E4 = new JTextField ("",2);
    E5 = new JTextField ("",2);
    E6 = new JTextField ("",2);
    PE.add(EL); PE.add (E1); PE.add(E2); PE.add(E3); PE.add(E4); PE.add(E5); PE.add(E6);

    // Pulsanti di conferma / reset
    BOK = new JButton ("Ok");
    //BOK.setActionCommand("OK");
    BReset = new JButton ("Reset");
    //BReset.setActionCommand("Reset");
    PX.add (BOK); PX.add(BReset);
    BOK.addActionListener (this);
    BReset.addActionListener(this);

    F.setSize(400, 200);
    F.setVisible(true);

    }
    catch (Exception E)
    {
    E = E;
    }
    }

    public void actionPerformed(ActionEvent e)
    {
    try
    {

    String Cmd = e.getActionCommand();
    String File ="", chunk;
    BufferedReader S = new BufferedReader (new FileReader (FilePath));
    chunk = S.readLine();
    while (chunk != "")
    {
    File += chunk;
    chunk = S.readLine();
    }

    if (Cmd == "OK")
    {



    F.setVisible(false);
    }
    if (Cmd =="Reset")// reset all values
    {
    TP.setText(FilePath);
    TYear.setText(""); TMonth.setText(""); TDay.setText("");
    E1.setText("");
    E2.setText("");
    E3.setText("");
    E4.setText("");
    E5.setText("");
    E6.setText("");
    }
    }
    catch (Exception E)
    {
    E=E ;
    }

    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: action event doesn't seem to activate actionListener

    You can't compare Strings for equality whith the == operator (you can't compary any two objects like that); use the equals( ... ) method instead.

    kind regards,

    Jo
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: action event doesn't seem to activate actionListener

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    towmtdj is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: action event doesn't seem to activate actionListener

    Well this sure is a problem but, the fact is that, the actionPerformed () method is never called ... I expect that the control flow at least enter the method , and then maybe give lots of errors.... but the method seems not to be called . Like if the event is not fired at all ....
    Greetings,

    Leo

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: action event doesn't seem to activate actionListener

    How do you know it's never entered?
    I see no logging in there to trace what it's doing.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    towmtdj is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: action event doesn't seem to activate actionListener

    I see it by debugging the code ... put a breakpoint both on the first sentence ("String Cmd = e.getActionCommand();")and in the "catch" section and none of the two are ever executed ....

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: action event doesn't seem to activate actionListener

    Well, it's not exactly easy to read the code you posted.
    Even if you added code tags, it still looks like you have a non-standard way of formatting and naming.

    I would still put printlns in the code rather than debugging.

    And if it's still not going in there then either that is not the actionPerformed that is being used, or you are not running the code you think you are.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    311
    Rep Power
    10

    Default Re: action event doesn't seem to activate actionListener

    I "recognized" you put try-catch block inside actionPerformed() method which is good since you are reading from file system.
    But you deprive yourself of its main advantage by not doing anything if any of code lines in try produces an exception.
    Why you did not use catch to monitor possible issues?
    So far if code in try fires some exception it will "exit" to catch part and... well, do nothing. Just what you explained as a problem with your button pressing..
    At least put E.printStackTrace();

    Not saying that this is your issue, but at least you will be sure that part is doing its job well..

Similar Threads

  1. Replies: 8
    Last Post: 01-05-2014, 03:37 PM
  2. Action Event buttons
    By MBP9004 in forum New To Java
    Replies: 6
    Last Post: 03-12-2013, 03:19 PM
  3. N o Action by ActionListener
    By MetalR0 in forum New To Java
    Replies: 6
    Last Post: 07-17-2011, 08:29 PM
  4. Stop action event in JDialog
    By eleda in forum New To Java
    Replies: 2
    Last Post: 10-15-2010, 04:24 AM
  5. Action Event and Listeners
    By lost1 in forum New To Java
    Replies: 3
    Last Post: 11-14-2007, 05:26 AM

Tags for this Thread

Posting Permissions

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