Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I subclass JFrame/JPanel or not?

Tags:

java

swing

I was used to subclassing the window-classes in other programming environment, but in the java tutorials I usually see something like

JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));

p.add(aComponent);
p.add(anotherComponent);

So what are the conventions in Java regarding subclassing top-container classes?

like image 788
Georg Schölly Avatar asked May 05 '11 09:05

Georg Schölly


People also ask

Should you extend JFrame?

Avoid extending JFrame as it ties your GUI to being, well a JFrame. If instead you concentrate on creating JPanels instead, then you have the freedom to use these JPanels anywhere needed -- in a JFrame, or JDialog, or JApplet, or inside of another JPanel, or swapped with other JPanels via a CardLayout.

What is the difference between JPanel and JFrame?

Basically, a JFrame represents a framed window and a JPanel represents some area in which controls (e.g., buttons, checkboxes, and textfields) and visuals (e.g., figures, pictures, and even text) can appear.

Does JPanel extend JFrame?

You can't do it. Jpanel and jframe are both classes and java doesn't support multiple inheritance of classes. Only one class and many interfaces. Don't extend frame or other top level containers.


4 Answers

Use the same principle as you would for all java classes. If you are modifying or extending the behaviour or functionality, then by all means, extend JPanel or JFrame. The trick is to think carefully and decide if you really are adding anything. In most cases when I see people extending JFrame it is unnecessary and wrong;

public class MyFrame extends JFrame {
  public static void main(String[] args) {
    new MyFrame().setVisible(true);
  }
}

Why bother extending JFrame? You haven't added anything! Composition is a much better option.

Extending JPanel is a little different. JPanel is a fairly abstract idea, its just a generic container for other components. IMHO it is valid to subclass JPanel to create a more concrete panel, and then use that in your application. It promotes OOP and encapsulation.

For example, say your GUI had 2 main display areas; one with some buttons/controls/inputs and another which displayed output (eg in a text area). It is perfectly acceptable to subclass JPanel to create a ControlPanel that contains the buttons/controls/inputs. This moves all the code into a nice neat module, cleaning up your class that contains and handles the main JFrame.

like image 94
Qwerky Avatar answered Sep 22 '22 07:09

Qwerky


There is no rule for this, it depends how you think about your Object. You may have a JPanel which has a specific layout and bahavior, so its e.g. a VideoViewerPanel. But even this VideoViewerPanel may contain a JPanel which is only there to arrange some Buttons so you do not expicitly name it and just use it as JPanel.

like image 33
rurouni Avatar answered Sep 22 '22 07:09

rurouni


I think this is related to the Liskov substitution principle, you should look into it.

Liskov substitution principle

Liskov substitution principle: Applied example

like image 23
G-Man Avatar answered Sep 22 '22 07:09

G-Man


A well-known and good practice is to avoid subclassing top-level containers (JFrame, JDialog, JInternalFrame).

Regarding JPanel, several practices are in use:

  • subclass it for every view (then add all components inside the subclass constructor)
  • create a ViewBuilder (for each kind of view) that dynamically adds components to a "standard" JPanel

I generally use the first option, which seems more logical to me, but I also sometimes use the second way, with some level of adaptation: my view builder actually creates and stores (as fields) all components but adds them to an existing panel (passed as an argument).

For example, I use that in order to reuse sets of components: e.g. I have an AddressView class that works like that and I add it twice to a ContactView that subclasses JPanel, once for home address, once for office address.

One may say that I could also subclass JPanel for AddressView and then add 2 instances to my ContactView panel. The reason I don't do that is because Swing LayoutManagers don't support alignment of components across different panels, thus the resulting ContactView panel is not visually pleasing in this case.

like image 20
jfpoilpret Avatar answered Sep 23 '22 07:09

jfpoilpret