I know that, as of Java 1.5, one can add a component to a JFrame like this:
myFrame.add(myButton);
instead of:
myFrame.getContentPane().add(myButton);
Why wasn’t this always the case?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As is spelled out in the JFrame API, both do the same thing: add a component to the contentPane. It’s just recently (Java 1.5 perhaps?) Swing added the syntactic sugar/convenience method to allow you to make this call directly on the JFrame (or any other Swing top-level container), but you’re still adding to the contentPane. Same for
remove(...)andsetLayout(...)This becomes all too clear if you try to set the background color of the JFrame viamyJFrame.setBackground(Color.green);and nothing happens. It is for this reason, I’m not too happy with this change. That and also because I must be an old curmudgeon.