• No results found

Advance Swing

N/A
N/A
Protected

Academic year: 2022

Share "Advance Swing"

Copied!
64
0
0

Loading.... (view fulltext now)

Full text

(1)Advance Swing.

(2) Swing ●. ● ● ●. Swing is a set of classes that provides more powerful and flexible components than are possible with AWT. Unlike AWT components, Swing components are not implemented by platform specific code. Instead they are written entirely in Java and, therefore, are platform-independent. The term Lightweight is used to describe such elements..

(3) ● ● ● ● ●. ●. 1.Swing is also called as JFC’s (Java Foundation classes) and AWT stands for Abstract windows toolkit. 2.AWT components are called Heavyweight component and Swings are called light weight component because swing components sits on the top of AWT components and do the work. 3. Swing components require javax.swing package where as AWT components require java.awt package . 4.swings components are made in purely java and they are platform independent whereas AWT components are platform dependent. 5.we can have different look and feel in Swing whereas this feature is not supported in AWT. 6.Swing has many advanced features like JTabel, Jtabbed pane which is not available in AWT. Also. Swing components are called "lightweight" because they do not require a native OS object to implement their functionality. JDialog and JFrame are heavyweight, because they do have a peer. So components like JButton, JTextArea, etc., are lightweight because they do not have an OS peer. 7.With AWT, you have 21 "peers" (one for each control and one for the dialog itself). A "peer" is a widget provided by the operating system, such as a button object or an entry field object..

(4) 8.With Swing, you would have only one peer, the operating system's window object. All of the buttons, entry fields, etc. are drawn by the Swing package on the drawing surface provided by the window object. This is the reason that Swing has more code. It has to draw the button or other control and implement its behavior instead of relying on the host operating system to perform those functions. ● 9.Several consequences result from this difference between AWT and Swing. AWT is a thin layer of code on top of the OS, whereas Swing is much larger. Swing also has very much richer functionality. 10.Using AWT, you have to implement a lot of things yourself, while Swing has them built in. For GUI-intensive work, AWT feels very primitive to work with compared to Swing. Because Swing implements GUI functionality itself rather than relying on the host OS, it can offer a richer environment on all platforms Java runs on. ●.

(5) Swing components classes ● ● ● ● ● ● ●. AbstractButton ButtonGroup ImageIcon JApplet JButton JCheckBox JComboBox.

(6) ● ● ● ● ● ● ●. JLabel JRadioButton JScrollPane JTabbedPane JTable JTextField JTree.

(7) Advanced Swing ● ● ● ● ● ●. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers.

(8) Swing has a few simple defaults for displaying values in lists, trees, and tables. ● In a JList, values that are Icons are drawn directly, while other objects are converted to strings via their toString method, then displayed via a JLabel. ●.

(9) ●. ●. However, Swing also lets you define arbitrary mappings between values and display components, yielding whatever visual representation you want for values of specified types. This is done by building a "cell renderer" that takes the containing component, the value of the entry, a few state parameters that say things like whether or not the value is currently selected, and then returns a Component that is used to draw the value..

(10) Lists ●. JList Component ●. ●. The JList component is similar to a set of checkboxes or radio buttons, except that the items are placed inside a single box and are selected by clicking on the items themselves, not on buttons. If you permit multiple selection for a list box, the user can select any combination of the items in the box..

(11) Create JList String words[]={"Ajava","OOSD","DOS","Ajava Lab","NSM"}; ● JList wordList=new JList(words); ● For scrolling JScrollPane scrollPane=new JScrollPane(wordList); ● Add scrollpane to the panel ●.

(12) By default, the list components displays eight items; use the setVisibleRowCount(); ● wordList.setVisibleRowCount(3); ●.

(13) By default, a user can select multiple items. ● You can restrict the user to a more limited selection mode with a setSelectedMode() ● wordList.setSelectionMode(ListSelectionMo del.SINGLE_SELECTION); ●.

(14) Event handling For handling events you need to listen to list selection events instead of action events. ● public void valueChanged(ListSelectionEvent evt) ●.

(15) ● ● ● ● ● ● ● ●. Once event has happened, we need to find out what items are currently selected. The getSelectedValues() method returns an array of objects contaning all selected items. Cast each array element to a string: Object values[]=wordList.getSelectedValues(); For(Object value:values) { Do something with item }.

(16) If your list does not allow multiple selections, you can call the getSelectedValue. It returns the first selected value. ● String value=(String)wordList.getSelectedValue() ●.

(17) • Build JList – Have existing data implement ListModel interface • getElementAt – Given an index, returns data element • getSize – Tells JList how many entries are in list • addListDataListener – Lets user add listeners that should be notified when an item is selected or deselected. • removeListDataListener – Pass model to JList constructor • Set visible rows & handle events: as before • Add/remove items: use the model.

