• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extending classes and importing libraries basics

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've just started learning Swing.

1. Tried to use setLayout() but it wouldn't let me. Have imported libs

import javax.swing.*;
import java.awt.*;

but it doesn't work. It only worked when I extended a class with JFrame. Why do I have to do it? I already have a JFrame instance:



This code runs smooth, so it's confusing for me that it lets me create a JFrame object, and put buttons onto it, but it wants me to extend the class onto JFrame class in order to use a method. Can someone shed some light onto it?




2. I've read somewhere that AWT is old and should not be used anymore. Is there a diagram or a list with objects and properties that are manipulated only by Swing so that I could concentrate only on those? For example (made up list):

JPaper -

- width, setWidth(x)
- height, setHeight(x)
- position, setPosition(x,y)
- color, setBackground(RGBa)
- acquire, add(subordinate component)
- deprive, remove( subordinate comopnent)
- etc...

JSticker -

- width ...
- height ...
- write, setText(String)
- etc...
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a bare bones JFrame program to be filled in.

Initially, most of your efforts will go in after line 23.
 
Cornelius Flavius
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@CareyBrown Thanks for the form, but I don't understand any of these.

So, once again, this was my routine. For example, I have a main program, and want to calculate the area of hexagon, so I make a separate class with all methods needed in it, and when needed:




So I can use another class and it's methods no problem. Why can't I use JFrame's methods once I've instantiated a JFrame object? Why do I have to extend main class with JFrame, when it is obvious that main class sees SWING library and it'components, cause otherwise it would not let me use the JFrame class in the first place?
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cornelius Flavius wrote:@CareyBrown Thanks for the form, but I don't understand any of these.

So, once again, this was my routine. For example, I have a main program, and want to calculate the area of hexagon, so I make a separate class with all methods needed in it, and when needed:




So I can use another class and it's methods no problem. Why can't I use JFrame's methods once I've instantiated a JFrame object? Why do I have to extend main class with JFrame, when it is obvious that main class sees SWING library and it'components, cause otherwise it would not let me use the JFrame class in the first place?


OK, now I'm confused. What does this have to do with Swing and JFrame. Java doesn't have a Hexagon class so is this a class you created? Can't you use this class without a GUI (e.g. Swing)?
 
Cornelius Flavius
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't have to have too much with Swing. I just started learning Swing and encountered this phenomena.

Before this I was using working with input and output from the keyboard and I used:
import java.util.Scanner;

and I could use it's methods without having to extend my classes with:


 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cornelius Flavius wrote:It doesn't have to have too much with Swing. I just started learning Swing and encountered this phenomena.

Before this I was using working with input and output from the keyboard and I used:
import java.util.Scanner;

and I could use it's methods without having to extend my classes with:



That's because Swing is a "framework". A framework is an abstraction whereby the framework classes own the process and invokes the classes and methods you provide. JFrame, for example, owns the process and calls your classes and methods. This is why you have to extend JFrame whereas with Scanner you don't.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:That's because Swing is a "framework". A framework is an abstraction whereby the framework classes own the process and invokes the classes and methods you provide. JFrame, for example, owns the process and calls your classes and methods. This is why you have to extend JFrame whereas with Scanner you don't.


No, it isn't. No, it doesn't. No, you don't.

And all else aside, Scanner is a final class.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Here is a bare bones JFrame program to be filled in.


Here's my preferred style for an SSCCE for a forum, exports excluded for brevity:
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:

Carey Brown wrote:That's because Swing is a "framework". A framework is an abstraction whereby the framework classes own the process and invokes the classes and methods you provide. JFrame, for example, owns the process and calls your classes and methods. This is why you have to extend JFrame whereas with Scanner you don't.


No, it isn't. No, it doesn't. No, you don't.

And all else aside, Scanner is a final class.


From Wikipedia
The Swing Application Framework (JSR 296) is a Java specification for a simple application framework for Swing applications, with a graphical user interface (GUI) in computer software. It defines infrastructure common to most desktop applications, making Swing applications easier to create. It has now been withdrawn.The Swing Application Framework (JSR 296) is a Java specification for a simple application framework for Swing applications, with a graphical user interface (GUI) in computer software. It defines infrastructure common to most desktop applications, making Swing applications easier to create. It has now been withdrawn.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:From Wikipedia
The Swing Application Framework (JSR 296) is a Java specification for a simple application framework for Swing applications, with a graphical user interface (GUI) in computer software. It defines infrastructure common to most desktop applications, making Swing applications easier to create. It has now been withdrawn.


Don't miss the last sentence. And don't confuse the SAF with Swing; the SAF was a proposed framework designed to use Swing classes. The two are not the same thing.

Swing isn't a framework, JFrame doesn't 'own' any process (whatever that might mean) and doesn't 'call' your classes and methods.

As to whether your class should or shouldn't extend JFrame, that should hinge on an honest answer to the standard inheritance decision-maker: if your class is-a JFrame, then go for inheritance; if it has-a JFrame, then composition is the way to go.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, and don't forget that if your class is-a JFrame, any code that constructs an instance of your class can invoke any of the public methods of JFrame on your class. Methods including but not limited to setLayout(...), add(...), remove(...) and setContentPane(...).

However, when your class has-a JFrame, either as a private (or maybe protected) field or a local variable, you limit access to those JFrame methods.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have seen lots of Swing tutorials with
public class Foo extends JFrame
in. I think that is poor design and you should avoid extending Swing Components, with the probable exception of overriding the paintComponent method.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depending on your goals, overriding other methods may provide a solution, like my suggestion in Which Layout.

In my personal projects, I've found it useful to extend JPanel to create an abstract class, usually generic with just one or two abstract methods, which I can then implement in a type-specific way in minimal concretizations of the abstract class.

But so far, I've never perceived a need to extend JFrame.
 
Cornelius Flavius
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I've settled down with making a class that paints GUI:


and then calling it from the main:




This is much too cleaner for my taste, and I think I know what's going on right now.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You still need to learn to respect Swing's single threaded rule: Concurrency in Swing
 
reply
    Bookmark Topic Watch Topic
  • New Topic