100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Introduction to Swing R50,00
Add to cart

Class notes

Introduction to Swing

 7 views  0 purchase

Swing components were named after a musical style that was popular in the 1940s. The name is meant to imply that the components have style and pizzazz. You have already used the JoptionPane component that is part of the String class. The Swing classes are part of a more general set of UI programm...

[Show more]

Preview 4 out of 72  pages

  • June 16, 2021
  • 72
  • 2020/2021
  • Class notes
  • Dr. simbarashe
  • All classes
book image

Book Title:

Author(s):

  • Edition:
  • ISBN:
  • Edition:
All documents for this subject (7)
avatar-seller
keanuperumal
CSI21M1 Java Programming Notes, prepared by Mr. S. Nyika, Semester I, 2019


Lecture 7: Introduction to Swing

Objectives:
At the end of this lecture, students will be able to:
• Create graphical user interfaces using classes from the javax.swing package.
• Understand swing components and use inheritance to build applications
• extend the JFrame class or any other swing component.
• Understand why the name Swing was chosen for the package.
• Use layout managers -FlowLayout, GridLayout, BorderLayout and CardLayout
• use the ActionListener class and the ItemListener
• create anonymous objects for components or any classes
• Sketch the swing application GUI before coding it in the editor
• use the ActionCommand to get the title of buttons
• use the ComboBox, ListBox, JMenu, JMenuItem and JPanel classes
Notes:

Computer programs usually are more user friendly (and more fun to use) when they contain user
interface (UI) components. UI components are button, texts fields, and other components with which
the user can interact. Java's creators have packaged a number of components prewritten components in
the Swing package. Swing components are UI elements such as dialog boxes and buttons, you can
usually recognize their names because thy begin with J.

Swing components were named after a musical style that was popular in the 1940s. The name is
meant to imply that the components have style and pizzazz. You have already used the JoptionPane
component that is part of the String class. The Swing classes are part of a more general set of UI
programming capabilities that are colletively called the Java Foundation Classes, or JFC. JFC includes
Swing component classes and selected classes from the java.awt.package. In early versions of Java,
components had simple names, such as Frame and Button. The components created from these original
classes did not have a consistent appearance when used with different browsers and operating systems.
When Java's creators designed new, improved classes, they needed new names for the classes, so they
used a J in front of each new class name. Hence, Swing components have names like JFrame,
JButton, JScrollbar, JOptionPane and so on.

UI components are also called controls or widgets. Each Swing component is a descended of a
JComponent which is in turn inherits from the java.awt.Container class. You can insert the statement
import javax.swing.*; at the beginning of your class files so you can take advantage of the Swing UI
components and their methods. When hyo import Swing classes, you use the javax.swing package
instead of java.swing package and the x stood for extension, so named because the Swing classes were
an extension of the original Java language specification. When you use Swing components, you
usually place them in containers. A container is a type of component that holds other components so
you can treat a group of them as a single entity. Containers are define in the Container class. Often a
container takes the form of a window that you can drag, resize, minimize, restore and close.

As you know from lecture notes 6 – introduction to inheritance, all Java classes descend from the
Object class. The Component object (including every container) “is an” Object. The Container
class is also parent class, and the Window class is a child of Container. However, Java programmers
rarely use Window objects because the Window subclass Frame and its child the Swing component
JFrame, both allow you to create more useful objects. Window object do not have title bars or borders,
but JFrame objects do.


Page 1 of 72

, CSI21M1 Java Programming Notes, prepared by Mr. S. Nyika, Semester I, 2019



Extending the JFrame class

You can instantiated a simple JFrame object object within an application's main() method or with any
other method of any class you write. Alternatively, you can create your own class tht descends from the
JFrame class. The advantage of creating a child class of JFrame is that you can set the JFrame's
properties within your object's constructor; then, when you create your JFrame child object, it is
automatically endowed with the features you have specified, such as title, size, and default close
operation. This can be done with any swing component like JButton , JLabel, JTextBox, JPanel etc.

You already know that you create a child class by using the keyword extends in the class header,
followed by the parent class name. You also know that you can call the parent class's constructor by
using the keyword super, and that when you call super(), the call must be the first statement in the
constructor. For example, the Calculator class in the example that follows: which extends JFrame.
Within the Calculator constructor, the super() JFrame constructor is called; it accepts a String argument
to use as the JFrame's title. Alternatively, the setTitle() method could have been used. The Calculator
constructor also sets the size, visibility and default close operation for every Calculator window. Each
of the methods setSize(), setVisible(), setDefaultCloseOperation() - appears in the constructor in this
example without an object, because the object is the current Calculator being constructed. Each of
the three methods could be preceded with a this reference with exactly the same meaning. That is
within the Calculator constructor, the following two statements have identical meanings:

setSize(300,300);
this.setSize(300,300);

Each of these statements sets the size of “this” current Calculator JFrame instance.

The Calculator class




Page 2 of 72

, CSI21M1 Java Programming Notes, prepared by Mr. S. Nyika, Semester I, 2019



The application, with an anonymous object




Anonymous object



The Library (Calculator.jar) for the Calculator class used in the application.




library




The output of the application
Title Bar




Frame




Page 3 of 72

, CSI21M1 Java Programming Notes, prepared by Mr. S. Nyika, Semester I, 2019

Some methods which can be used with a JFrame

Method Purpose
void setTitle(String) Sets a JFrame title using the String argument
void setSize(int,int) Sets a JFrame's size in pixels with the width and
height as arguments
void setSize(Dimension) Sets a JFrame's size using a Dimension class
object; the Dimension(int,int) constructor create
an object that represents both a width and a height
String getTitle() Returns the JFrame's title
void setResizable(boolean) Sets the JFrame to be resizable by passing true to
the method, or sets the JFrame not to be resizable
by passing false to the method
boolean isResizable(boolean) Returns true or false to indicate whether the
JFrame is resizable.
void setVisible(boolean) Set a JFrame to be visible using the boolean
argument true ans invisible using boolean
argument false
void setBounds(int,int,int,int) Overrides the default behaviour for the JFrame to
be positioned in the top left corner of the
computer screen's desktop. The fist two arguments
are the horizontal and vertical positions of the
JFrame's upper left corner on the desktop. The
final two arguments set the width and height.
void seLocationRelativeTo(null) Positions the JFrame at the center of the computer
screen's desktop.
void setDefaultCloseOperation() Determines the behaviour of the JFrame when a
user clicks the Close button. It is used with one of
the four constants,
DO_NOTHING_ON_CLOSE (it continues
running, it basically disables the close button),
HIDE_ON_CLOSE (closes the JFrame and
continues running), EXIT_ON_CLOSE (exits the
program), and DISPOSE_ON_CLOSE (disposes
the JFrame object and keeps running). Each
constant represents an integer e.g.
EXIT_ON_CLOSE is 3.




Page 4 of 72

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through EFT, credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying this summary from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller keanuperumal. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy this summary for R50,00. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

53022 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy summaries for 14 years now

Start selling
R50,00
  • (0)
Add to cart
Added