(18) JList has the following useful properties: • listData: A write-only property used to set an array or a vector of objects for the list. • fixedCellHeight: The height of a list cell. All the cells in a list have the same height. If this property is not specified, the tallest item in the list dictates the cell height for all the cells..

(19) • fixedCellWidth: The width of a list cell. All the cells in a list have the same width. If this property is not specified, the widest item in the list dictates the cell width for all the cells. • model: An object that maintains the data for the list. List models must be used explicitly when adding or deleting items in a list. List models are introduced in the section "List Models." • prototypeCellValue: An object whose cell size dictates the cell size of the list..

(20) • selectedIndex: An int value indicating the index of the selected item in the list. • selectedIndices: An array of int values representing the indices of the selected items in the list. • selectedValue: The first selected value in the list. • selectedValues: An array of objects representing selected values in the list. This property is read-only..

(21) • selectionBackground: The background color for selected cells. • selectionForeground: The foreground color for selected cells. • selectionMode: One of the three values (SINGLE_SELECTION, SINGLE_INTERVAL_SELECTION, MULTIPLE_INTERVAL_SELECTION) that indicate whether single items, single-interval items, or multiple-interval items can be selected..

(22) • selectionModel: An object that tracks list selection. List-selection models are rarely used explicitly. List-selection models are discussed in the section "List-Selection Models." • visibleRowCount: The preferred number of rows in the list that can be displayed without a scroll bar. The default value is 8..

(23) JTable ●. JTable is a Swing component that displays data in rows and columns in a two-dimensional grid..

(24) JTable doesn't directly support scrolling. ● To create a scrollable table, you need to create a JScrollPane and add an instance of JTable to the scroll pane. If a table is not placed in a scroll pane, its column header will not be visible, because the column header is placed in the header of the view port of a Scroll pane. ●.

(25) ●. ● ● ●. JTable has three different models: a table model, a column model, and a list-selection model. The table model is for storing and processing data. The column model deals with column management. The list-selection model is the same used by JList for selecting rows, columns and cells in a table..

(26) ●. Like JList, JTable has editors. JTable has many features that make it possible to customize its rendering and editing. JTable also provides many convenient easy-to-use renderers and editors..

(27) There are seven constructors in the JTable class. ● The builder tool always uses the default constructor to create an instance of JTable. ●.

(28) JTable Properties JTable is a powerful control with a variety of properties and methods that provide many ways to customize tables. The following properties are often used: • autoCreateColumnsFromModel: A boolean value indicating whether the columns are created in the table. The default value is true. • autoResizngMode: You can always resize table columns, but not the rows. This property specifies how columns are resized. Possible values are: JTable.AUTO_RESIZE_OFF JTable.AUTO_RESIZE_LAST_COLUMN JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS JTable.AUTO_RESIZE_NEXT_COLUMN JTable.AUTO_RESIZE_ALL_COLUMNS.

(29) • cellSelectionEnabled: A boolean value specifying whether individual cells can be selected. • columnModel: An object that maintains the table column data. Table column models are introduced in the section "Table Column Models." • columnSelectionAllowed: A boolean value specifying whether column selection is allowed. • defaultEditor: The default editor for table cells. JTable provides a set of default editors that can be overridden, if desired. The editors are introduced in the section "Table Renderers and Editors.".

(30) • defaultRenderer: The default renderer for table cells. JTable provides a set of default renderers that can be overridden, if desired. • editingColumn: The column of the cell that is currently being edited. • editingRow: The row of the cell that is currently being edited. • gridColor: The color used to draw grid lines..

(31) • intercellSpacing: A dimension that represents the horizontal and vertical margins between cells. • model: An object that maintains the table data. • rowHeight: Row height of the table. The default value is 16 pixels. • rowMargin: The vertical margin between rows. • rowCount: The number of rows in a table..

(32) • rowSelectionAllowed: A boolean value specifying whether the rows can be selected. • selectionBackground: The background color of selected cells. • selectionForeground: The foreground color of selected cells. • selectionMode: Specifying how table cells can be selected. This property is write-only. The possible values are: ListSelectionModel.SINGLE_SELECTION ListSelectionModel.SINGLE_INTERVAL_SELECTION ListSelectionModel.MULTIPLE_INTERVAL_SELECTION.

(33) • selectionModel: An object that tracks the selection of table cells. Table selection model is the same as the list selection model. • showGrid: A boolean value indicating whether the grid lines are displayed. The defualt value is true. • showHorizontalGrid: A boolean value indicating whether the horizontal grid lines are displayed. The default value is true. • showVerticalGrid: A boolean value indicating whether the vertical grid lines are displayed. The default value is true. • tableHeader: An instance of JTableHeader that is displayed in the header of the view port in a scroll pane. Table header is usually specified as an array of strings in the table model..

