0% found this document useful (0 votes)
82 views8 pages

Write A Short Note On The Advantages/features of JFC

The document discusses various Swing components for text entry and display. It describes the different types of Swing text components, including text fields, text areas, and styled text areas. It notes that all Swing text components inherit from the JTextComponent superclass. The document provides examples of the different Swing text component classes, such as JTextField, JTextArea, JEditorPane, and JTextPane, and describes their typical uses and capabilities. It also provides steps for creating a tree using the Swing JTree component, including creating TreeNode objects in a hierarchy and setting the tree model.

Uploaded by

rajoli_nikhil4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views8 pages

Write A Short Note On The Advantages/features of JFC

The document discusses various Swing components for text entry and display. It describes the different types of Swing text components, including text fields, text areas, and styled text areas. It notes that all Swing text components inherit from the JTextComponent superclass. The document provides examples of the different Swing text component classes, such as JTextField, JTextArea, JEditorPane, and JTextPane, and describes their typical uses and capabilities. It also provides steps for creating a tree using the Swing JTree component, including creating TreeNode objects in a hierarchy and setting the tree model.

Uploaded by

rajoli_nikhil4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Swing

1.

Write a short note on the advantages/features of JFC.


(Any 6: 1M each)
[1] Pluggable Look and Feel: Instead of being restricted to a single "look and feel",
we can select a look and feel and "plug it in". An interface made of Swing
components can look like a Win32 app, a Motif app, or a Mac app. It can use the
new "Metal" look and feel. Developers can use standard Swing components and
design their own look and feel (L&F) for them, and even allow users to select the
look and feel they prefer. The plaf package includes the standard "Pluggable Look
And Feel" classes.
[2] MVC Architecture: The Model-View-Controller (MVC) architecture is used
consistently throughout the Swing component set. The View and Controller parts of
the architecture are combined in the component. Each component has an associated
Model class and an interface it uses. We can provide our own data-model for a
component by subclassing the Model class or by implementing the appropriate
interface.
[3] Keystroke Handling: The JComponent architecture makes it easy to handle
keyboard events in nested components.
[4] Action Objects: Action-interface objects provide a single point of control for
program actions.
[5] Virtual Desktops: The JDesktopPane and JInternalFrame classes can be used to
create a virtual desktop, or "multiple document interface". A JInternalFrame can be
specified as iconizable, expandable, or closable, while the JDesktopPane provides
real estate for them to operate in.
[6] Compound Borders: Insets (the space between the edges of the component and
the area it is drawn in) can be specified with a blank border. In addition, many
border styles are available, which can be combined to create compound borders.
[7] Customized Dialogs: The JOptionPane class provides a variety of static methods
that you can invoke to create and display both message dialogs and user-choice
dialogs in a variety of formats.
[8] Standard Dialog Classes: Standard dialogs currently available include:
JFileChooser and JColorChooser
[9] Structured Table and Tree Components: The JTable class provides a data-aware
matrix. JTree provides hierarchical-structuring of data elements.
[10] Powerful Text Manipulations: In addition to single-font text fields and text
areas, Swing provides a JPassword field for hidden input and a JTextPane class for
displaying multi-font text. In addition, the JEditorPane class provides editing
capabilities for multi-font text, while the text.html and text.rtf packages handle text
encoded in HyperText Markup Language (HTML) or Rich Text Format (RTF).
[11] Generic Undo Capabilities: The undo package provides generic undo
capabilities that can be used in a variety of situations.
[12] Accessibility Support: Swing has built-in support for developers to make
products that are compatible with Assistive Technologies (for alternative interfaces
like, for example, braille.) All of the Swing components implement interface
Accessible.

2.

3.

How are swing components different from AWT components?


