Swinging in Your Java Playground Background Swing is
Swinging in Your Java Playground
Background • Swing is a part of the Java Foundation Classes (JFC). • The JFC is made up of features intended to give a programmer all the tools necessary to build enterprise applications. – Swing Components – Pluggable Look and Feel – Accessibility API – Java 2 D API – Drag and Drop Support
Swing Features • Built on top of AWT 1. 1, taking your 1995 school playground into 2000 and beyond • New components exist in Swing – Tables, Trees, Sliders, Progress Bars, Internal Frames, and Text Components. • Tooltips can appear over GUI Components • Bind Keyboard events to GUI Components • Easily extendible to create Customized Components
Swing Design Model/View/Controller Architecture • Model - The state data for a component. For example, a Choice would contain all the items that could be chosen. This data remains constant no matter what it’s representation is on the screen. • View - The way a component actually appears when it is drawn on the screen • Controller - The way a component interacts with the events that occur.
Taking Turns on a Swing Component (Event Handling) • When a component receives input, it fires events. An Object can register to receive those events by being added as a Listener to the Component. • A Component can have multiple objects listening for its events. • When the listening Object handles an event, it can do something to the Component or something else entirely.
Taking Turns on a Swing Component (Event Handling) Example public class Click. Event extends JFrame implements Action. Listener { private JButton click = new JButton("Click Me"); private JText. Field welcome. Text = new JText. Field(); private JPanel the. Panel = new JPanel(); public Click. Event() { super("Event Example"); set. Size(400, 300); the. Panel. set. Layout(new Grid. Layout(2, 1)); //this. get. Content. Pane(). add(click, Border. Layout. NORTH); the. Panel. add(click); the. Panel. add(welcome. Text); this. get. Content. Pane(). add(the. Panel, Border. Layout. CENTER); click. add. Action. Listener(this); this. set. Visible(true); }
Taking Turns on a Swing Component (Event Handling) Example public void action. Performed(Action. Event e) { welcome. Text. set. Horizontal. Alignment(JText. Field. CENTER); welcome. Text. set. Text("Hello Linux User's Group"); } public static void main(String[] args) { Click. Event click. Event 1 = new Click. Event(); } }
JTree Component • The visual representation of data that is organized in tree format – Root, there is only 1 of these for a tree – Node, any entry in the tree – Leaf, any node without a child node • Two Interfaces: – Tree. Model - how to work with the data – Tree. Selection. Model - how to select nodes within the tree
JTree Component Continued • Node Interface: Tree. Node, The basic piece of the tree – Contains the basic methods needed by the Tree. Model to manipulate the data • All of the interfaces have Default implementations so that you don’t have to start from scratch
JTree Component Continued • Events – Tree. Expansion. Event, When a node has been expanded or collapsed – Tree. Selection. Event, A row (path) has been selected or, if multiple rows can be selected, a row has been added or removed from that group selection.
JTable Component • The visual representation of data in a table format. It is very useful to display data from databases in this format. – Column is the basic unit of the Table. The data represented in it is consistent – Row the collection of columns that uniquely identifies an entity. – Cell the location at a particular row and column of data.
JTable Component Continued • Two Interfaces: – Table. Column. Model manages selection in the column and spacing of the column in the table. – Table. Model provides all the information concerning the rows and columns in the table as well as access to the data found in the cells. • Again, both interfaces have Default implementations.
JTable Component Continued • Events – Table. Column. Event used to notify that a column model has changed. For example, that a column has been added, removed, or moved. – Table. Model. Event used to notify that the Table. Model has changed and/or that rows/column data has changed
Other Details • Components are lightweight • Swing runs on any JDK 1. 1. 5 or higher does not need to be downloaded for JDK 1. 2; it is part of the core JDK. • Java. swing. border package which contain 8 predefined classes for borders that can be placed around a component. • Lots of classes exist to provide editing within the complex components.
- Slides: 14