(34) Table Models Like JList, JTable delegates data storing and processing to its table data model. ● A table data model must implement the TableModel interface, which defines the methods for registering table model listener, for manipulating cells, and for obtaining row count, column count, column class, and column name. ●.

(35) ●. ●. ● ● ●. The AbstractTableModel class provides default implementations for most of the methods in TableModel. It takes care of the management of listeners and provides some conveniences for generating TableModelEvents and dispatching them to the listeners. To create a concrete TableModel, you can simply extend AbstractTableModel and implement the following three methods at least: public int getRowCount(); public int getColumnCount(); public Object getValueAt(int row, int column);.

(36) ●. The DefaultTableModel class extends AbstractTableModel and implements these three methods. Additionally, DefaultTableModel provides concrete storage for data. The data are stored in a vector. Each element in the vector is an array of objects, each of which represents an individual cell value..

(37) JTree ●. JTree is a Swing component that displays data in a treelike hierarchy..

(38)

(39) ●. ●. All the nodes displayed in the tree are in the form of a hierarchical indexed list. Tree can be used to navigate structured data with hierarchical relationships. The JBuilder navigation pane, structure pane, and component tree are examples of displaying items as nodes in a treelike structure. A node can have child nodes. A node is called a leaf if it has no children; a node with no parent is called the root of its tree. A tree may consist of many subtrees, each node acting as the root for its own subtree..

(40) ●. ●. A nonleaf node can be expanded or collapsed by double-clicking on the node or on the node's handle in front of the node. The handle usually has a visible sign to indicate whether the node is expanded or collapsed. For example, on Windows, the + symbol indicates that the node is collapsed and the - symbol indicates that it is expanded. Like JTable, JTree is a very complex component with many supporting interfaces and classes. JTree is in the javax.swing package, but its supporting classes are all included in the javax.swing.tree package. These supporting classes are DefaultMutableTreeNode, DefaultTreeModel, TreeSelectionModel, DefaultTreeCellEditor, DefaultTreeCellRenderer, and TreePath..

(41) ●. While JTree displays the tree, the data representation of the tree is handled by the DefaultMutableTreeNode class, which provides operations for creating nodes, for examining and modifying a node's parent and children, and also for examining the tree to which the node belongs. While the DefaultMutableTreeNode represents a node, the DefaultTreeModel represents the entire tree. Unlike the ListModel or TableModel, the tree model does not directly store or manage tree data. Tree data are stored and managed by the DefaultMutableTreeNode class. To create a tree model, you first create an instance of DefaultMutableTreeNode to represent the root of the tree, and then create an instance of DefaultTreeModel fitted with the root..

(42) ● ●. ●. The TreeSelectionModel class handles tree node selection. The DefaultTreeCellRenderer class provides a default tree node renderer that can display a label and/or an icon in a node. The DefaultTreeCellEditor can be used to edit the cells in a text field. The TreePath class is a support class that represents a set of nodes in a path..

(43) ●. JTree has the following useful properties: ●. cellEditor: An object that is used to edit the cell in a tree node. By default, an instance of DefaultCellEditor is used to edit the cells in a text field.. ●. cellRenderer: An object that is used to render the cells in a tree node. By default, an instance of DefaultTreeCellRenderer is used to display nodes in a tree, which can display strings or icons..

(44) • editable: A boolean value indicating whether the cell is editable. The default value is false. • model: An instance of TreeModel. • rootVisible: A boolean value indicating whether the root is displayed in the tree. The default value is true. • rowHeight: The height of the row for the node displayed in the tree. The default height is 16 pixels. • selectionModel: An instance of TreeSelectionModel..

(45) ● ●. ●. Creating Trees JTree doesn't directly support scrolling. To create a scrollable tree, you need to create a JScrollPane and add an instance of JTree to the scroll pane. There are several constructors that can be used to create a JTree. You can create a JTree with an array of objects, a vector, or a hash table. The builder tool always uses the default constructor to create an instance of JTree with a default tree model..

(46) Styled Text Components JTextField and JTextArea classes are used to take input from the user ● JEditorPane ●. ●. that displays and edits text in HTML and RTF format..

(47) ●. The subclass JTextPane of JEditorPane can hold styled text with special fonts and text formats, as well as embedded components..

(48) If you click on the Editable check box, then the editor pane becomes editable. You can type in text and use the BACKSPACE key to delete text. ● The component also understands the CTRL+X, CTRL+C, and CTRL+V shortcuts for cut, copy, and paste. ●.

(49) When the component is editable, then hyperlinks are not active. ● Also, with some web pages you can see JavaScript commands, comments, and other tags when edit mode is turned on. ●.

(50) ●. By default, the JEditorPane is in edit mode. You should call editorPane.setEditable(false) to turn it off..

