IO SWING Byju Veedu Streams Streams are ordered
IO & SWING Byju Veedu
Streams • Streams are ordered sequence of data that have 1. 2. • Two types 1. 2. • • A source A destination Character streams Byte streams Input. Stream and Output. Stream are of type Byte streams Reader and Writer streams are of type Character streams.
Byte streams • To read and write 8 -bit bytes, programs should use the byte streams, descendants of Inputstream and Outputstreams. • These streams are typically used to read and write binary data such as images and sounds. • Two of the byte streams Object. Input. Stream and Object. Output. Stream are used for object serialization.
Char streams • Reader and Writer are abstract super classes for character streams in java. io • Advantage of char streams are that they are not dependant on a specific char encoding , and are therefore easy to internationalize. • Java stores strings in unicode , an international char encoding that is capable of representing most of the world’s written languages. • Chat streams are efficient than byte streams
File. Reader public class File. Read { public static void main(String[] args) { File. Reader in=null; File. Writer out=null; try { in =new File. Reader(new File("input. txt")); out=new File. Writer(new File("output. txt")); int c; while ((c=in. read())!=-1){ out. write(c); } } catch (File. Not. Found. Exception e) { e. print. Stack. Trace(); } catch (IOException e) { e. print. Stack. Trace(); } finally{ try { in. close(); out. close(); } catch (IOException e) { e. print. Stack. Trace(); } }
Serializing objects public class Object. Serializer { /** * @param args */ public static void main(String[] args) { Object. Input. Stream in=null; Object. Output. Stream out=null; try { in =new Object. Input. Stream(new File. Input. Stream("object. txt")); out=new Object. Output. Stream(new File. Output. Stream("object. txt")); out. write. Object("Ramesh"); out. write. Object(new Date()); out. flush(); String name =(String)in. read. Object(); Date date =(Date)in. read. Object(); } catch (File. Not. Found. Exception e) { e. print. Stack. Trace(); } } } catch (Class. Not. Found. Exception e) { e. print. Stack. Trace(); }catch (IOException e) { e. print. Stack. Trace(); }
Awt • GUI toolkit which rely on native peers. • Widget library is small • Widgets only have basic functionality • Look and feel tied to OS
Swing • Uses lightweight components • It uses a variant of MVC • It has pluggable look and feel(PLAF) • It uses delegation event model
MVC • Model State data for each component • View How the component look on screen • Controller Dictates how the component reacts to events
PLAF • Default metal style • Can emulate motif and windows styles • New styles can be designed • Can be reset at runtime
Jcomponent • The common root of most of Swing UI classes • Extends java. awt. Container class • Provides the guiding framework for GUI objects
Hello. Swing public class Hello. World. Swing { /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void create. And. Show. GUI() { //Create and set up the window. JFrame frame = new JFrame("Hello. World. Swing"); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); //Add the ubiquitous "Hello World" label. JLabel label = new JLabel("Hello World"); frame. get. Content. Pane(). add(label); } } //Display the window. frame. pack(); frame. set. Visible(true); public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. create. And. Show. GUI(); }
Layout managers • Arrange widgets according to a pattern • Can update containers to handle resizing of the container or internal widgets • Make complex UI possible
Delegation Event Model • Event , Sources, Listerners • Each event is represented by an object that gives information about the event and identifies the event source • Events are fired by sources which are usually GUI components. • Components will have a list of listeners attached to them with each listener handling a specific type of event. • When an event occurs source will notify all interested listeners by invoking the handling method in the listener with the Event as the parameter.
Flow. Layout • Flow. Layout is the default layout manager for every JPanel. It simply lays out components in a single row, starting a new row if its container is not sufficiently wide
Border Layout • Every content pane is initialized to use a Border. Layout. (As Using Top -Level Containers explains, the content pane is the main container in all frames, applets, and dialogs. ) A Border. Layout places components in up to five areas: top, bottom, left, right, and center. All extra space is placed in the center area. •
Box. Layout • The Box. Layout class puts components in a single row or column. It respects the components' requested maximum sizes and also lets you align components.
Grid. Layout • Grid. Layout simply makes a bunch of components equal in size and displays them in the requested number of rows and columns.
Box. Layout. Demo public class Box. Layout. Demo { public static void add. Components. To. Pane(Container pane) { pane. set. Layout(new Box. Layout(pane, Box. Layout. Y_AXIS)); add. AButton("Button 1", pane); add. AButton("Button 2", pane); add. AButton("Button 3", pane); add. AButton("Long-Named Button 4", pane); add. AButton("5", pane); } private static void add. AButton(String text, Container container) { JButton button = new JButton(text); button. set. Alignment. X(Component. CENTER_ALIGNMENT); container. add(button); } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void create. And. Show. GUI() { //Create and set up the window. JFrame frame = new JFrame("Box. Layout. Demo"); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); //Set up the content pane. add. Components. To. Pane(frame. get. Content. Pane()); //Display the window. frame. pack(); frame. set. Visible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. create. And. Show. GUI(); } }
Hands on • Use byte streams • Reading and writing a file • Serializing object to a file • Swing application for temperature conversion from Celsius to Fahrenheit.
- Slides: 20