Where should i add the ActionListener

ok, i have a main screen (my main() class), and within this main screen, i create a JDesktopPane. I have one JInternalFrame inside. Then i make several panel(panel1.class, panel2.class) with buttons, textfields ext..

each time i click on the menu item on my main screen, the internalframe will be set to null, and then will add the panel onto the internalframe.

then i have a class call databaseConnection.class which connect to the database and, ideally, perform all the database update, create and delete. the databaseConnection class will be initiate under the main() in my mainScreen class.

so the question now are:
1. the textfields and buttons inside the panel1.class and panel2.class are all serve as creating or updating the data in the database. so it is like when i click on the save button on panel1.class, all the data in the fields will be collected and save to the database. normally i will add the actionlistener of the buttons in my mainScreen class, and it work out fine. but i find that my mainScreen class is pack of coding and not readable. so i am thinking of "move out" my ActionListener outside the mainScreen and the panel.. if possible another class call MyListener.class.. but soon i find that there are a lots of problem arise:
a) how do i "link" my database to the panel? is it advisible that i make my datbaseConnection class a static class?

b)how to create the MyListener class?

  if(e.getSource() == component){
  what should i put in for the component? if i initiate a Panel1 and Panel2 object inside MyListener, they will be two class initiating each other and it is an error! because for sure i will have to initiate the object of MyListener inside my panel1 and panel2.

So i hope i describe what i really wanted clearly. ideally, i want a class call databaseConnection to connect to the database, and mainScreen which has a JDesktopPane and a JInternalFrame, 2 panels, and one MyListener class to react and perform operation once the buttons inside the panels is click, and the data fromt the fields of the panels will be either save, or update to the database.

So can anyone tell me how to achieve that by showing me the simplest coding to demonstrate to me??

I can assure you all that this is NOT a homework, coz i have graduated. i love Java programming and that's why i wanted to learn, wanted to try, desperate to know ;)

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

InNoCenT_Ch1ldAuthor Commented:
if you find this question worth more pts, let me know.. i will add 50-200 pts for the answer depending on my satisfactory.

Mick BarryJava DeveloperCommented:
a) If your ActionListeners are getting complex then I'd suggest create a seperate class for each.

b) If you have a seperate ActionListener class for each listener then there should be no need to perform different actions depending on the source.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
InNoCenT_Ch1ldAuthor Commented:
But like how? because there are more than 1 button inside  both panel, and my listener class need to know which button (source) that trigger the actionevent.. maybe you can write a simple code for me for the panel and the listener class for that particular panel? just for a reference. ;)

OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Mick BarryJava DeveloperCommented:
> and my listener class need to know which button (source) that trigger the actionevent..

Not if you have different listeners for each task that needs to be performed.

addbutton.addActionListener(new AddListener(db, whatever));
removebutton.addActionListener(new RemoveListener(db, arg1, arg2));
>> how do i "link" my database to the panel?

     Same way you did on your main program

>> how to create the MyListener class?

     You could create a new class out side your main like
     what you proposing the " MyListner class "

     How can you do that ?

     You could get an idea like this :
     Follow the samples & Dont forget to register the database
     on your odbc datasource under control panel before compiling.

Hope this helps . . .
>> maybe you can write a simple code for me just for a reference. ;)
      Try the database codes above from the link above

InNoCenT_Ch1ldAuthor Commented:
maybe i post some sample code here:

public class MainScreen extends JFrame{
  final JDesktopPane desktop = new JDesktopPane();
  JInternalFrame frame = new JInternalFrame();

  Panel1 panel1 = new Panel1();
  Panel2 panel2 = new Panel2();
  DatabaseConnection dbconn = new DatabaseConnection();

  public MainScreen(){
    dbconn.connect();//connect to database

    //setting up the menubar and add actionlistener for the menu item
    //setSize etc...
    menuSave.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        frame = null;
        frame.add(panel1, BorderLayout.CENTER);
        frame.setSize(panel1.getWidth(), panel1.getHeight());
public class Panel1 extends JPanel{

  JTextField field1 = new JTextField();
  JTextField field2 = new JTextField();
  JButton saveButton = new JButton("save");
  JButton searchButton = new JButton("Search");

  public Panel1(){
    //setting the layout and size, add the components

**when click on the saveButton, the data inside the field1 and field2 will be save in to the database.
**when click on the searchButton, the data inside the field1 will be use as the criteria to search data from database.


public class DatabaseConnection{

  public DatabaseConnection(){

  public void connect(){
  //connect to db

  //maybe i can add some method such as:
  public void saveData(*parameters*){
    //save data...

so in this case, how should i
first) create the new listener class?
second) from the new listener class, get the data from the field1 and field2, then save to or search from the database thru DatabaseConnection class?

or is ther any other easy/simple way in doing it?
InNoCenT_Ch1ldAuthor Commented:
objects>> i think i got what you trying to tell me..

create different actionlistener class for each component (buttons).. but then i will have lots of those listener class, right? but it seems to me that this will give me a better control over my source code if i'm not mistaken..
ok, i go try it out..

>> or is ther any other easy/simple way in doing it?

    1.) Do you now how to set-up your database connection ?
    2.) Do you now how to add records ?
InNoCenT_Ch1ldAuthor Commented:
Javatm>> i couldnt download the zip file.. it redirect me to http://hostinganime.com/
InNoCenT_Ch1ldAuthor Commented:
setup db and save/update all arent a problem for me..
i have develop some amount of network + db application before.. just that last time i put all the actionListener into my main class, and this time, i wanted to shift them out as when it comes to maintenance, my old app almost drive me crazy..
i want your opinion about a better, more organized way to add the listener to my components such as JButtons or such, and how to transfer the data in the field of panels to the database.. is whether i should make my databaseConnection class a static class, or initiate it in every listener class, or just pass it instance around?


Try this link and download the files :
Mick BarryJava DeveloperCommented:
> is whether i should make my databaseConnection class a static class, or
> initiate it in every listener class, or just pass it instance around?

Depends a little on the architecture, but probably passing an instance around is best.

InNoCenT_Ch1ldAuthor Commented:
wow, i have to admit that you guys really "open" my eyes and mind with your comments and links  ;-) now i can see the way to really seperate my code instead of putting them all together inside one class..

i will increased the pts to 400 and split between you two.

Good day and God bless

Mick BarryJava DeveloperCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.