When developing a database application system using Java, you often need to display the query results on the user interface. Since the JDK1.x Development Kit provided by SUN is not a visual integrated development environment (IDE), the query results cannot be conveniently displayed in tables such as DBGrid as Delphi and VB. Therefore, you can only write your own code.
In practical applications, we can use Vector, JTable, and AbstractTableModel to better solve this problem. The following describes the implementation methods in detail.
1. Introduction to class Vector, class JTable and class AbstractTableModel:
1. Vector:
The class Vector is a collection class of Java history and belongs to the java. util package. It encapsulates heterogeneous linked lists and arrays. It has the following two features:
* The vectors are heterogeneous and do not require the same type of each element. The vectors can be mixed with Multiple object types;
* Vectors are arrays, because they can dynamically increase when adding elements.
Its Heterogeneity is in line with the characteristics of different attribute types in database records, and its dynamic nature is also in line with the characteristics of the number of results set records in database queries.
The class Vector is defined as follows:
Public class Vector extends actlist
Implements List, Cloneable, Serializable {...}
It allows you to search, add, and delete vector members. For example, add (Object obj) can easily add an Object; get (int index) can easily get an Object in the vector; remove (Object obj) you can easily delete an object in a vector.
2. JTable class:
The JTable component is a complex small component in the Swing component and is affiliated to the javax. swing package. It can display data in the form of a two-dimensional table. The JTable class is defined as follows:
Public class JTable extends JComponent
Implements TableModelListener, Scrollable, TableColumnModelListener,
ListSelectionListener, CellEditorListener, Accessible {...}
JTable has the following features when displaying data:
* Customization: You can customize the display mode and editing status of data;
* Heterogeneous: different types of data objects can be displayed, including complex objects such as colors and icons;
* Simplicity: You can easily create an external table by default.
Customization can meet the requirements of different users and scenarios, and the heterogeneity is also in line with the characteristics of different attribute types in the database access result set. JTable-like provides rich two-dimensional table operation methods, such as setting the editing status, display mode, and selecting columns.
Before using JTable to display data, you must generate a custom model, cell Paster, or cell editor as needed. The AbstractListModel class is used to customize your own data models. This class will be introduced later. The TableCellRenderer interface is used to customize the cell Paster, And the TableCellEditor interface is used to customize the cell editor. These two interfaces are mainly used to process color objects. They are not used in the example and are not described too much.