Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 works.
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)
                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);
                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");
                BReset  = new JButton ("Reset");
                PX.add (BOK); PX.add(BReset);
                BOK.addActionListener   (this);
                F.setSize(400, 200);
            catch (Exception E)
                E = E;
        public void actionPerformed(ActionEvent e)
                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")

                if (Cmd =="Reset")// reset all values
                        TYear.setText(""); TMonth.setText(""); TDay.setText(""); 
            catch (Exception E)
                E=E ;
Updated 9-Mar-15 6:49am
Sergey Alexandrovich Kryukov 9-Mar-15 12:52pm    
Oh, I see: you fork "Cmd" value by comparison with hard-coded strings. You repeat the same line 6 times, with E1... E6, instead of having an array and loops. This is opposite to programming. First, write what you want to do accurately, so we could discuss something real.

I formatted your code for you. Please click "Improve question" to see how to do it properly next time.


1 solution

Please use an IDE and debug your coding. Eclipse is free and can be downloaded easily. It will improve your coding.

You will soon find out what is wrong.

Also you will find lots of programming problems like those variables with Capital letters. Better use speaking names for them, "e0" to "e..." will get confusing when things get complicated. a variable named "name" or "street" speaks for itself and describes the content.
Share this answer
tiwal 10-Mar-15 7:48am    
I am already using an Ide which is Netbeans : I'm not an expert and did not use extensively any other Ide as far as Java is concerned you mean I should switch to Eclipse ? Is there a method to monitor the exchange of messages/events through the JVM ?
I know my coding style is very naive , but I wrote this code in a short time and didn't care much about the readbility ....things like "e0", "e1" etc which are sure unanderstandable on a global scale, are easy to catch if scope is limited to the method they belong to ("e" basically stands for "estrazione")....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900