How about changing the actionPerformed code...public void actionPerformed(ActionEvent ae) {JButton btn = (JButton) ae.getSource();if (btn == cnbutton) {....} else if (btn == okbutton) {...}}