About Swing's model

Source: Internet
Author: User
Tags abstract define final functions implement variable thread access
Model
People who often develop Java GUI programs with swing must have heard that swing controls are designed in the MVC architecture. More precisely, swing is the model-driven structure. But does the model of the different swing controls have the same effect? For example, when you are using JButton, you rarely need to care about the existence of Buttonmodel, but in jtable use, you always need to use TableModel. Further, when you use jtable frequently, you will find that you may not only use TableModel, but also Tablecolumnmodel, ListSelectionModel. This makes us realize that there are different kinds of model, different types of model to achieve different functions.

Gui-state Model

First of all, we discuss the first model, GUI? Cstate Model. The role of Gui-state model is to identify the visual state of the control (visual status). For example, if the button is clicked, the item in the list is selected. Swing Controls Act on the Gui-state model, and usually do not directly manipulate the GUI? Cstate Model.
Buttonmodel


The most common Gui-state model is Buttonmodel, the controls that fall into this category are JButton, Jtogglebutton, Jcheckbox, Jradiobutton, JMenu, JMenuItem, JCheckBoxMenuItem, JRadioButtonMenuItem. (Subclasses of all AbstractButton)



The status of the Buttonmodel need to be identified is:


Whether the Pressed:button was clicked

Enabled:button can be clicked (whether it appears dimmed)

Rollover: Whether the mouse from the button across. button to determine whether to display Rollovericon by judging this property, of course, if the button is passed Setrollovericon, set the Rollovericon

SELECTED: Only useful for RadioButton or Checkbox

Armed: After the mouse clicks button, whether the button should be released


Obviously, these attributes are only relevant to the display. For Gui-state Model, there are only two things we need to be concerned about: (1) We want to change the default visual behavior of the control (assuming this rarely happens) (2) Share Model for some display purposes, Manipulating a control changes the state of another control (which is discussed below), and in other cases we can ignore it. There is, of course, a situation where we need to be aware that this is when using Jradiobutton. Because a group of Jradiobutton can have only one selected (SELECTED) at the same time when using Jradiobutton, this is certainly done only by manipulating the SELECTED property of the Buttonmodel. However, swing introduced the Buttongroup class for this problem and set the same button group through the Buttongroup.add () method, so we also don't need to manipulate the Buttonmodel directly.






BoundedRangeModel




Another common Gui-state model is BoundedRangeModel, a control that falls into this category with JProgressBar Jscrollbar JSlider.



The main states of the BoundedRangeModel logo are: min,max,value (int), similarly, we rarely directly manipulate BoundedRangeModel. The most common way to use JProgressBar is to specify Min,max in the constructor or read and write Min,max,value through Get/set. The controls then forwarded these requests to the BoundedRangeModel.



mentioned above for some display purposes, we may need to directly manipulate gui-state Model. Here's a possible scenario (scenario): When we put a larger image in the JScrollPane, we also want to control the display image in JScrollPane by moving the slider (jslider). A common practice is to listen for BoundedRangeModel changeevent events (Addchangelistener (ChangeListener L)), When JSlider changes the value of model, the display is adjusted in ChangeListener.






Tablecolumnmodel


Tablecolumnmodel is the jtable peculiar gui-state Model. Tablecolumnmodel is used to manage TableColumn. TableColumn represents the visual properties of each column of data in the jtable, such as the corresponding Data-model index of the column (which determines what to display, see later), whether the column is variable in width, the maximum, minimum, and preferred width of the column The column's Tablecellrenderer and editor Tablecelleditor (jtable is column-oriented, drawing and editing based on each column)


Selection Model


Consider the question of how to set a selection from x row to Y when using jtable.



We can do this by tuning the jtable.setrowselectioninterval (int index0, int index1). Further, if you want to implement the reverse selection (Toggle Selection), that is, the number of clicks in the selection state, even several times in the optional state, JTable does not provide a direct method to implement. Because JTable will choose the work to selection model to achieve. See the realization of Setrowselectioninterval



public void Setrowselectioninterval (int index0, int index1) {



Selectionmodel.setselectioninterval (Boundrow (index0), Boundrow (index1));



}



To implement toggle selection, you only have to directly program the selection model.



Selection model also belongs to the category of Gui-state model, because it is also a visual state, the selected item will be highlighted (high light). As with other gui-state model, we usually do not need to directly manipulate selection model.



The status of the Selection model identification is:


Single_selection

Single_interval_selection

Multiple_interval_selection


Let's analyze other needs to judge the user to select several controls


JList, like JTable, is ListSelectionModel.

JTree, JTree needs the selection Model is the most complex, so it has a dedicated selection Model:treeselectionmodel.

JComboBox can only be a radio, so do not need to have a special selection Model

JFileChooser, set files_only, directories_only files_and_directories (int) To set whether the user can select files, directories, or both. By setting the Multiselectionenabled property to decide whether to select one or more, record the current selection through a file array selectedfiles

JTabbedPane, JTabbedPane the situation is somewhat special, although JTabbedPane can only be selected, but it can have a special selection Model:singleselectionmodel


The way to treat selection model and other gui-state model, the corresponding jcomponent provide a special function to block our direct operation of it.


Application-data model




