GUI Swing Class Hierarchy Swing Components Swing Conatiners





















![Exceptions Example of our own Exception --- throw/throws int min. Element(int[] numbers) throws Empty. Exceptions Example of our own Exception --- throw/throws int min. Element(int[] numbers) throws Empty.](https://slidetodoc.com/presentation_image_h2/16b401682551a6916a596459669a6d08/image-22.jpg)
- Slides: 22

GUI

Swing Class Hierarchy

Swing Components

Swing Conatiners JFrame – top-level window to store components

Swing Conatiners JPanel – container; can be embedded in JFrame

Layouts Flow. Layout arranges elements in a row elements centered by default within container Grid. Layout subdivides container into cells of identical sizes components take up all available space of a cell Border. Layout subdivides container into 5 areas: N, S, E, W, Center

Layouts 3 x 3 Grid. Layout 4 x 1 Grid. Layout Border. Layout Flow. Layout used to place the 3 panels in the Jframe.

Grid Layout 3 x 3 Grid. Layout 4 x 1 Grid. Layout layout = new Grid. Layout(3, 3); panel. set. Layout(layout); Grid. Layout Border. Layout button 1 = new JButton( "1" ); panel. add( button 1 ); button 2 = new JButton( "2" ); panel. add( button 2 ); button 3 = new JButton( "3" ); panel. add( button 3 ); button 4 = new JButton( "4" ); panel. add( button 4 ); . . . . Components are stretched to occupy the whole panel area.

Border Layout Border. Layout layout = new Border. Layout(); panel. set. Layout(layout); 3 x 3 Grid. Layout 4 x 1 Grid. Layout button 1 = new JButton( "North" ); panel. add( button 1, Border. Layout. NORTH ); button 2 = new JButton( "South" ); panel. add( button 2, Border. Layout. SOUTH ); button 3 = new JButton( "East" ); panel. add( button 3, Border. Layout. EAST ); button 4 = new JButton( "West" ); panel. add( button 4, Border. Layout. WEST ); button 5 = new JButton( "Center" ); panel. add( button 5, Border. Layout. CENTER ); Center area gets most of the space. The other areas are given only as much as they need. Not all areas need to be occupied. Border. Layout

Listeners Process events from components, containers Action. Listener (JButton, Timer, JCombo. Box) Change. Listener (JSlider) Mouse. Listener, Mouse. Motion. Listener (JPanel, JFrame) Listeners are interfaces; must implement ALL specified methods Action. Listener: void action. Performed(Action. Event e) Change. Listener: void state. Changed(Change. Event e) Mouse. Listener: void mouse. Clicked(Mouse. Event e) void mouse. Pressed(Mouse. Event e) void mouse. Released(Mouse. Event e) void mouse. Entered(Mouse. Event e) void mouse. Exited(Mouse. Event e) Mouse. Motion. Listener: void mouse. Moved(Mouse. Event e) void mouse. Dragged(Mouse. Event e)

Adapter classes Convenience classes server as intermediaries between the available interfaces and the user-defined (listener) classes that implement the interfaces make it possible to implement only the methods of interest

Adapter classes Convenience classes server as intermediaries between the available interfaces and the user-defined (listener) classes that implement the interfaces make it possible to implement only the methods of interest abstract class Mouse. Adapter implements Mouse. Listener, Mouse. Motion. Listener { } void mouse. Pressed(Mouse. Event e) { // empty body } void mouse. Released(Mouse. Event e) void mouse. Entered(Mouse. Event e) void mouse. Exited(Mouse. Event e) { // empty body } void mouse. Moved(Mouse. Event e) { // empty body } void mouse. Dragged(Mouse. Event e) { // empty body } Mouse. Listener methods Mouse. Motion. Listener methods

Adapter classes Convenience classes server as intermediaries between the available interfaces and the user-defined (listener) classes that implement the interfaces make it possible to implement only the methods of interest abstract class Mouse. Adapter implements Mouse. Listener, Mouse. Motion. Listener { } abstract void mouse. Pressed(Mouse. Event e); abstract void mouse. Released(Mouse. Event e); abstract void mouse. Entered(Mouse. Event e); abstract void mouse. Exited(Mouse. Event e); abstract void mouse. Moved(Mouse. Event e); asbtract void mouse. Dragged(Mouse. Event e);

Adapter classes Convenience classes server as intermediaries between the available interfaces and the user-defined (listener) classes that implement the interfaces make it possible to implement only the methods of interest abstract class Mouse. Adapter implements Mouse. Listener, Mouse. Motion. Listener { ………………… } class Line. Listener extends Mouse. Adapter { … implement only the methods of interest … }

File IO

Serialization Mechanism for making exact copies of objects For simple classes enough to declare implements Serializable Application – saving / reading actual objects from a file

Saving with Serialization Object. Output. Stream and method write. Object(obj) try { File. Output. Stream file = new File. Output. Stream("pets. ser"); Object. Output. Stream output = new Object. Output. Stream(file); output. write(pets. List. size()); for (Pet pet : pets. List) { output. write. Object(pet); } output. close(); } catch (Exception e) { System. out. println("Could not write to file. " + e); }

Reading with Serialization Object. Input. Stream and method read. Object() try { File. Input. Stream file = new File. Input. Stream("pets. ser"); Object. Input. Stream output = new Object. Input. Stream(file); int count = input. read. Int(); for (int i = 0; i < count; i++) { Pet pet = input. read. Object(); } output. close(); } catch (Exception e) { System. out. println("Could not read from file. " + e); }

Exceptions

Exceptions Mechanism for handling unexpected RUN-TIME conditions (errors) Force the programmer to handle error conditions Allow for separating the logic of the code from error-handling Sometimes no other option to report the value: constructor min. Element, max. Element Example – see File. IO

Exceptions Can create our own type of exception (should inherit from Exception) class Empty. Array. Exception extends Exception { public void Empty. Array. Exception() { super(); } public void Empty. Array. Exception(String message) { super(message); } }
![Exceptions Example of our own Exception throwthrows int min Elementint numbers throws Empty Exceptions Example of our own Exception --- throw/throws int min. Element(int[] numbers) throws Empty.](https://slidetodoc.com/presentation_image_h2/16b401682551a6916a596459669a6d08/image-22.jpg)
Exceptions Example of our own Exception --- throw/throws int min. Element(int[] numbers) throws Empty. Array. Exception { // empty array --- throw an exception if (numbers. length == 0) { throw Empty. Array. Exception(“Empty array given”); } // //. . . compute smallest element. . . // }