Solved

Where should i add the ActionListener

Posted on 2003-11-03
16
211 Views
Last Modified: 2010-03-31
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){
    //operation
  }
  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 ;)

-Child-
0
Comment
Question by:InNoCenT_Ch1ld
  • 7
  • 5
  • 4
16 Comments
 
LVL 3

Author Comment

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

-Child-
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 9675793
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.

0
 
LVL 3

Author Comment

by:InNoCenT_Ch1ld
ID: 9676017
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. ;)

-Child-
0
 
LVL 92

Expert Comment

by:objects
ID: 9676036
> 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));
0
 
LVL 15

Assisted Solution

by:Javatm
Javatm earned 200 total points
ID: 9676046
>> 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 :
     http://www.hostinganime.com/sample1/Java%20Program.zip
     
     Follow the samples & Dont forget to register the database
     on your odbc datasource under control panel before compiling.

Hope this helps . . .
JAVATM
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9676065
>> maybe you can write a simple code for me just for a reference. ;)
      Try the database codes above from the link above

JAVATM :)
0
 
LVL 3

Author Comment

by:InNoCenT_Ch1ld
ID: 9676082
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());
        frame.setOpaque(false);
        frame.setVisible(true);
        desktop.add(frame);
      }
    });
    show();
  }
}
-------------------------------------------------------------------------------------------------------
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?
0
 
LVL 3

Author Comment

by:InNoCenT_Ch1ld
ID: 9676102
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..

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 15

Expert Comment

by:Javatm
ID: 9676113
>> 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 ?
0
 
LVL 3

Author Comment

by:InNoCenT_Ch1ld
ID: 9676120
Javatm>> i couldnt download the zip file.. it redirect me to http://hostinganime.com/
0
 
LVL 3

Author Comment

by:InNoCenT_Ch1ld
ID: 9676139
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?

;)

-Child-
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9676229
Try this link and download the files :
http://www.hostinganime.com/sample1/
0
 
LVL 92

Expert Comment

by:objects
ID: 9676253
> 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.

0
 
LVL 3

Author Comment

by:InNoCenT_Ch1ld
ID: 9676520
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

-Child-
0
 
LVL 92

Expert Comment

by:objects
ID: 9676536
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9682470
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now