( i): basic interaction: A simple component that gets the user's input or display status.
1. Button (JButton): Text can prompt shortcut keys, can be placed relative to the icon, using ActionListener;
2. check box (jcheckbox): You can select or cancel, use ItemListener, check box and radio button are the sub-classes of the toggle Jtogglebutton.
3. Combo box (JCOMBOBOX): You can select one or self-editing from multiple options, use an array, collection, or model ComboBoxModel construct, through getselected (index| Item) determines the selected item, using ItemListener, if the user edits the content themselves, the index is 1, and the element is the user's input; The Listcellrenderer render option is available (default is JLabel). Edit the current item with Comboboxeditor (default is JTextField).
4. List box (JList): Multi-column display setlayoutorientation, multi-choice setselectionmode, can be used array, set or model Listmodel construction (dynamic increase or decrease), Use Listselectionlistener (getvalueisadjusting as false), render with Listcellrenderer, and make the index visible with ensureindexisvisible.
5. Menu item (jmenuitem): Menu bar JMenuBar contains multiple menu jmenu, menus can have submenu JMenu, menu item JMenuItem, Group Bar jseparator, check menu JCheckBoxMenuItem, The JRadioButtonMenuItem menu can have text (font color, size, style), and icons.
6. Radio button (Jradiobutton): usually combined with Buttongroup (Operation Model Buttonmodel), each group of radio button can only select one, using ActionListener.
7. Slider (jslider): Horizontal or vertical style, with size ruler and label, using ChangeListener (when Getvalueisadjusting is false), with Setlabeltable (Hashtable<integer, jlabel>) Custom labels, or use createstandardlabels to create offset labels, usually the sliders and Jformattedtextfield pair and precisely position the values (Numberformatter ( Numberformat.getintegerinstance ())).
8. Fine-Tuning (JSpinner): With Spinnermodel construction (numeric range Spinnernumbermodel, date span Spinnerdatemodel, array set Spinnerlistmodel), each model has corresponding editors and components , use ChangeListener.
9. Text box (JTextField): Has the initial string and the column width, obtains the text with the GetText, uses the ActionListener.
10. Password box (JPasswordField): Set the echo character with Setechochar and use GetPassword to get the password.
(b): Non-editable components: used to display information or prompts.
11. Label (JLabel): Display text and icons, set shortcut keys for Target with Setdisplayedmnemonic and setlabelfor, use Set (vertical| Horizontal) (alignment| TextPosition) to set the icon and text position, you can use HTML to display multiple lines of hypertext.
12. Progress bar (JProgressBar): The range of available values to build JProgressBar, with SetValue and setstring change progress and text, with setstringpainted display text; The progress bar may be indeterminate, Wait until you can be sure to use setindeterminate to change back (suitable for complex control, multi-progress bar, reuse occasions); Advanced Progress Monitor progressmonitor, built with parent, title, note, Min, Max, Update the status with Setprogress and Setnote, iscanceled determine whether to cancel, setmillistopopup show the delay time (suitable for simple tips, easy to cancel the occasion) There is also a progressmonitorinputstream wrapper stream, which displays a progress box if the read takes longer than the delay time;
13. ToolTip (JToolTip): Any jcomponent can settooltiptext set ToolTips, for JTabbedPane can set settooltiptextat for each label or provide corresponding parameters when AddTab For JTable and JTree available cell renderer implementations, the renderer is jcomponent, call function Settooltiptext on it, or overwrite Gettooltiptext (MouseEvent), navigate to the column position, Return the generated hints, for custom components can inherit jcomponent and implement Gettooltiptext, JToolTip components are used and created by Jcomponent.createtooltip;
(iii): Common container components: used for layout general components
14. Universal Panel (JPanel): Default opaque setopaque, can set border SetBorder, manual layout of components when configuring the appropriate layout manager for FlowLayout, BoxLayout, GridLayout and springlayout add directly, for BorderLayout need to provide azimuth parameters, for the gridbaglayout need to provide the unit limit parameters gridbagconstraints For CardLayout, a key is required for switching the display;
15. Scroll panel (JScrollPane): Provide target object at build time, or use GetViewport (). (Set|get) View child client object; scroll bar Jscrollbar via get (horizontal| Vertical) ScrollBar, with scrolling strategy (on demand, always available, total none), scrolling distance set (unit| Block) Increment, domain (range and current value), using Adjustmentlistener, scroll panel includes center, four-sided, corner nine regions, the angle needs to be visible when the two pro edge, available set (column| Row) Headerview sets the row and column edge header, sets the corner component with Setcorner, and invokes the revalidate when the target object changes;
16. Split Panel (JSplitPane): Use split strategy SetOrientation and two sub-panel set (left| Right) component build, you can nest or include a scrolling panel, you can set the split bar position size and pull-over characteristics Setdivider (location| Size) (If you add only one component, the split bar will stick to the edge), Scaling Setresizeweight to 0.0 indicates that the size of the upper left component is fixed, the component size changes need to use Resettopreferedsizes update the split panel, adjust the instant display with Setcontinuouslayout, with Propertychangeli Stener Monitoring Property divider_location_property;
17. Label panel (JTabbedPane): You can specify the label position settabplacement (quartet) and placement strategy Settablayoutpolicy (multiline wrapping or rolling extension) at build time, or add tags using addtab or inserttab. Need to provide title, icon, component, Tip, index, can be indexed by index to configure the relevant information, Indexoftab, etc. can be obtained by relevant information tag index, tabcomponent is the label title of the renderer ; SetSelected (index| Component) Set the current tab, using ChangeListener monitoring;
18. Toolbar (JToolBar): When the container is borderlayout and there is only one subcomponent, the added toolbar can be dragged to the Quartet (change with setfloatable), with Setrollover (true) setting to display the border when the mouse passes over Add split bar with AddSeparator, you can also add components other than buttons, direction settings with SetOrientation, component direction with setalignment (x| Y), such as top_alignment;
(iv): Top-level window components: components that can be displayed independently (other components must have a top-level window root and can only be contained within a container), have menu bars and content panes, and are not thread-safe.
19. Applet (JApplet): Its init, start, stop, destroy are called by ordinary threads, so the actual initialization function is called for thread-safe invokeandwait, and timer and SwingWorker can be used to assign timing and time-consuming tasks Embedded page with applet elements, can contain menus and so on.
20. dialog box (JDialog): dialog box in the display and destruction on the frame, there is the concept of pattern modal, simple standard dialog can use Joptionpane, message type sub-error, warning, information, questioning, normal, option type of default, whether, Cancel, OK cancel (only for Showconfirmdialog, result points yes, no, OK, Cancel, close), input box can provide array to choose, option box can customize text;
21. Main Window (JFrame): Title decoration can be provided by the system or the appearance of setdefaultlookandfeeldecorated, or can not setundecorated; there is a title, icon, size, Location property, Windowadapter that can be hidden, destroyed, exited, or customized when closed.
(v): Advanced Interactive Components
22. Color selection (JColorChooser): You can add to the container or bring up the color selection window ShowDialog, use Colorselectionmodel to manage colors and notify ChangeListener, The selected color is then obtained by GetColor (null), and the preview and selection panel is changed by Setpreviewpanel and Setchooserpanels;
23. File Selection (JFileChooser): Add to container or bring up File Selection window show (open| Save) Dialog, return approve_option and use Getselectedfile to get the selected file, you can select or choose a directory setfileselectionmode, hidden file setfilehiddenenabled, Filter files Setfilefilter, multiple filters addchoosablefilefilter, all files setacceptallfilefilterused, custom file views Setfileview provide information such as icons and descriptions, Additional thumbnail setaccessory components are changed by becoming the PropertyChangeListener listener file selection.
24. Multi-line text (JTextArea): Provides the number of rows and columns set at build time (rows| Columns), word wrap and intercept words setlinewrap, Setwrapstyleword, scroll to the end setcaretposition, add text append, insert text inserts, replace text replacement, Select all text SelectAll, a row index getlinestartoffset.
25. Rich Text (Jtextpane): Simple display of Web pages available jeditorpane.setpage, custom Format editor Stylededitorkit available Jtextpane.registereditorkitforcontenttype, Stylecon stants format, use Styleddocument to change the existing text format, use Stylededitorkit to configure the following input format.
26. Table (JTable): tables can be constructed with Object[][]data+object[]column or Vector data+vector column, or extended Abstracttablemodel provide rows, columns, and column names, Unit can be edited and cell data can be added to JScrollPane after the setpreferredscrollableviewportsize to specify the visual range, For other containers to manually add Table.gettableheader to North, using Setselectionmodel to specify the selection mode, with Listselectionlistener Listener user selection, when evt.getvalueisadjusting () No when using Getminselectionindex to obtain the selected, any multi-select can be used Getmaxselectionindex and isslectedindex comprehensive judgment to find all the selection row ; Returns the cell specific type with getColumnClass to jtable find the default renderer and editor, with Tabcolumn.setcell (renderer| Editor) to specify the column renderer and editor (Defaultcelleditor wrapper Jcheckbox, JComboBox, and JTextField), the editor can have a checksum input function with Jformattedtextfield, If the column data type can be constructed using a string, the default editor is sufficient, with Jtable.setdefault (editor| Renderer) (class,editor) to specify a default editor or renderer for a data type ; use Renderer.settooltiptext to set the hint, the custom renderer in the gettablecellrenderercomponent can be prompted according to the unit content, expand JTable overlay GetToolTip (MouseEvent) Returning a hint after locating the unit can also , Tablerheader.settooltiptext can be used to set the table tip, but also cover the Jtable.createdefaulttableheader Jtableheader Gettooltiptext ( MouseEvent) returns a hint after the index is set, and the table can be sorted by tablemap+tablesorter or tablerowsorter+jtable.setrowsorter, with rows, columns, and cells.
27. Tree (JTree): Build with Defaultmutabletreenode as root node hierarchy, or Defaulttreemodel model to notify Treemodellistener, JTree and other listeners to update the display ; Treeselectionmodel's Setselectionmodel and Addtreeselectionlistener can be selected and listeners, using tree. Getlastselectedpathcomponent get the node and Getuserobject, according to the node type isleaf know what type of user data; Hide the root node with setrootvisible, Use Setshowroothandles to display the handle of the root node, set the Linetype with Tree.putclientproperty ("Jtree.linestyle", "angled"), Set the renderer Defaulttreecellrenderer with Setcellrenderer (in the Set (leaf| open| Closed) icon set the node icons, in the gettreecellrenderercomponent with Settooltiptext set the hint, but also need to register tooltipmanager.sharedinstance (). RegisterComponent (tree), add node to Getselectionpath (). Getlastpathcomponent () with Treemodel.insernodeinto (new node), Parent,parent.getchildcount ()), display nodes with Scrollpathtovisible (Childnode.getpath ()), Treeselectionlistner and tree[ willexpand| Expansion]listener the selection and expansion of the monitoring tree, using seteditable to make the tree editable, using setdragenabled and Settransferhandler to set the drag characteristics, with (expand|collapse) Path ( TreePath) expands or shrinks a branch.
(vi): Special purpose containers
28. Internal window (JInternalFrame): Usually added to JDesktopPane, it is not a top-level window (not the root of the component hierarchy, generating internalframeevent), but platform-independent (programmable minimization, main icon, title bar decoration) ; Need to use Set (size| Bounds) or pack set size otherwise not visible, setlocation can be used to set the relative position, the component may be added to its contents pane, the Internal dialog box with joptionpane.showinternal and other functions, Desktop.setdragmode ( Outline_drag_mode) enable border drag mode to increase efficiency.
29. Hierarchy pane (JLayeredPane): Adds a third dimension z-order to the container, is used in the JRootPane, and adds the specified level of components (higher level in front) with Add (Componet,integer). Change the hierarchy of components with Setlayer, using SetPosition or MoveTo (front| Bach) adjust the upper and lower positions within the hierarchy, the components can be setbounds set size and position, the existing LayoutManager will only think of all components as one layer and layout, with (lowest|highest) layer and getcomponent (s| Count) Inlayer get all the components.
30. Root pane (JRootPane): Used in the top-level container, including glass panes (Getglasspane, default transparency, to intercept input events), hierarchy pane (Getlayeredpane, place menu bar and Contents pane), The contents pane (Getcontentpane, place component), menu bar (setjmenubar, main menu), and Setdefaultbutton to set the default button.
Summary of swing components frequently used in Java