Professional Documents
Culture Documents
SWING COMPONENTS
IN JAVA
Object Oriented Programming – Lec
1
BSIT – SMBA B21
10/26/19
Table of Contents
Introduction of Swing API
page 3-4
2
Stream page 12
Reader page 13
References page 14
Java Swing is a lightweight Java graphical user interface (GUI) widget toolkit that
includes a rich set of widgets. It is part of the Java Foundation Classes (JFC) and includes
several packages for developing rich desktop applications in Java. Swing includes built-in
controls such as trees, image buttons, tabbed panes, sliders, toolbars, color choosers, tables,
and text areas to display HTTP or rich text format (RTF). Swing components are written entirely
in Java and thus are platform-independent.
Swing offers customization of the look and feel of every component in an application without
making significant changes to the application code. It also includes a pluggable look and feel
feature, which allows it to emulate the appearance of native components while still having the
advantage of platform independence. This particular feature makes writing applications in Swing
easy and distinguishes it from other native programs.
3
Hierarchy of Java Swing classes
4
Input Stream
5
The figures show the class hierarchy for the input stream classes in the java.io package.
As you can see from the figure, InputStream inherits from the Object class; six classes inherit
directly from InputStream. One of InputStream's descendants, Filter InputStream, is itself an
abstract class with four children.
6
File InputStream Constructors
File InputStream provides many methods for reading bytes from a file and we can create an
instance of FileInputStream using below constructors.
1. available(): This method returns an estimate of the number of remaining bytes that can be read
from input stream without blocking by the next invocation of method for this input stream.
2. close(): This method closes this file input stream and release any system resources associated
with the stream. If this stream has an associated channel then channel is closed as well.
3. finalize(): This method ensures that the close() method of this file input stream is called when
there are no more reference to it.
4. getChannel(): This method returns the unique FileChannel object associated with this file input
stream. The initial position of the returned channel will be equal to the number of bytes read
from the file and reading bytes from this stream will increment the channel’s position.
5. getFD(): This method returns the FileDescriptor object that represents the connection to the
actual file in the file system being used by this FileInputStream.
6. read(): This method reads a byte of data from this input stream and returns the next byte of data
or -1 if the end of file is reached.
7
7. read(byte[] b): This method reads byte of data up to the length of specified array(b.length) from
this input stream into specified array of bytes and returns the total number of bytes read into
the buffer or -1 if the end of file is reached.
8. read(byte[] b, int off, int len): This method reads bytes of data up to specified len from this input
stream into specified array of bytes. If specified len is not zero, the method blocks until some
input are available; otherwise no bytes are read and 0 is returned. It returns number of bytes
read into the buffer or -1 if the end of file is reached.
9. skip(long n): This method skips over and discards specified n bytes of data from the input stream
and returns the actual number of bytes skipped. It throws an IOException if the specified n is
negative.
SOURCE CODE
Output
8
Output Stream
Output
Output Stream
9
implicitly when the OutputStream is garbage collected, which occurs when the object is no
longer referenced.
As you can see OutputStream inherits from the Object class; four classes inherit directly
from OutputStream. One of OutputStream's descendants, Filter OutputStream, is itself an
abstract class with three descendants.
1. FileOutputStream(File file): Creates a file output stream to write to the file represented by
specified file object. If the file exists but is a directory rather than a regular file, does not exist
10
but cannot be created, or cannot be opened for any other reason then
a FileNotFoundException is thrown.
2. File OutputStream(File file, boolean append): Creates a file output stream to write to the file
represented by the specified File object. If the second argument is true, then bytes will be
written to the end of the file rather than the beginning. If the file exists but is a directory rather
than a regular file, does not exist but cannot be created, or cannot be opened for any other
reason then a FileNotFoundException is thrown.
4. FileOutputStream (String name): Creates a file output stream to write to the file with the
specified name.
5. FileOutputStream (String name, boolean append): Creates a file output stream to write to the
file with the specified name. If the second argument is true, then bytes will be written to the end
of the file rather than the beginning. If the file exists but is a directory rather than a regular file,
does not exist but cannot be created, or cannot be opened for any other reason
then FileNotFoundException is thrown.
2. finalize(): This method cleans up the connection to the file and ensures that the close() method
of this file output stream is called when there are no more reference to it.
11
3. getChannel(): This method returns the unique FileChannel object associated with this file output
stream. The initial position of the returned channel will be equal to the number of bytes written
to the file and writing bytes to this stream will increment the channel’s position.
4. getFD(): This method returns the FileDescriptor object that represents the connection to the
actual file in the file system being used by this FileOutputStream object.
5. write(byte[] b): This method writes b.length bytes from the specified byte array to this file
output stream.
6. write(byte[] b, int off, int len): This method writes len bytes from the specified byte array
starting at offset off to this file output stream.
7. write(int b): This method writes the specified byte to this file output stream. Implements the
write method of OutputStream.
12
OUTPUT
Stream
13
A stream is a sequence of data. In Java, a stream is composed of bytes. It's called a
stream because it is like a stream of water that continues to flow.
In Java, 3 streams are created for us automatically. All these streams are attached with
the console.
Let's see the code to print output and an error message to the console.
Reader
The Java Reader is the base class of all Reader's in the Java IO API. Subclasses include
a BufferedReader, PushbackReader, InputStreamReader, StringReader and several others.
14
Here is a simple Java IO Reader example:
Notice, that while an InputStream returns one byte at a time, meaning a value between 0 and
255 (or -1 if the stream has no more data), the Reader returns a char at a time, meaning a value
between 0 and 65535 (or -1 if the stream has no more data). This does not necessarily mean
that the Reader reads two bytes at a time from the source it is connected to. It may read one or
more bytes at a time, depending on the encoding of the text being read.
Format Use
15
Font Size: pt.12 and pt.22
Black (Text 1)
Spacing: 1.5
References
http://journals.ecs.soton.ac.uk/java/tutorial/java/io/overview.html
https://www.journaldev.com/19187/java-fileinputstream
http://math.hws.edu/eck/cs124/javanotes7/c11/s1.html
http://www.java2s.com/Tutorials/Java/Stream_Reader_Writer/Get_to_know_Java_input_outpu
t_stream_and_reader_writer.htm
https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
16