Your SlideShare is downloading. ×
0
Swing jecrc
Swing jecrc
Swing jecrc
Swing jecrc
Swing jecrc
Swing jecrc
Swing jecrc
Swing jecrc
Swing jecrc
Swing jecrc
Swing jecrc
Swing jecrc
Swing jecrc
Swing jecrc
Swing jecrc
Swing jecrc
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Swing jecrc

340

Published on

swing presentation prepared by jecrc, jodhpur

swing presentation prepared by jecrc, jodhpur

Published in: Technology, Art & Photos
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
340
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Swing- Introduction <ul><li>Swing provides three generally useful top-level container classes: JFrame , JDialog , and JApplet . </li></ul><ul><li>Every GUI component must be part of a containment hierarchy . A containment hierarchy is a tree of components that has a top-level container as its root. </li></ul><ul><li>Each GUI component can be contained only once. If a component is already in a container and you try to add it to another container, the component will be removed from the first container and then added to the second. </li></ul>
  • 2. Swing- Introduction <ul><li>Each top-level container has a content pane that, generally speaking, contains (directly or indirectly) the visible components in that top-level container&apos;s GUI. </li></ul><ul><li>You can optionally add a menu bar to a top-level container. The menu bar is by convention positioned within the top-level container, but outside the content pane. </li></ul>
  • 3. Swing Introduction <ul><li>Adding Components to the Content Pane </li></ul><ul><li>frame.getContentPane().add(yellowLabel, BorderLayout.CENTER); </li></ul><ul><li>The default content pane is a simple intermediate container that inherits from JComponent, and that uses a BorderLayout as its layout manager. </li></ul><ul><li>If you create your own content pane, make sure it&apos;s opaque. An opaque JPanel object makes a good content pane. Note that the default layout manager for JPanel is FlowLayout. </li></ul><ul><li>Create a panel and add components to it. </li></ul><ul><li>JPanel contentPane = new JPanel(new BorderLayout()); contentPane.setBorder( someBorder ); </li></ul><ul><li>contentPane.add( someComponent , BorderLayout.CENTER); contentPane.add( anotherComponent , BorderLayout.PAGE_END); //Make it the content pane. contentPane.setOpaque(true); topLevelContainer .setContentPane(contentPane); </li></ul>
  • 4. Swing- Introduction <ul><li>Adding a Menu Bar </li></ul><ul><li>To add a menu bar to a top-level container, you create a JMenuBar object, populate it with menus, and then call setJMenuBar. </li></ul><ul><li>frame.setJMenuBar(cyanMenuBar); </li></ul><ul><li>The Root Pane </li></ul><ul><li>Each top-level container relies on a reclusive intermediate container called the root pane . The root pane manages the content pane and the menu bar, along with a couple of other containers . </li></ul><ul><li>The two other components that a root pane adds are a layered pane and a glass pane. The layered pane directly contains the menu bar and content pane, and enables Z-ordering of other components you might add. </li></ul>
  • 5. Swing – Component Models <ul><li>Most noncontainer Swing components have models. A button (JButton), for example, has a model (a ButtonModel object) that stores the button&apos;s state — what its keyboard mnemonic is, whether it&apos;s enabled, selected, or pressed, and so on. </li></ul><ul><li>Some components have multiple models. A list (JList), for example, uses a ListModel to hold the list&apos;s contents, and a ListSelectionModel to track the list&apos;s current selection. </li></ul><ul><li>You often don&apos;t need to know about the models that a component uses. </li></ul><ul><li>Why then do models exist? </li></ul><ul><li>The biggest reason is that they give you flexibility in determining how data is stored and retrieved. For example, if you&apos;re designing a spreadsheet application that displays data in a sparsely populated table, you can create your own table model that is optimized for such use. </li></ul>
  • 6. Swing - MVC Model <ul><li>Although Swing&apos;s model architecture is sometimes referred to as a Model-View-Controller (MVC) design, it really isn&apos;t. Swing components are generally implemented so that the view and controller are indivisible, implemented by a single UI object provided by the look and feel. The Swing model architecture is more accurately described as a separable model architecture . </li></ul>
  • 7. Swing – JComponent Class <ul><li>With the exception of top-level containers, all Swing components whose names begin with &amp;quot;J&amp;quot; descend from the JComponent class. For example, JPanel, JScrollPane, JButton, and JTable all inherit from JComponent. However, JFrame and JDialog don&apos;t because they implement top-level containers. </li></ul><ul><li>The JComponent class extends the Container class, which itself extends Component . The Component class includes everything from providing layout hints to supporting painting and events. The Container class has support for adding components to the container and laying them out. </li></ul>
  • 8. Swing – Text Component <ul><li>JTextComponent is the foundation for Swing&apos;s text components, and provides these customizable features for all of its descendants: </li></ul><ul><li>A model, known as a document , to manage the component&apos;s content. </li></ul><ul><li>A view, which is in charge of displaying the component on screen. </li></ul><ul><li>A controller, known as an editor kit , that can read and write text and that implements editing capabilities with actions . </li></ul><ul><li>Support for infinite undo and redo. </li></ul><ul><li>Pluggable caret and support for caret change listeners and navigation filters. </li></ul>
  • 9. Swing – Text Component <ul><li>Associating Text Actions with Menus and Buttons </li></ul><ul><li>All Swing text components supports standard editing commands such as cut, copy, paste, and inserting characters. Each editing command is represented and implemented by an Action object. </li></ul><ul><li>textComponent..getActions() returns all the supported by this particular text component. </li></ul><ul><li>Steps to use actions: </li></ul><ul><li>Build an hashmap of all the actions with action name as key to stored action object. </li></ul><ul><li>Make a method getActionByName that action name returns action object. </li></ul><ul><li>Now to make a cut menu items write following code: </li></ul><ul><li>JMenu menu = new JMenu(&amp;quot;Edit&amp;quot;); menu.add(getActionByName(DefaultEditorKit.cutAction)); </li></ul><ul><li>DefaultEditorkit kit provides actions for basic text editing and is the superclass for all the editor kits provided by Swing </li></ul>
  • 10. Swing – Text Component <ul><li>Associating Text Actions with Menus and Buttons </li></ul><ul><li>code that creates the Style menu and puts the Bold menu item in it: </li></ul><ul><li>protected JMenu createStyleMenu() </li></ul><ul><li>{ </li></ul><ul><li>JMenu menu = new JMenu(&amp;quot;Style&amp;quot;); </li></ul><ul><li>Action action = new StyledEditorKit.BoldAction(); action.putValue(Action.NAME, &amp;quot;Bold&amp;quot;); </li></ul><ul><li>menu.add(action); </li></ul><ul><li>. . . </li></ul><ul><li>The StyledEditorKit provides Action subclasses to implement editing commands for styled text. </li></ul>
  • 11. Swing – Text Component <ul><li>Associating Text Actions with key strokes </li></ul><ul><li>In addition to associating an action with a GUI component, you can also associate an action with a key stroke, using a text component&apos;s input map. </li></ul><ul><li>following code adds the CTRL-B key binding to the text pane. </li></ul><ul><li>InputMap inputMap = textPane.getInputMap(); KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_B, Event.CTRL_MASK); inputMap.put(key, DefaultEditorKit.backwardAction); </li></ul>
  • 12. Swing – Text Component <ul><li>Implementing Undo and Redo </li></ul><ul><li>Implementing undo and redo has two parts: </li></ul><ul><li>Remembering undoable edits . </li></ul><ul><li>Implementing the undo and redo commands and providing a user interface for them. </li></ul><ul><li>Remembering Undoable Edits </li></ul><ul><li>To support undo and redo, a text component must remember each edit that occurs, the order of edits, and what it takes to undo each edit. The example program uses an instance of the UndoManager class to manage its list of undoable edits. </li></ul><ul><li>protected UndoManager undo = new UndoManager(); </li></ul><ul><li>A document notifies interested listeners whenever an undoable edit occurs on its content. </li></ul><ul><li>lsd.addUndoableEditListener(new MyUndoableEditListener()); </li></ul><ul><li>The undoable edit listener used adds the edit to the undo manager&apos;s list: </li></ul>
  • 13. Swing – Text Component <ul><li>Implementing Undo and Redo </li></ul><ul><li>Part2: Implementing Undo and Redo commands </li></ul><ul><li>JMenu menu = new JMenu(&amp;quot;Edit&amp;quot;); </li></ul><ul><li> //Undo and redo are actions of our own creation </li></ul><ul><ul><li>undoAction = new UndoAction(); </li></ul></ul><ul><ul><li> menu.add(undoAction); </li></ul></ul><ul><ul><li> redoAction = new RedoAction(); </li></ul></ul><ul><ul><li> menu.add(redoAction); </li></ul></ul><ul><ul><li> ... </li></ul></ul>
  • 14. Swing – Text Component <ul><li>Implementing Undo and Redo </li></ul><ul><li>Part2: Implementing Undo and Redo commands </li></ul><ul><ul><li>The undo and redo actions are implemented by custom AbstractAction subclasses: UndoAction and RedoAction, respectively. </li></ul></ul><ul><ul><li>When the user invokes the Undo command, UndoAction&apos;s actionPerformed method, shown here, gets called: </li></ul></ul><ul><ul><li>public void actionPerformed(ActionEvent e) { </li></ul></ul><ul><ul><ul><li>try { undo.undo(); } </li></ul></ul></ul><ul><ul><ul><li>catch (CannotUndoException ex) </li></ul></ul></ul><ul><ul><ul><li>{ System.out.println(&amp;quot;Unable to undo: &amp;quot; + ex); ex.printStackTrace(); } </li></ul></ul></ul><ul><ul><ul><li>updateUndoState(); </li></ul></ul></ul><ul><ul><ul><li>redoAction.updateRedoState(); } </li></ul></ul></ul><ul><ul><ul><li>This method calls the undo manager&apos;s undo method and updates the menu items to reflect the new undo/redo state. </li></ul></ul></ul>
  • 15. Swing – Text Component <ul><li>About Document Object </li></ul><ul><li>A text component&apos;s model is known as a document and is an instance of a class that implements the Document interface. A document provides these services for a text component: </li></ul><ul><li>Contains the text. A document stores the textual content in Element objects, which can represent any logical text structure, such as paragraphs, text runs that share styles, and so on. We do not cover Elements. However, The Swing Connection has at least one article on the subject. </li></ul><ul><li>Provides support for editing the text through the remove and insertString methods. </li></ul><ul><li>Notifies document listeners and undoable edit listeners of changes to the text. </li></ul><ul><li>Manages Position objects, which track a particular location within the text even as the text is modified. </li></ul><ul><li>Allows you to get information about the text, such as its length, and segments of the text as a string. </li></ul>
  • 16. Swing – Text Component <ul><li>About Document Object </li></ul><ul><li>Notifies document listeners and undoable edit listeners of changes to the text. </li></ul><ul><li>Manages Position objects, which track a particular location within the text even as the text is modified. </li></ul><ul><li>Allows you to get information about the text, such as its length, and segments of the text as a string. </li></ul>

×