1

Is there a difference from extending to JFrame and javax.swing.JFrame?

Example:

public class Authenticator extends JFrame {

and...

public class Authenticator extends javax.swing.JFrame {
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
TBNRsiyi
  • 23
  • 1
  • 4
  • 5
    ***Don't*** extend frame or other top level containers. Instead create & use an instance of one. – Andrew Thompson Jul 14 '13 at 20:33
  • See http://stackoverflow.com/q/1143923/758280 – Jeffrey Jul 14 '13 at 20:33
  • if you have another class call `JFrame` then you need to identify with package you want to use – nachokk Jul 14 '13 at 20:33
  • @AndrewThompson Why? I always used to extend the GUI class to `JFrame`, it makes things easier for me, but i didn't know it is considered a bad practice, why is it considered a bad practice? – BackSlash Jul 14 '13 at 22:32
  • @BackSlash Do you extend `JButton` or `JLabel` when you need either? See the link by Jeffrey for more technical reasons. – Andrew Thompson Jul 15 '13 at 02:49
  • @AndrewThompson No i don't extend `JButton` or `JLabel` i just extend `JFrame` the class i want to use for GUI – BackSlash Jul 15 '13 at 14:57

4 Answers4

6

It makes no difference unless you have another class called JFrame and are importing it instead of javax.swing.JFrame.

That said, as Andrew Thompson said, you shouldn't extend JFrame, you should use an instance.

Paolo
  • 20,112
  • 21
  • 72
  • 113
Doodad
  • 1,518
  • 1
  • 15
  • 20
1

If you import javax.swing.JFrame; then you use public class Authenticator extends JFrame { if not then use public class Authenticator extends jvax.swing.JFrame {
But the second method is mostly used when you have classes with same name in different pakage, to differentiate the classes.
for example
[-]mypackage
 |----[-]pakage1
              |---TestClass.java
 |----[-]pakage2
        |---TestClass.java

Here is the situation we have a package named mypackage and two sub packages pakage1 and pakage2

now if we just import it will give this

import mypackage.pakage1.TestClass;
import mypackage.pakage2.TestClass;
class Testw
{
public static void main(String []args)
{
System.out.println("Swah!");
}
}

it will give following error

C:\Program Files\Java\jdk1.6.0_38\bin>javac Testw.java
Testw.java:2: mypackage.pakage1.TestClass is already defined in a single-type import
import mypackage.pakage2.TestClass;
^
1 error


so what you do?
In this case you use the second method which is also called fully quailfied name now you import one pakage and use fully qualified name for other.

import mypackage.pakage1.TestClass;
class Testw
{
public static void main(String []args)
{
TestClass  testclass1 = new TestClass();
mypackage.pakage2.TestClass  testclass2 = new  mypackage.pakage2.TestClass();
System.out.println("Swah!");
}
}

So the summery of whole thing is that fully qualified name is used when their is name clashing, we can also use this method when their is no name clashing ,their will be no -ve effect on program

Azmat Karim Khan
  • 437
  • 5
  • 18
0

It depends on your import. If you import javax.swing.JFrame, no.

mael
  • 2,214
  • 1
  • 19
  • 20
0

you have to import JFrame anyway, and it will be from javax.swing package most likely, you only need to write the whole package path if there are multiple JFrame classes included, so the compiler will know which one you want to use, so if there's only one JFrame class, then the import is enough, you don't have to specify it again

frauneworld
  • 72
  • 1
  • 4
  • 11