1 / 14

Swinging in Your Java Playground

Explore Swing fundamentals, design principles, event handling, Swing components like JTree and JTable, and customization techniques. Learn to build robust Java GUI applications effortlessly.

alfredv
Download Presentation

Swinging in Your Java Playground

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Swinging in Your Java Playground

  2. 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

  3. 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

  4. 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.

  5. 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.

  6. 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); }

  7. 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(); } }

  8. 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

  9. 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

  10. 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.

  11. 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.

  12. 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.

  13. 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

  14. 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.

More Related