DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Subclasses

  1. #1
    Join Date
    Feb 2005


    I have two classes with buttons. One of the buttons in these classes do the same action. Hence I created another class which implements actionListener.

    However, I could not pass the class information to ActionListener in order to differentiate which class triggered this action. How can I do this?

    For example;
    Panel1 extends JPanel {...
    show =   new JButton("Show");
    show.addActionListener( new ShowAct (this));
    Panel2 extends JPanel {...
    show =   new JButton("Show");
    show.addActionListener( new ShowAct (this));
    public class ShowAct implements ActionListener {
       private Panel1 panel1;
       private Panel2 panel2;
       public ShowAct ( Panel1 p) { panel1= p; }
       public ShowAct (Panel2 p)    { panel2= p;}
       public void actionPerformed( ActionEvent e){
           if ( !?????????????????.id.getText().equals( "" ) ) { ..... }
    How can I pass which class has triggered this action???
    Thank you

  2. #2
    Join Date
    Mar 2005
    Sendling, MUC, .de
    You could keep references to the buttons in the panels eg.
    Panel1 extends JPanel {...
      JButton show = new JButton("Show");
      void someInitMethod() {
        show.addActionListener( new ShowAct(this) );
    // same in Panel2...
    and then in ShowAct
    public void actionPerformed(ActionEvent e) {
      if ( e.getSource() ) {
        // action specific to panel1
      } else if ( e.getSource() ) {
        // action specific to panel2
      // action common to both panels
    You could do this as said, but it seems you should think about your design again, since if the two buttons really were supposed to trigger exactly the same action you would not have to differentiate between the two panels here.
    Otherwise you'd implement two different ActionListeners which, in their actionPerformed() methods, would implement the action specific to their respective panel and then call some other method where the action common to both panels is implemented.

    Please note: that's not the one and only solution, it rather depends on what else is around and required.
    Last edited by meisl; 04-06-2005 at 06:21 PM.

  3. #3
    Join Date
    Feb 2005
    Thanks a lot for your response.

    The reason that I want to differentiate the panels in actionPerformed is because I am retreiving different inputs from these panels (panel 1 and panel2). Panel 1 and 2 have same textFields. The output corresponding to the inputs is displayed in these textfields. I just did not want to write the same code twice by creating two actionListener class. What would you recommend for this situation?

    *** means panel1 or panel2
    public void actionPerformed( ActionEvent e)
                 if ( !***.id.getText().equals( "" ) ) {
                   Statement statement =connection.createStatement();
                   String query = "SELECT * FROM Table1" +
                                  "WHERE t_id = '" +
                                  ***.id.getText() + "'";
                   ResultSet rs = statement.executeQuery( query );
                   display( rs );   //will display different output

  4. #4
    Join Date
    Mar 2005
    Sendling, MUC, .de

    funny littul demo

    Hmm. Yes, it's a good idea to avoid redundant code. That's what the concept of classes and objects (aka OOP) is about. Are you sure your (software-) design is employing this concept?
    See, I'm wondering what actually is the difference between Panel1 and Panel2 (the classes, not instances!)?

    Well, I've put together some demonstration code for you to study.

    There is a class called TFieldDemoPanel that is a JPanel with a button and a coloured JTextField. If you hit the button, the colour of the text-field changes.
    That is the behaviour that is common to all instances of the class. However, not every instance behaves exactly the same as you'll find out by running TFieldDemo which is essentially a JFrame containing three of those panels.

    To push it somewhat further, there is also TFieldDemo2 where there are more panels than in TFieldDemo and where we also have a JMenuBar:

    Only the "Colour" menu is functional, the other two are just dummies. There you have three items, namely "all blue" (changing the colour of all text-fields to blue), "switch random" (same as hitting one randomly chosen button) and "switch all" (you might guess what, but do try it - neat effect!).

    Note: I only need these three classes and I reuse the code from TFieldDemo in TFieldDemo2 (by subclassing).
    There might be some things in the code confusing you like how the switch-all thing is done (and why it is done that way). If so, simply ignore them, I'm nonetheless quite sure you can adapt the thing for your JInternalFrames and the rest - if you understand the following:

    1. Why is there (and must be) TFieldDemoPanel.switchTFieldColor(int)?
    (it's only a one-liner and in TFieldDemoPanel, it's only called from TFieldDemoPanel.actionPerformed(...) - another one-liner. So why not just put the line into actionPerformed(...)?)

    2. Why is there the protected array of TFieldDemoPanel s "panels" in TFieldDemo? What consequences were there if I just had made three instances, say, panel1, panel2 and panel3?
    (wrt reusing code)

    3. The use (and purpose) of JComponent.setActionCommand(...) in TFieldDemo2.initComponents().
    (line 34, see comment there)

    Find attached the zipped source and a screenshot (for the unpatient it is displayed above).
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by meisl; 04-11-2005 at 04:12 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center