(1M each)
The AWT components are those provided by the JDK 1.0 and 1.1 platforms.
The Java 2 Platform still supports the AWT components.
We can identify Swing components because their names start with J. The
AWT button class, for example, is named Button, while the Swing button class is
named JButton.
The AWT components are in the java.awt package, while the Swing
components are in the javax.swing package.
The biggest difference between the AWT components and Swing
components is that the Swing components are implemented with absolutely no
native code.
Since Swing components aren't restricted to the least common denominator the features that are present on every platform -- they can have more functionality
than AWT components.
Even the simplest Swing components have capabilities far beyond what the
AWT components offer:
Swing buttons and labels can display images instead of, or in addition to,
text.
We can easily add or change the borders drawn around most Swing
components. For example, it's easy to put a box around the outside of a
container or label.
We can easily change the behavior or appearance of a Swing component by
either invoking methods on it or creating a subclass of it.
Swing components don't have to be rectangular. Buttons, for example, can
be round.
Assistive technologies such as screen readers can easily get information
from Swing components. For example, a tool can easily get the text that's
displayed on a button or label.
Swing lets you specify which look and feel your program's GUI uses. By contrast,
AWT components always have the look and feel of the native platform.
Write short note on JFrame:
(Description 2M + Constructor 2M + Methods 2M)
(Purpose) JFrame class is an extended version of java.awt.Frame that adds support
for the JFC/Swing component architecture.
A JFrame object is a window with borders and a title bar. It can have a menu bar also.
A JFrame has some notion of how to respond when the user attempts to close the
window. The default behavior is to simply hide the JFrame when the user closes the
window. To change the default behavior, we invoke the method
setDefaultCloseOperation(int operation). Operations must be one of the following
values.

DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do


anything; require the program to handle the operation in the windowClosing
method of a registered WindowListener object.
HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the
frame after invoking any registered WindowListener objects.
DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide
and
dispose the frame after invoking any registered WindowListener objects.
EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System
exit method. Use this only in applications.
The value is set to HIDE_ON_CLOSE by default.
Constructors
1) JFrame():
2) JFrame(String title):
If we pass a String object to the constructor, the content of the string appears in the
title bar of the frame window.
Methods
1) Container getContentPane():
Returns the contentPane object for this frame.
2) void setContentPane(Container contentpane): Sets the contentPane property.
3) JMenuBar getJMenuBar():
Returns the menubar set on this frame.
4) void setJMenuBar(JMenuBar menubar): sets the menubar for this frame
5) void setDefaultCloseOperation(int operation):
Sets the operation that will happen by default when the user initiates a close on
this frame
6) void setVisible(boolean b):
Shows or hides this component depending on the value of parameter b.
7) Graphics getGraphics():
Creates a graphics context for this component. This method will return null if this
component is currently not displayable.
Code Snippet
JFrame
jf
=
new
JFrame(Demo);
jf.setLayout(new FlowLayout());
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS
E); jf.setSize(300,300); jf.setVisible(true);
4.

Write a short note on Text-Entry Components

Swing text components display text and optionally allow the user to edit the text.
Programs need text components for tasks ranging from the straightforward (enter a
word and press Enter) to the complex (display and edit styled text with embedded
images in an Asian language).
Swing provides six text components, along with supporting classes and interfaces that
meet even the most complex text requirements. In spite of their different uses and
capabilities, all Swing text components inherit from the same superclass,
JTextComponent, which provides a highly-configurable and powerful foundation for
text manipulation.
The following figure shows the JTextComponent hierarchy.

Group

Text Controls

Plain Text
Areas

Description
Also known simply as text fields, text
controls can display only one line of
editable text. Like buttons, they generate
action events. Use them to get a small
amount of textual information from the
user and perform an action after the text
entry is complete.
JTextArea can display multiple lines of
editable text. Although a text area can
display text in any font, all of the text is in
the same font. Use a text area to allow the
user to enter unformatted text of any length
or to display unformatted help information.

Swing Classes
JTextField and its
subclasses
JPasswordField
And
JFormattedTextField

JTextArea

Styled Text
Areas

A styled text component can display


editable text using more than one font.
Some styled text components allow
embedded images and even embedded
components. Styled text components are
powerful and multi-faceted components
suitable for high-end needs, and offer more
avenues for customization than the other
text components.

