Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By kjkrum

Thread: probably a principle misunderstanding of OOP?

  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default probably a principle misunderstanding of OOP?

    There are two classes A and B in different files.
    Both have a single button A and B.
    I'd like to read out the button B from within class A:
    see please the ifButtonBpushed() method.
    How can that be done, any suggestion please.
    I think I am struggling with a principle misunderstanding of OOP?



    frist class:
    Java Code:
    package willemlearningjava;
    
    /**
     *
     * 
     */
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    /**
     *
     * @author dragstrawillem
     */
    public class A extends JFrame{
        public A()      {
                JPanel buttonpanel = new JPanel();
                B b = new B();
                setBounds(300, 300, 80, 100);
                setTitle("A-button");
                JButton button = new JButton("A-button");
                button.addActionListener((ActionListener) new
                    ActionListener()
                    {
                        public void actionPerformed(ActionEvent event) {
                            System.out.println(" button A pressed ");
                        }
                });
                buttonpanel.add(button); // button of class A
                buttonpanel.add(b.addButton()); // button of class B
                Container contentPane = getContentPane();
                
                contentPane.add(buttonpanel);
        }
        
        public void ifButtonBpushed() {
            
            // I'd like to read out button B from here
            // have no clue how that can be done....
            
        }
    
        public static void main(String[] args) {
                A a = new A();
                a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                a.setVisible(true);
        }
    
    }
    second class:
    Java Code:
    package willemlearningjava;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    /**
     *
     * 
     */
    public class B extends JFrame{
    
    
        public JButton addButton()  {
               JButton button = new JButton("B-button");
                  button.addActionListener((ActionListener) new
                    ActionListener()
                    {
                        public void actionPerformed(ActionEvent event) {
                            System.out.println(" button B pressed ");
                        }
                });
                return button;
        }
    
        
    
    }

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: probably a principle misunderstanding of OOP?

    First off, it's almost never required to extend JFrame.
    It definitely isn't in this case as you are not adding any functionality in either case.

    Why is B even extending JFrame anyway?
    All it has is a button.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default Re: probably a principle misunderstanding of OOP?

    Swing programming isn't terribly hard, but it does require a solid knowledge of Java basics. When doing Swing, you need to understand the concept of MVC (Model-View-Controller); that's a good starting point for some reading. Each component in Swing typically implements MVC. The controller part of each component is responsible for communicating with other components. Also, that communication is often performed using event listeners. It's easier to implement than it is to explain, but a controller publishes an interface for an event listener and allows other components to add listener instances. When the controller wants to announce that something happened, such as a a button click, then it calls all the listeners that have been added, passing an EventObject instance that describes the event. I know this isn't really an answer, but it is a good starting point for some research...
    The Java Tutorial. Read it.

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: probably a principle misunderstanding of OOP?

    "Why is B even extending JFrame anyway?
    All it has is a button."

    Tolls you are right class B should not extend JFrame!
    It is as simple as this:

    Java Code:
    public class B {
    
    
        public JButton addButton()  {
               JButton button = new JButton("B-button");
                  button.addActionListener((ActionListener) new
                    ActionListener()
                    {
                        public void actionPerformed(ActionEvent event) {
                            System.out.println(" button B pressed ");
                        }
                });
                return button;
        }
    }

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: probably a principle misunderstanding of OOP?

    Steve, I studied the MVC (Model-View-Controller) reading the COREJave book
    which is an excellent manual I believe.
    What happened to me several times is that I wrote a class with some GUI stuff
    in it (buttons etc.), and I construct a Actionlistener that reads out the signals that
    fires each GUI component.
    But when I make an object of that class within another class I do not know how
    to get hold off the action firing events, or in other words: how can I see
    from class be that a user pushed the button of class B?

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: probably a principle misunderstanding of OOP?

    Yes I have it working....
    But is it considered bad programming?

    First class (A) that reads out the button push of second class (B)

    Java Code:
    package willemlearningjava;
    
    /**
     *
     * 
     */
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    /**
     *
     * 
     */
    public class A extends JFrame{
        private B b;
        private JButton buttonA;
        private JButton newbutton;
        public A()      {
                JPanel buttonpanel = new JPanel();
                
                b = new B();
                ActionListener blistener = new ButtonListener ();
                setBounds(300, 300, 80, 100);
                setTitle("A-button");
    
                buttonA = new JButton("A");
                buttonA.addActionListener(blistener);
                newbutton = b.getButton();
    
                newbutton.addActionListener(blistener);
    
                buttonpanel.add(buttonA); // button of class A
                buttonpanel.add(newbutton); // button of class B
                Container contentPane = getContentPane();         
                contentPane.add(buttonpanel);
        }
    
    
        public class ButtonListener implements ActionListener {
          
           public void actionPerformed(ActionEvent e)  {
               String cmd = e.getActionCommand();
               Object source = e.getSource();
               
                if (cmd.equals("A"))  {
                     System.out.println(" button A pressed ");
                }
                if (cmd.equals("B"))  {
                     System.out.println(" button B pressed noted within class A");
                }
           }
        }
    
    
        public static void main(String[] args) {
                A a = new A();
                a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                a.setVisible(true);
        }
    
    }

    second class with single button:

    Java Code:
    package willemlearningjava;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    /**
     *
     * 
     */
    public class B {
        private JButton buttonB;
    
        public JButton getButton()  {
               buttonB = new JButton("B");
                 
               return buttonB;
        }
        
    }

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: probably a principle misunderstanding of OOP?

    If you want class A to know when a button in class B is pushed, then class B needs to provide a means for class A to register a listener on that button.
    Get in the habit of using standard Java naming conventions!

  8. #8
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: probably a principle misunderstanding of OOP?

    Isn't that what I did, kjkrum?

  9. #9
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: probably a principle misunderstanding of OOP?

    Basically, yes. It would be better OOP if you strengthened the encapsulation by giving B methods to add/remove listeners instead of returning the button instance. That way, callers could not do things like modify the text of the button. This would be an example of composition and delegation. B would have delegate methods for some, but not all, of the methods of its private component, the button.
    Tolls likes this.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Misunderstanding of Break
    By Steve Whalen in forum New To Java
    Replies: 6
    Last Post: 10-22-2008, 06:31 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
  •