140 likes | 257 Views
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 2D API
E N D
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 2D 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 DesignModel/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 ClickEvent extends JFrame implements ActionListener { private JButton click = new JButton("Click Me"); private JTextField welcomeText = new JTextField(); private JPanel thePanel = new JPanel(); public ClickEvent() { super("Event Example"); setSize(400,300); thePanel.setLayout(new GridLayout(2,1)); //this.getContentPane().add(click,BorderLayout.NORTH); thePanel.add(click); thePanel.add(welcomeText); this.getContentPane().add(thePanel,BorderLayout.CENTER); click.addActionListener(this); this.setVisible(true); }
Taking Turns on a Swing Component(Event Handling)Example public void actionPerformed(ActionEvent e) { welcomeText.setHorizontalAlignment(JTextField.CENTER); welcomeText.setText("Hello Linux User's Group"); } public static void main(String[] args) { ClickEvent clickEvent1 = new ClickEvent(); } }
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: • TreeModel - how to work with the data • TreeSelectionModel - how to select nodes within the tree
JTree ComponentContinued • Node Interface: TreeNode, The basic piece of the tree • Contains the basic methods needed by the TreeModel 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 • TreeExpansionEvent, When a node has been expanded or collapsed • TreeSelectionEvent, 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 ComponentContinued • Two Interfaces: • TableColumnModel manages selection in the column and spacing of the column in the table. • TableModel 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 ComponentContinued • Events • TableColumnEvent used to notify that a column model has changed. For example, that a column has been added, removed, or moved. • TableModelEvent used to notify that the TableModel 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.