This type of model determines what is displayed in the control, so it often takes a direct action. They were:

Jlist:listmodel
Jtable:tablemodel Jcombobox:comboboxmodel Jtree:treemodel Various text controls: Document

Listmodel


Swing first defines the interface Listmodel

The abstract class AbstractListModel is then defined to implement this interface. There is no way to define the actual data stored in an abstract class. So to implement AbstractListModel, the user also needs to define these two functions
public int getsize (); Public Object getelementat (int index);
Because there is no way of storing the actual data, of course, there is no way to provide the implementation of these two functions.

Finally swing provides the default class Defaultlistmodel implementation abstract class, and the default class is vector as the way to store data.

There are four ways to construct an JList instance:

JList ()

JList (final object[] listdata)

JList (Final Vector listdata)

JList (Listmodel Datamodel)

In the first three constructors, the corresponding Listmodel are generated separately. You can also use the following functions to make the jlist after construction Listmodel

void Setlistdata (Final object[] listdata)

void Setlistdata (Final Vector listdata)

void Setmodel (Listmodel model)



JList does not provide a way to edit its item, and the user cannot edit its item directly (this is different from JComboBox, JComboBox provides a way to edit its item directly), To change the contents of the item, you need to directly manipulate Listmodel (the array and vector generation jlist are not suitable to display variable content data). The easiest way to display the jlist of variable content is to use Defaultlistmodel, but because it uses vcetor as its internal storage data, it is not appropriate for them to handle the display of large volumes of data. First Vector has the concept of internal capacity, when the capacity is not enough to accommodate more data, it needs to reallocate a piece of memory, copy the original memory, and discard the original memory, which is very time-consuming action; second, vector is a thread-safe container (thread-safe collection), all operations on the container need to be synchronized (synchronized), which is also time-consuming for collection that contain large amounts of data. Therefore, for large data application-data, if users want to use collection, they should be in ArrayList and LinkedList (Thread-unsafe collection) Choose between: ArrayList also has the concept of internal capacity, but it provides a random access function (random access), which can be used in advance to request a large memory, so as not to reallocate memory later.


LinkedList does not have the concept of internal capacity, so it does not reallocate memory, but it does not provide random access functionality.

TableModel


Swing is similar to TableModel processing and Listmodel:

First defines the interface TableModel,

The abstract class is then defined to implement this interface Abstracttablemodel. There is no way to define the actual data stored in an abstract class.

To implement Abstracttablemodel, the user also needs to define these three functions
Public getColumnCount () public Object getvalueat (int rowIndex, int columnindex) public int getrowcount ()
The public String getcolumnname (int column) often needs to be defined, otherwise the table header will appear as A,b,c,d ...

Finally swing provides the default class DefaultTableModel implementation abstract class, and the default class is vector as the way to store data. Therefore, the operation of large amount of data (such as the results of JTable Display database query) is also not suitable for DefaultTableModel. When using JTable to display the results of a database query, it is best to extend

Abstracttablemodel, and let the database return only the batch of data at a time.



The JTable constructor is more complex than jlist because it also needs to specify Tablecolumnmodel. But for the Application-data model processing and jlist is the same.


ComboBoxModel


JComboBox and JList are very similar, are used to display a list item, and can accept the user's choice (Jradiobutton also implement this function). But they are also fundamentally different, JComboBox can accept user input, and can edit existing options. Typically, you use JComboBox to divide it into two categories: editable and not editable. The default state is not editable, and the JComboBox can be set to editable by calling Jcombobox.seteditable (true). Two types of Data-model interfaces, ComboBoxModel and Mutablecomboboxmodel, are defined for these two states JComboBox.



The definition of ComboBoxModel is as follows:

Public interface ComboBoxModel extends Listmodel {

void Setselecteditem (Object anitem);

Object GetSelectedItem ();

}

It extends Listmodel and simply defines the way to get and set the current option, because JComboBox does not have selection Model.

Mutablecomboboxmodel, as the name suggests, of course, defines the method of modifying Data-model, which is defined as follows:

Public interface Mutablecomboboxmodel extends ComboBoxModel {

public void addelement (Object obj);

public void removeelement (Object obj);

public void Insertelementat (Object obj, int index);

public void Removeelementat (int index);

}

Swing provides the implementation of Mutablecomboboxmodel DefaultComboBoxModel, its internal vector to store data, and when we want to provide our own reality, the most convenient way is to expand the AbstractListModel, and choose whether to implement ComboBoxModel or Mutablecomboboxmodel.



There are four ways to construct a JComboBox instance:

Public JComboBox ()

Public JComboBox (Final Object items[])

Public JComboBox (Vector items)

Public JComboBox (ComboBoxModel Amodel)

In the first three constructors, the corresponding DefaultComboBoxModel are generated separately. (Personally feel that this construction jcombobox is not good, because the constructor does not establish invariance (invariants), that is, the JComboBox is editable, when you need to modify the Data-model, JComboBox need to first determine whether Data-model is editable, and the JComboBox call edit function will be thrown out runtimeexception () for non-editable.


TreeModel


TreeModel is one of the most complex Data-model, a reference to a detailed description


Various text controls


Each type of text control is a relatively independent subject, which is no longer detailed here.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.