JEditorPane
and its subclass
JTextPane

Because they are so powerful and flexible,


styled text components typically require
more initial programming to set up and
use. One exception is that editor panes can
be easily loaded with formatted text from a
URL, which makes them useful for
displaying uneditable help information.

5.

How to create a tree using Swing components?


(Purpose) JTree is a component that displays information in hierarchical format.
Steps for creating a JTree are as follows:
i) Create an instance of JTree
ii) Create objects for all the nodes required with the help of DefaultMutableTreeNode,
which implements MutableTreeNode interface which extends the TreeNode
interface. The TreeNode interface declares methods that obtains information about
a TreeNode. To create a hierarchy of TreeNodes the add() of the
DefaultMutableTreeNode can be used.
Methods of TreeNode interface
TreeNode
getChild(int
childIndex)
int getChildCount()
int getIndex(TreeNode node)

TreeNode getParent()

Returns the child TreeNode at index childIndex


Retruns the number of children TreeNodeS the
receiver contains
Returns the index of node in the receivers
children. If the receiver does not contain node, -1
will be returned.
Returns the parent TreeNode of the receiver.

Methods of MutableTreeNode
void insert(MutableTreeNode Adds child to the receiver at index.
child, int index)

void remove(int index)


void remove(MutableTreeNode
node)

Removes the child at index from the receiver.


Removes node from the receiver.

void
setParent(MutableTreeNode
newParent)

Sets the parent of the receiver to newParent.

Methods of DefaultMutableTreeNode
void
add(MutableTreeNode Removes newChild from its parent and makes it
newChild)
a child of this node by adding it to the end of this
node's child array
int getChildCount()
Returns the number of children of this node.
TreeNode[] getPath()
Returns the path from the root, to get to this
node.
TreeNode getRoot()
Returns the root of the tree that contains this
node.
boolean isRoot()
Returns true if this node is the root of the tree.
Create the object of the tree with the topmost node as argument
iii) Use the add method to create the hierarchy
iv) Create the object of JScrollPane and add the JTree to it. Add the scroll pane to the
content pane.
Event Handling. (2 marks)
The JTree generates a TreeExpansionEvent which is in the package javax.swing.event.
The getPath() of this class returns a TreePath object that describes the path to the changed
node.
The addTreeExapnsionListener and removeTreeExpansionListener methods allows
listeners to register and unregister for the notifications. The TreeExpansionListener
interface provides two methods
void treeCollapsed(TreeExpansionEvent
Called whenever an item in the tree has
event)
been collapsed
void treeExpanded(TreeExpansionEvent
Called whenever an item in the tree has
event)
been expanded

The addTreeSelectionListener and removeTreeSelectionListener methods allows


listeners to register and unregister for the notifications. The TreeSelectionListener
interface provides one method
void valueChanged(TreeSelectionEvent e)
Called whenever the value of the
selection

changes.
Example:
import java.awt.*;
import
java.awt.event.*;
import javax.swing.*;
import
javax.swing.tree.*;
/*<applet code="JTreeEvents" width=300 height=300>
</applet>*/
public class JTreeEvents extends JApplet
{
JTree tree;
JTextField jtf;
public void init()
{
Container contentPane=getContentPane();
contentPane.setLayout(new BorderLayout());
DefaultMutableTreeNode top=new DefaultMutableTreeNode("Options");
DefaultMutableTreeNode a=new DefaultMutableTreeNode("A");
top.add(a);
DefaultMutableTreeNode a1=new DefaultMutableTreeNode("A1");
a.add(a1);
DefaultMutableTreeNode b=new DefaultMutableTreeNode("B");
top.add(b);
DefaultMutableTreeNode b1=new DefaultMutableTreeNode("B1");
b.add(b1);
tree=new JTree(top);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp=new JScrollPane(tree,v,h);
contentPane.add(jsp,BorderLayout.CENTER);
}
}

6.

Explain in detail JScrollPane


JScrollPane provides a scrollable view of a lightweight compo

int

You might also like