10 Replies Latest reply: Jul 27, 2012 12:39 PM by mrsAngelo RSS

    to do composite panels that implements interfaces

    mrsAngelo
      Hi,

      I am not able to use panels that implement interfaces to realize a composition with other panels.

      I would like to do this composition to decide at run time (using a method factory pattern) how the panels have to be arranged.

      The the code is this...:
      public ClassDialog extends JFrame {
      Interface_one panel1 ;   
      Interface_two panel2 ;  
      
      
      public ClassDialog() {
          panel1 = new Panel1();
          panel2 = new Panel2();
          
          add(panel1);
          add(panel2)
      
      }
      
      
      public class Panel1 extends JPanel implements Interface_one{
      ..
      }
      
      public class Panel2 extends JPanel implements Interface_two{
      ..
      }
      When this code is wrote, the error: "no suitable method found for add" is showed in the IDE of NetBean..

      I cannot understand what can be the reason because is not allowed to add component that implements interface..

      So I would ask an explanation for the problem that I am having.

      I should also get an advice how realize composition of panels using factory method pattern

      thank you

      regards

      Angelo Moreschini
        • 1. Re: to do composite panels that implements interfaces
          morgalr
          You have failed to extend JFrame, in your ClassDialog class, in a meaningful manner to know what to do with objects of type Interface_one and Interface_two.
          • 2. Re: to do composite panels that implements interfaces
            mrsAngelo
            thank you morgalr for your answer,      

            I don't understand very well what you mean with: "You have failed to extend JFrame" and
            "to know what to do with objects of type Interface_one and Interface_two."
            (I wont add the objects [JPanel] to the class ClassDialog [JFrame]).
            - - - - - - - - - - - -

            I tried a second example (like the first) where the classes Panel1 and Panel2 (only) don't
            implement Interfaces...

            In this second (modified) example it is not showed the message "no suitable method
            found for add" in the IDE of NetBean..

            and in this (second) case the panels (Panel1 and Panel2) are added regularly to the JFrame
            - - - - - - - - - - - -

            Could you explain better ...

            thank you
            regards

            Angelo

            Edited by: mrsAngelo on Jul 17, 2012 10:11 PM
            • 3. Re: to do composite panels that implements interfaces
              sabre150
              Java has static type checking. All the compiler knows about panel1 and panel2 is that they reference objects that implement Interface_one and Interface_two but the add() method only takes objects that are instances of JComponent and interfaces Interface_one and Interface_two are not instances of JComponent. You are initialising panel1 and panel2 with objects that are also instances of JComponent but the compiler cannot tell this from a static analysis.
              • 4. Re: to do composite panels that implements interfaces
                mrsAngelo
                Thank you sabre150,

                OK... I understand now what is the reason of the problem.
                And for the reason that you explained, I think (now also) that is not possible to use interfaces to compose panels....

                As well the possibility of change composition of panels at run time (via pattern method factory) can be very interesting to realize...

                Can you suggest me a different possibility to realize a such mechanism in the composition of panels ???
                (via inheritance, or abstract classes, for example....)

                Regards

                Angelo
                • 5. Re: to do composite panels that implements interfaces
                  gimbal2
                  mrsAngelo wrote:
                  Thank you sabre150,

                  OK... I understand now what is the reason of the problem.
                  And for the reason that you explained, I think (now also) that is not possible to use interfaces to compose panels....
                  If you want to hide it behind the interface without having to make assumptions in the code through manual typecasts you could always do something like this:
                  public interface InterfaceOne {
                    ...
                  
                    JComponent getComponent();
                  }
                  Then in an implementing class you can do this:
                  public class MySuperDuperPanel extends JPanel implements InterfaceOne {
                    ...
                  
                    public JComponent getComponent(){
                      return this;
                    }
                  } 
                  And then finally in your frame:
                  add(panel1.getComponent());
                  That way you can even flexibly create objects that do not in fact extend JPanel or any other JComponent but can still deliver a component through the getComponent() method. But it all may be complete overkill, I don't know what you're planning to do.
                  • 6. Re: to do composite panels that implements interfaces
                    mrsAngelo
                    Hi gimbal2,

                    in this way it works ...

                    that is a very good suggestion ... :-)

                    thank you very much

                    regards
                    Angelo
                    • 7. Re: to do composite panels that implements interfaces
                      gimbal2
                      mrsAngelo wrote:
                      in this way it works ...

                      that is a very good suggestion ... :-)
                      Just because something works does not make it a good design automatically. But if you're happy then I see no reason to pursue it further.
                      • 8. Re: to do composite panels that implements interfaces
                        mrsAngelo
                        hi gimbal2,

                        I understand what you say, and to complete this thread it is necessary to say that I have problem to use the panels.

                        I cannot put inside the panels components (like JButtons, JLabel..).

                        So, at last we have to think that swing components cannot be used as classes that implements interfaces...
                        And consequently panels cannot be arranged at run time...


                        Some one can add some thing to this ???
                        • 9. Re: to do composite panels that implements interfaces
                          gimbal2
                          mrsAngelo wrote:
                          So, at last we have to think that swing components cannot be used as classes that implements interfaces...
                          Yes you can, they're still components. You're simply doing it wrong and that is basically caused by you not having a firm enough grip on how the Java language works.

                          To illustrate this wouldn't compile:
                          panel1.add(new JLabel("bla!"));
                          Because the interface indeed does not have a method 'add' that accepts a JComponent; the compiler can't know that there is a JPanel lurking under there, all it sees is the interface. But this does work:
                          panel1.getComponent().add(new JLabel("bla"));
                          • 10. Re: to do composite panels that implements interfaces
                            mrsAngelo
                            Hi gimbal2,

                            Ok...
                            participating in this thread I could understand what the problem is:

                            how sabre150 said, Java has static type checking. consequently I cannot hope to have a JPanel that implements the interface.

                            However it is possible using interface to choice a normal class where we can instantiate The JPanel..

                            This is the complete code that allow to define at run time the arrangement of panels...
                            public class Class_Dialog extends JFrame {
                            
                                First_INTERFACE pannello;
                            
                                public Class_Dialog() {
                                    //-------------
                                    // alternative.....
                                    pannello = new PanelOne();
                            //        pannello = new PanelTwo();
                                    //--------------
                                    JPanel pan_ = pannello.getPanel();
                            
                                    add(pan_);
                                    JLabel label = pannello.getLabel();
                                    if (label != null){
                                        label.setText("###############");
                                    }
                                    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                    
                                    setSize(300,200);
                                    setVisible(true);
                                    
                            //        panel1.getComponent().getRootPane().getAccessibleContext().
                                }// costruttore
                                
                                public static void main(String[] args){
                                    Class_Dialog cd = new Class_Dialog();
                                }
                            } 
                            public interface First_INTERFACE {
                                public void doSomeThing();
                                public JPanel getPanel();
                                public JLabel getLabel();
                            }
                            public class PanelOne  implements First_INTERFACE {
                            JLabel labelOne;
                            JPanel panel = new JPanel();
                                public PanelOne() {
                                   labelOne = new JLabel("aabbccddeeff") ;
                                   panel.add(labelOne);
                                }
                            
                                public void doSomeThing() {
                            //        ???????????
                                }
                            
                                public JPanel getPanel() {
                                    return panel;
                                }
                            
                                public JLabel getLabel() {
                                    return labelOne;
                                }
                            }
                            public class PanelTwo  implements First_INTERFACE {
                                JPanel jPanel;
                                JButton button;
                                public PanelTwo() {
                                    jPanel = new JPanel();
                                    button = new JButton("button") ;
                                    jPanel.add(button);
                                }
                            
                            
                                public void doSomeThing() {
                            //        do something ….
                                }
                            
                                public JPanel getPanel() {
                                    return jPanel;
                                }
                            
                                public JLabel getLabel() {
                                    return null;
                                }
                            }
                            that is good ???

                            regards
                            thank you

                            Angelo