11

I have a program in which a JPanel is added to a JFrame:

public class Test{

    Test2 test = new Test2();
    JFrame frame = new JFrame();

    Test(){

    ...
    frame.setLayout(new BorderLayout());
    frame.add(test, BorderLayout.CENTER);
    ...

    }

    //main

    ...

    }

    public class Test2{

    JPanel test2 = new JPanel();

    Test2(){

    ...

    }

}

I get an error asking me to change type of 'panel' to 'component'. I do I fix this error? It wants me to do: Component panel = new Component();

tenorsax
  • 21,123
  • 9
  • 60
  • 107
Anonymous181
  • 1,863
  • 6
  • 24
  • 27

5 Answers5

19
public class Test{

Test2 test = new Test2();
JFrame frame = new JFrame();

Test(){
...
frame.setLayout(new BorderLayout());
frame.add(test, BorderLayout.CENTER);
...
}

//main
...
}

//public class Test2{
public class Test2 extends JPanel {

//JPanel test2 = new JPanel();

Test2(){
...
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    By curiosity, is there a reason you did not extend `JFrame` directly in `Test` ? I always do this, perhaps it's a bad habit ? Upvoted. – Jean-François Savard Apr 18 '15 at 02:28
  • 4
    @Jean-FrançoisSavard I'm not adding any functionality to or changing any of the existing functionality of the frame, so I just use a plain instance. It is typically GUI builders that encourage extending frames or panels (for no good reason that I can see). – Andrew Thompson Apr 18 '15 at 02:31
  • Well that make sense, Thank's for the fast answer. – Jean-François Savard Apr 18 '15 at 02:40
  • After re-thinking to it, doesn't it make it easier to extends JFrame so you don't have to specify a variable each time you want to call the frame methods ? Not mentionning that extending allow you to pass parameters directly to parent constructor. Or perhaps it's just a matter of opinion, I did not find any relevant information on this in swing specifications. – Jean-François Savard Apr 18 '15 at 02:49
  • 1
    @Jean-FrançoisSavard *"extending allow you to pass parameters directly to parent constructor."* Once you do that you *are* changing the functionality of the frame. ;) – Andrew Thompson Apr 18 '15 at 02:55
  • That's true. If I understand what you tell me, needing to do any modification (such as setting the title) would re-direct you into extending the `JFrame` ? And the reason you did not extend in that example was only because this example does not modify even one simple properties ? – Jean-François Savard Apr 18 '15 at 02:58
  • 1
    @Jean-FrançoisSavard *"needing to do any modification (such as setting the title) would re-direct you into extending the JFrame?"* I'll typically pass the title in the constructor. E.G. as in [this example](http://stackoverflow.com/a/29672939/418556). Note also that example uses a method to configure and build the `ui` panel, again rather than extend it. I really prefer not to extend unless doing something like custom painting. – Andrew Thompson Apr 18 '15 at 03:03
4

do it simply

public class Test{
    public Test(){
        design();
    }//end Test()

public void design(){
    JFame f = new JFrame();
    f.setSize(int w, int h);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.setVisible(true);
    JPanel p = new JPanel(); 
    f.getContentPane().add(p);
}

public static void main(String[] args){
     EventQueue.invokeLater(new Runnable(){
     public void run(){
         try{
             new Test();
         }catch(Exception e){
             e.printStackTrace();
         }

 }
         );
}

}
Community
  • 1
  • 1
sabbibJAVA
  • 1,068
  • 1
  • 11
  • 17
2

Instead of having your Test2 class contain a JPanel, you should have it subclass JPanel:

public class Test2 extends JPanel {

Test2(){

...

}

More details:

JPanel is a subclass of Component, so any method that takes a Component as an argument can also take a JPanel as an argument.

Older versions didn't let you add directly to a JFrame; you had to use JFrame.getContentPane().add(Component). If you're using an older version, this might also be an issue. Newer versions of Java do let you call JFrame.add(Component) directly.

rob
  • 6,147
  • 2
  • 37
  • 56
0
Test2 test = new Test2();
...
frame.add(test, BorderLayout.CENTER);

Are you sure of this? test is NOT a component! To do what you're trying to do you should let Test2 extend JPanel !

StepTNT
  • 3,867
  • 7
  • 41
  • 82
0

Your Test2 class is not a Component, it has a Component which is a difference.

Either you do something like

frame.add(test.getPanel() );

after you introduced a getter for the panel in your class, or you make sure your Test2 class becomes a Component (e.g. by extending a JPanel)

Robin
  • 36,233
  • 5
  • 47
  • 99