java – Why do we need to extend JFrame in a swing application?

java – Why do we need to extend JFrame in a swing application?

You dont need to extend JFrame and in fact many of us who do a lot of Swing programming make it a point not to extend this class. Myself, I try to extend classes where I plan on altering the innate behavior of the class — i.e., override one of the non-static methods of the class. Since I rarely have to do this for a JFrame, Ill rarely want to extend it.

Another reason to avoid extending it: what if you later want to display the GUI youve just created in a JDialog or a JOptionPane or in another container as part of a complex GUI? If your class extends JFrame this will be hard to do. Myself, I try to gear my GUI classes towards creating JPanels so that this is much easier to do.

A silly example based on your code:

import javax.swing.*;

// this guy extends *nothing*
public class TunaExample {
   private static final int COLS = 10;
   private JPanel mainPanel = new JPanel(); // this is what Ill add to contentPane
   private JTextField field1 = new JTextField(COLS);
   private JTextField field2 = new JTextField(COLS);
   private JPasswordField passwordField = new JPasswordField(COLS);
   private JComponent[] allComponents = { new JLabel(Field 1:), field1,
         new JLabel(Field 2:), field2, new JLabel(Password:), passwordField };

   public TunaExample() {
      field2.setEditable(false);
      field2.setFocusable(false);
      field1.setText(Field 1);
      field2.setText(Uneditable);

      for (JComponent comp : allComponents) {
         mainPanel.add(comp);
      }
   }

   public JComponent getMainComponent() {
      return mainPanel;
   }

   private static void createAndShowGui() {
      TunaExample tunaExample = new TunaExample();

      // creating my JFrame only when I need it and where I need it
      JFrame frame = new JFrame(Tuna Example);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(tunaExample.getMainComponent());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

The first thing to note in your code is this:

super(The title);

This actually calls the JFrame constructor, and passed it The title as a title String. This is an explicit example of using the Jframe functionality in your code. This will build the window that appears for you.

Using methods like add are all inherited from the JFrame class. These add Components to the JFrame object.

Why Inheritance?

Well, simply, your class IS a JFrame, with a little more. When you have a Is A operation, you use inheritance. The other advantage of this method is that your class can be referred to as a JFrame. That is:

JFrame tuna = new tuna();
// Note: All classes are meant to start with a capital letter.

Another viewpoint

Its important to note that you dont strictly HAVE TO inherit from a JFrame class. You can use Composition. In this instance youd have something like:

 public class Tuna {
      private JFrame parentWindow;
      // Rest of class.
 }

As mentioned above, the convention is to follow the Is A and Has A approach. If class A Is an example of class B, we tend to use inheritance. If class A has an instance of class B, then you use composition, although in most cases, inheritance is interchangeable with Composition.

Another another Viewpoint

As mentioned in the comments, you should always look for an existing API that offers this kind of functionality, before attempting to implement it yourself.

java – Why do we need to extend JFrame in a swing application?

To use JFrame in your application You can extend it as you did it in your code or make an object as

JFrame frame= new JFrame();

and then you can do it

frame.setTitle(Title);
frame.setLayout(layout);

do it in either way as you are easy but to use JFrame in Application and to access its methods etc you have to use one of these methods

Leave a Reply

Your email address will not be published.