(51) You use the setPage method to load a new document. ● The parameter is either a string or a URL object. ● The JEditorPane class extends the JTextComponent class. Therefore, you can call the setText method as well—it simply displays plain text. ●.

(52) To listen to hyperlink clicks, you add a HyperlinkListener. ● The HyperlinkListener interface has a single method, hyperlinkUpdate, that is called when the user moves over or clicks on a link. ● The method has a parameter of type HyperlinkEvent.. ●.

(53) ●. ● ● ● ●. ●. You need to call the getEventType method to find out what kind of event occurred. There are three possible return values: HyperlinkEvent.EventType.ACTIVATED HyperlinkEvent.EventType.ENTERED HyperlinkEvent.EventType.EXITED The first value indicates that the user clicked on the hyperlink. In that case, you typically want to open the new link. You can use the second and third values to give some visual feedback, such as a tooltip, when the mouse hovers over the link..

(54) ●. The getURL method of the HyperlinkEvent class returns the URL of the hyperlink..

(55) Component Organizers These include the split pane, a mechanism for splitting an area into multiple parts whose boundaries can be adjusted, ● the tabbed pane, which uses tab dividers to allow a user to flip through multiple panels, and ● the desktop pane, which can be used to implement applications that display multiple internal frames. ●.

(56) Split Pane ●. Split panes are used to split a component into two parts, with an adjustable boundary in between..

(57) Tabbed Pane Tabbed panes are a familiar user interface device to break up a complex dialog in to subsets of related options. ● You can also use tabs to let a user flip through a set of documents or images. ●.

(58)

(59) ● ● ● ●. To create a tabbed pane, you first construct a JTabbedPane object, then you add tabs to it. JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.addTab(title, component); The last parameter of the addTab method has type Component. If you want to add multiple components into the same tab, you first pack them up in a container, such as a JPanel..

(60) Desktop Panes and Internal Frames Many applications present information in multiple windows that are all contained inside a large frame. ● If you minimize the application frame, all of its windows are hidden at the same time. ● In the Windows environment, this user interface is sometimes called the multiple document interface or MDI. ●.

(61) Cascading ● Tiling ● Outline dragging ●.

(62) Progress Indicators ●. A user-friendly program provides some indication to the user that the task is occurring, how long the task might take, and how much work has already been done. One way of indicating work, and perhaps the amount of progress, is to use an animated image..

(63) Use a progress bar if: You want more control over the configuration of the progress bar. If you are working directly with a progress bar, you can set it to be indeterminate, make it display vertically, provide a string for it to display, register change listeners on it, and provide it with a bounded range model to control the progress bar's minimum, maximum, and current values. ● The program needs to display other components along with the progress bar. ● You need more than one progress bar. With some tasks, you need to monitor more than one parameter. For example, an installation program might monitor disk space usage in addition to how many files have been successfully installed. ● You need to reuse the progress bar. A progress bar can be reused; a progress monitor cannot. Once the progress monitor has decided to display a dialog (or not), the progress monitor cannot do it again. ●.

(64) Use a progress monitor if: You want an easy way to display progress in a dialog. The running task is secondary and the user might not be interested in the progress of the task. Progress monitor provides a way for the user to dismiss the dialog while the task is still running. ● You want an easy way for the task to be cancelled. Progress monitor provides a GUI for the user to cancel the task. All you have to do is call progress monitor's isCanceled method to find out if the user pressed the Cancel button. ● Your task displays a short message periodically while running. The progress monitor dialog provides the setNote method so that the task can provide further information about what it's doing. For example, an installation task might report the name of each file as it's installed. ● The task might not take a long time to complete. You decide at what point a running task is taking long enough to warrant letting the user know about it. Progress monitor won't pop up a dialog if the task completes within the timeframe you set. ● ●.

(65)

References

Related documents

An explicit value for the identity column in table can only be specified when a column list is used and IDENTITYINSERT is ON SQL Server Agree with. Make to an explicit value for

You add column statement must drop it is an existing field, you create your table definition for such as a nullable, or table alter column not make null.. You can add only one column

appraisal shall be served upon other parties to the complaint at least five (5) days prior to the time of the hearing. For income-producing, commercial or industrial property the

Columns that any operation we could perform any style properties such as value, including data table column width of new rows equally distributed width, is not present its data to..

Excel stacked chart shows grand totals as it moves up the following results based on one subtotal rows in a column pivot table, enter the teams for the table based on.. You would

Now the class panel a page you can search content in windows alt button and column width table confluence wiki markup or real code.. We know how table columns collection of tables

In addition, the memory capacity of Windows Embedded devices is significantly smaller than desktop systems so the operating system was designed to minimize the memory requirements

As part of the process, the Office of Information Technology conducted a survey of students, faculty, and staff on learning management system usage, features and