Understanding Swing's Model

zhaozj2021-02-08  227

Understanding Swing's Model

People who often use Swing Development Java GUI programs must have heard such a statement, and the Swing control is designed according to the MVC structure. More specifically, Swing is the structure of Model-Driven. But whether the model of different SWING controls is the same? For example, when you use JButton, you rarely need to care about ButtonModel, but you always need to use TableModel when you use JTable. Further, when you frequently use JTABLE, you will find that you may not only use TableModel, and use TableColumnModel, ListSelectionModel. This makes us realize that Model has different types, different types of Model implement different functions.

GUI-State Model

First, we discuss the first Model, Gui-State Model. GUI-State Model's role is to identify the visual state of the control (Visual Status). For example, if the button is clicked, Items in the list are selected. Swing Control Agent's operation of GUI-State Model, usually do not directly operate Gui-State Model.

Buttonmodel

The most common Gui-State Model is ButtonModel, which belongs to this category has JButton, JTogglebutton, JCheckbox, Jradiobutton, JMenu, JMenuItem, Jrac, JRADIOBUTTONMENUITEM. (All AbstractButton subclasses)

ButtonModel needs to be identified:

Pressed: Has Button is clicked

ENABLED: Can Button Clicked (Whether it is displayed gray)

ROLLOVER: Whether the mouse is scribbled from Button. Button determines whether it is to display Rollover by judging whether the property is to display Rollovericon, of course, the premise is Button through Strollover, and set ROLLOVERICON.

SELECTED: Only useful for Radiobutton or Checkbox

ARMED: Whether the mouse click Button to release this

Obviously, these properties are only related to the display. For GUI-State Model, there are only two cases we need to care about it: (1) We want to change the default visual behavior of the control (assuming this situation rarely) (2) For some display purposes shared MODEL, operating a control will change the status of another control (this will be discussed below), in other cases we can ignore it. Of course, there is anything that we need to pay attention, this is when using JRADIOBUTTON. Because when using Jradiobutton, a group of Jradiobutton can only have one selected (SELECTED), which of course only be implemented by operating ButtonModel's class attribute. However, Swing introduced the ButtonGroup class for this problem, set the same Button Group via the ButtonGroup.Add () method, so we also do not need to directly operate ButtonModel.

BoundedRangemodel

Another common Gui-State Model is BoundedRangemodel, which belongs to this category has JProgressBar JscrollBar Jslider.

The main status of the BoundedRangeModel identity is: min, max, value (int), the same, we rarely operate BoundedRangemodel). The most common way to use JProgressBar is to specify min, max, or read and write min, max, value via GET / SET, in the constructor. The control forwards these requests to BoundedRangemodel. As far as it is mentioned for some point of display, we may need to directly operate Gui-State Model. The following is a possible situation (Scenario): When we put a larger image in JScrollPane, it is desirable to control the display image displayed in the JScrollPane through the mobile slider (JSLider). Common practices are to monitor the ChangeEvent Event of BoundedRangemodel, which adjusts the corresponding adjustment in ChangeListener when Jslider changes the value of Model.

TableColumnModel

TableColumnModel is a JTable unique Gui-State Model. TableColumnModel is used to manage TableColumn. TableColumn represents the visual properties of each of the data in the jtable, such as the Data-Model INDEX corresponding to the column (this determines the content to display, see later, the width of this column varies, the maximum, minimum , Preferred width; the list of the plotter TableCellrenderer and editor TableCelleditor (JTable is a column, it draws and edits based on each column)

Selection Model

Consider such a problem, how do you set up from the X line to the Y line when using JTABLE?

We can be implemented by calling JTable.seTrowSerectionInterval (int index0, int index1). Further, if you want to implement the toggle selection, you click the number of times in the selection state, the even time is in a non-selected state, and the JTABLE does not provide a direct method to implement. Because JTABLE will be implemented by Selection Model. See the implementation of SetrowSelectionInterval

Public void setRowselectionInterval (int index0, int index1) {

SelectionModel.setSelectionInterval (Boundrow (Index0), Boundrow (Index1));

}

To implement Toggle Selection, only the Selection Model is programmed directly.

Selection Model also belongs to the category of GUI- State Model, because it is also a visual state, and the selected item will highlight. Like other Gui-State Model, usually we don't need to do it directly Selection Model.

The status of the Selection Model identity is:

SINGLE_SELECTION

SINGLE_INTERVAL_SELECTION

Multiple_Interval_Serection

We analyze other needs to determine the user choice several controls

Jlist, like JTable is ListSelectionModel

JTree, JTree needs Selection Model's most complicated, so it has special Selection Model: TreeselectionModel.

JCOMBOBOX can only be radio, so there is no need to have special Selection Model

JFilechOOOOOOSER, by setting Files_only, Directories_only files_and_directories (int) to set whether the user can select file, directory, or both. By setting the multislectionenabled attribute to determine whether or not, select the current selection of JTabbedpane, JTabbedpane, is only radio, but it can have a special Selection Model: SingleselectionModel

Like other GUI-State Models, the corresponding JComponent provides a special function shielding our direct operations.

Application-Data Model

This type of Model determines what is displayed in the control, so we often need to operate directly. They are:

Jlist: Listmodel Jtable: TableModel JcomboBox: ComboBoxModel Jtree: Treemodel all kinds of text controls: Document

Listmodel

Swing first defines the interface ListModel

Then define the abstract class AbstractListModel implementation this interface. There is no storage method for the actual data in the abstract class. So implement AbstractListModel, users also need to define these two functions

Public int getSize (); public object getElementat (int index);

Because there is no storage mode of the actual data, there is of course no way to provide the implementation of these two functions.

Final SWING provides the default class defaultListModel implementation abstraction class, the default class is used as a manner as a storage data.

There are four ways to construct a jlist:

Jlist ()

Jlist (firm Object [] listdata)

Jlist (Final Vector ListData)

Jlist (ListModel Datamodel)

The corresponding ListModel is generated in the first three constructors. It is also possible to use the following functions after the construction of the following functions.

Void setListData (Final Object [] ListData)

Void SetListData (Final Vector Listdata)

Void SetModel (ListModel Model)

Jlist does not provide the method of editing its Item, the user cannot directly edit its item (this is different from JCOMBOBOX, JCOMBOBOX provides a way to edit its Item), to change the content of Item, ListModel (with vectors) JLIST is not suitable for displaying data for variable content).

