By jtable on the use of vector<vector<object>> (from the a718515028 column)

Source: Internet
Author: User

Previously only used vector<object>, but in doing the export data from the database into the jtable, found that there is a vector<vector<object>> usage.

First, JTable and DefaultTableModel.

JTable itself is a list that can be displayed, but cannot be monitored by the button to increase the positive row data.

JTable no AddRow (object[] rowdata) or addrow (Vector rowdata), method.

But there is a construction method in JTable, JTable (TableModel DM)

And TableModel is an interface that implements this interface with a common class DefaultTableModel. There is a method in DefaultTableModel that AddRow (object[] rowdata) can add data from a one-dimensional array to the end of the model in this table, along with a addRow (Vector RowData) method, You can add a vector collection to the end of the model in the table.

Tip

Explain why there are AddRow (object[] rowdata) and AddRow (Vectorrowdata)?

We know that the array has an attribute of fixed length, AddRow (object[] rowdata) can only add a fixed length of an array of data. In other words, if the length of the data to be queried is unknown, if an array is required to initialize the length of the array beforehand, when the length is unknown, it must be set so large that it can store the data to be queried. If the data is not sufficient for the length of this array, then the remaining length will be added to the table or table model, which will be displayed in the table with blank data

Vector has a characteristic is the length variable, how much data to use the length of the vector, not enough automatic growth of the original length of one times. When adding data to a table or model, even if the length of the data to be queried is unknown, the vector will automatically increase the length to accommodate the data, without causing wasted space. Since there is no space in the vector, there is naturally no blank data in table.

How to display the data in JTable.

The data in the JTable is added to the table in the form of a two-dimensional array,

Publicjtable (object[][] rowdata,object[] columnnames)

The previous parameter represents the data, and the following represents the column.

Such as:

1 String [] title = {"Study number", "name", "Score"};   2 object[][]o = {"1", "Zhang San", "80"},{"2", "Li", "550"},{"3", "Wang er", "$"};   3 Jtable JT  

This only shows the fixed data. If you want to display data that is queried from a database, you must use vector<vector<object>>. Why not directly with Vector<object>,, such as directly to the VECTOR<STUDENT>, directly add to JTable, Student object can automatically get Student name, achievement equivalence, add to JTable?? This seems reasonable, but, we need to know that jtable can only display a data, not intelligent display of the object's individual property values, so we want to the student object's property values are also explicitly output to put in a collection, and then put into the table. Each row of the table consists of a vector, and each row of data consists of a vector, which requires a vector to be placed in the vector, because the base of the vector is also array-based, so it is like a two-dimensional array, the first vector representing the row, The second vector represents the column data for this row.

One way to construct DefaultTableModel in the API is to define the

DefaultTableModel
Public DefaultTableModel (Vector data,
Vector columnnames) Constructs a defaulttablemodel and initializes the table by passing data and columnnames to the Setdatavector method.

Parameters:
Data-a table that is a vector consisting of vectors containing multiple Object values (note)
ColumnNames-Vector containing the new column name

For example we have a well-queried set vector<book> Booklist;

Show this collection in JTable

vector<string> title = Newvector<string> ();//Column NameTitle.add ("ISBN"); Title.add (Title); Title.add ("Book type"); Title.add (Price); Vector<Vector<Object>> data = newvector<vector<object>> ();//     for(inti = 0; I < booklist.size (); i++) {Vector<object>v =NewVector<object> ();//the inside layer of the vector store all the data of the bookBOOKB=Booklist.get (i);                           V.add (B.GETISBN ());                           V.add (B.getbookname ());                           V.add (B.getbooktype ());                                                            V.add (B.getprice ()); Data.add (v);//the outer layer of vector data holds the first vector v that already has the data.} DefaultTableModel Model=NewDefaultTableModel (data, title); Table=NewJTable (model) {Publicboolean iscelleditable (intRowintCol) {                                      return false;}};//Use an anonymous inner class to override the isCellEditable method in this jtable, set to non-editable. 

By jtable on the use of vector<vector<object>> (from the a718515028 column)

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.