The Jlist to display variable content is to use DefaultListModel, but because it uses Vcetor as the way to store data inside, it is determined that they are not appropriate when handling the amount of data. First of all, Vector has the concept of internal capacity. When the capacity is not enough to accommodate more data, it needs to be reassigned, copy the original memory, and discard the original memory, this is a very time-consuming action; Second, Vector It is a Thread-Safe Collection, and all the operations of the container need synchronization (Synchronized), which is also very time consuming for Collection containing large data.

Therefore, for the amount of APPLICATION-DATA, users should choose between ArrayList and LinkedList (Thread-unsafe collection): ArrayList also has internal capacity concept, but it provides a random access (Random Access) ), When using it, you can apply a large amount of memory to avoid reassigning memory later.

LinkedList has no internal capacity concept, so it does not reassign the memory, but it does not provide random access.

TableModel

Swing is similar to TableModel's processing and ListModel:

First, the interface TableModel is defined.

Then define an abstract class to implement this interface AbstractTableModel. There is no storage method for the actual data in the abstract class.

To implement AbstractTableModel, users also need to define these three functions

Public GetColumncount () public object getValueat (int RowIndex, int columnIndex) public int achievecount ()

Public String getColumnname (int column) often needs to be defined, otherwise it will be displayed as A, B, C, D ... in the header.

Finally, SWING provides the default class DEFAULTTABLEMODEL implementation abstraction class, the default class is used as a manner as a storage data. Therefore, the operation of the large amount of data (such as the result of using a JTABLE display database query) is also not suitable for defaultTableModel. When using jtable to display the result of the database query, it is best to expand

AbstractTableModel and let the database return only batch data each time.

JTable constructor is more complicated than Jlist, because it also needs to specify TableColumnModel. But for the process of Application-Data Model and JLIST.

ComboBoxModel

JCOMBOBOX and JLIST are very similar, which are used to display a list item and accept user selection (JRADIOBUTTON also implements this). But they also have different nature, JCOMBOBOX can accept user input and edit existing options. Usually use JCOMBOBOX to divide it into two categories: editable and invigible. The default status is invisible, and JCOMBOBOX can be editable by calling JCOMBOBOX.SETEDITABLE (TRUE). Corresponding to these two status JCOMBOBOX define two types of Data-Model interfaces, ComboBoxModel and MutableComboBoxModel.

The definition of ComboBoxModel is as follows:

Public Interface ComboBoxModel Extends ListModel {

Void setSelectedItem (Object AnItem);

Object getSelectedItem ();

}

It expands ListModel and just defines the way to get and set up the current option, because JCOMBOBOX doesn't have Selection Model.

MutableComboBoxModel, as the name suggests, of course, defines how to modify Data-Model, which is as follows:

Public interface mutablecomboBOXMODEL EXTENDS ComboBoxModel {

Public void addelement (Object Obj);

Public void removelelement (Object obj);

Public void insertelementat (Object Obj, int index); public void removelelementat (int index);

}

Swing provides the implementation of the MutableComboBoxModel, and its internal Vector to store data. When we want to provide your own realities, the most convenient method is to extend AbstractListModel, and choose to implement ComboBoxModel or MutableComboBoxModel.

Constructing a JCOMBOBOX instance has four ways:

Public JCOMBOBOX ()

Public Object items [])

Public JCOMBOBOX (Vector Items)

Public JCOMBOBOX (ComboBoxModel Amodel)

The corresponding DEFAULTCOMBOBOXMODEL will be generated in the first three constructors. (Individual thinks that JCOMBOBOX is not good, since INVARIANTEL is created by constructor, the JCOMBOBOX can be edited, and JCOMBOBOX needs to first determine whether Data-Model can be edited when needed to modify Data-Model. For an invigible JCOMBOBOX call editing function, RuntimeException ())

Treemodel

TreeModel is the most complex Data-Model, and a detailed description of references

Various types of TEXT controls

Various TEXT controls are a relatively independent topic, which is no longer detailed here.

references

[1] Understanding The Treemodel:

Http://java.sun.com/products/jfc/tsc/articles/jtree/

[2] a swing architecture overview: http://java.sun.com/products/jfc/tsc/articles/architecture/index.html

[3] JGURU FAQ:

http://www.jguru.com/faq/

[4] «Swing» by Matthew Robinson & Pavel Vorobiev

转载请注明原文地址:https://www.9cbs.com/read-1839.html

New Post(0)