Jtable for GUI development to implement JB attribute tables

Source: Internet
Author: User

Source: [url] http://www.wangchao.net.cn/bbsdetail_45551.html#/url]

[Url = mailto: _ gtm_@163.com] the ownership of the article is owned by me, if you want to use for commercial purposes, please contact me, if you want to post, please indicate the source and author. powered by G. t. m. [/url]
I am very happy to have time to write down my own development experience and change it again. From the underlying data to the GUI, I think I am growing.
After I develop new things or solve new problems, I will write a new history of blood and tears. during the National Day holiday, this attribute table will be occupied by [img] http://dev.csdn.net/emoticons/cry_smile.gif#/img]
Too many questions:
All users who have used JB know that the design of JB is a good interface. This time I am participating in a project to produce an aplha version, so I want to write a GUI similar to IDE, I am responsible for Attribute tables and trees.
It was not difficult to implement a jtable (although it took some detours at the beginning to engage in jlist), as long as one side is the attribute name and the other side is the value. event Response or dragging are not the main issues and can be easily implemented. However, to achieve dynamic generation of data and different performance and modification methods based on specific attribute values, it is a little troublesome. If it is in different columns, but if it is in the same cloumn, it will ......
At the beginning, I wrote an internal class for setting up jtable in extends's replicacttablemodel (for details about the API, refer to the basic tutorial) to dynamically generate data in the future, I can determine the data connection, so I decided to use hashmap and attribute name for the attribute value. The following code is specific:
Class mytablemodel extends acttablemodel {
Hashmap data;

Public void setdata (hashmap hm, jtable table ){
Data = HM;

/* While (J & gt; = 0 ){
Type = array [J]. getclass (). tostring ();
If (type. Equals ("class java. util. Vector ")){
Contain = (vector) array [J];
Jcombobox ComboBox = new jcombobox ();
Int I = contain. Size ()-1;
While (I & gt; = 0 ){
ComboBox. additem (contain. Get (I --));
}
Table. prepareeditor (New defaultcelleditor (ComboBox), J, 1 );
}
If (type. Equals ("class java. Lang. boolean ")){
Boolean TF = (Boolean) array [J];

Jcheckbox jcbox = new jcheckbox ("", Tf. booleanvalue ());
Table. getcelleditor (J, 1) = new defaultcelleditor (jcbox), J, 1 );

}
J --;

}
Table. updateui ();*/

/* Table. setdefaulteditor (vector. Class,
New mytablecelleditor ());
Table. setdefaultrenderer (vector. Class,
New mycomboboxrenderer ());*/
}

Public int getcolumncount (){
Return 2;
}

Public int getrowcount (){
Int Rv = data. Size ();
Return RV;
}

Public String getcolumnname (INT col ){
Return ""; // This is very interesting. Without this "", there is no common header. You can cancel it and check the effect.
}

Public object getvalueat (INT row, int col ){
If (COL = 0 ){
Object [] OL = data. keyset (). toarray (); // the specific method for creating each cell in jtable is as follows:
Return ol [row];

} Else {
Object [] ol2 = data. Values (). toarray ();
Return ol2 [row];
}
}

/*
* Jtable uses this method to determine the default Renderer/
* Editor for each cell. If we didn't implement this method,
* Then the last column wocould contain text ("true"/"false "),
* Rather than a check box.
*/

/*
* Don't need to implement this method unless your table's
* Editable.
*/
Public Boolean iscelleditable (INT row, int col ){
// Note that the data/cell address is constant,
// No matter where the cell appears onscreen.
Return Col = 1;
}

/*
* Don't need to implement this method unless your table's
* Data can change.
*/
Public void setvalueat (object value, int row, int col ){
If (COL = 1 ){
Object [] OL = data. Values (). toarray ();
Ol [row] = value;
}
Firetablecellupdated (row, col );

}

}

Since it was changed from others' examples at the beginning, it is misleading because jtable itself has built-in default editors such as ComboBox and checkbox, generally, you can use the getcolumnclass method in the model. note !! This is the key and the culprit that has consumed my National Day holiday !! If you override this method in this internal class, you can obtain the default editor in the whole line for rewriting, which is very convenient, as shown below:
Public void setupsportcolumn (jtable table,
Tablecolumn column ){
// Set up the editor for the sport cells.
Jcombobox ComboBox = new jcombobox ();
ComboBox. additem ("snowboarding ");
ComboBox. additem ("Rowing ");
ComboBox. additem ("knitting ");
ComboBox. additem ("Speed reading ");
ComboBox. additem ("pool ");
ComboBox. additem ("none of the above ");
Column. setcelleditor (New defaultcelleditor (ComboBox ));

// Set up Tool tips for the sport cells.
Defaulttablecellrenderer Renderer =
New defaulttablecellrenderer ();
Renderer. settooltiptext ("click for combo box ");
Column. setcellrenderer (Renderer );
}
Then add a getcolunmclass Method to the internal class above, and return the class of a column in the corresponding row (Note: if the model structure is object, examples can be found on the Internet)
But it is not suitable for our needs, because the Attribute Table only has two rows, and the performance of no column may be different.
Therefore, in jtable itself, it is his getcelleditor (INT row, int column), which is the final conclusion I obtained after repeated single-step debugging, because the Renderer and editor of each cell in the jtable need to be located, the so-called positioning is to find the corresponding editor and render, and its internal implementation is obtained by column, that is, the entire column uses the same editor. this is a nightmare when I find out. then I spent a lot of time discovering its [url = file: // D:/jdk_1.4_05_doc/docs/API/javax/swing/jtable.html # prepareeditor (javax. swing. table. tablecelleditor,] prepareeditor [/url] ([url = file: // D:/jdk_1.4_05_doc/docs/API/javax/swing/table/T Ablecelleditor.html] tablecelleditor [/url] editor, int row, int column) and [url = file: // D: /jdk_1.4_05_doc/docs/API/javax/swing/jtable.html # setdefaulteditor (Java. lang. class,] setdefaulteditor [/url] ([url = file: // D:/jdk_1.4_05_doc/docs/API/Java/lang/class.html] class [/url] columnclass, [url = file: // D:/jdk_1.4_05_doc/docs/API/javax/swing/table/tablecelleditor.html] tablecelleditor [/url] editor) the latter can use the CL of the object Ass to get the Corresponding Editor and render, but in jtable, the key is getcelleditor (INT row, int column) can I return the class of a column in a row correctly? Because of the hashmap I used (in addition to the corresponding key value, this means that when the retrieved object is a vector, A jcombobox will be automatically encapsulated for modification). but in fact, the internal code of getcelleditor (INT row, int column) does not use row when only column is used !! [Img] http://dev.csdn.net/emoticons/hitwall.gif#/img]
If the getcolumnclass of the model cannot obtain the row parameter, I chose to inherit the parameter without compromising the encapsulation integrity.
Getcelleditor (INT row, int column) is rewritten once.
Return getdefaulteditor (this. GetModel (). getvalueat (row, column). getclass ());
Then, the editor and Renderer are encapsulated in the GUI, such:
Public class mycomboboxrenderer implements tablecellrenderer {
Public mycomboboxrenderer (){

}

Public component gettablecellrenderercomponent (jtable table,
Object value,
Boolean isselected, Boolean hasfocus, int row, int column ){
Vector VV = NULL;
If (value instanceof vector ){
VV = (vector) value;

Jcombobox box = new jcombobox (vv );

If (isselected ){
Setforeground (table. getselectionforeground ());
Box. setbackground (table. getselectionbackground ());
} Else {
Setforeground (table. getforeground ());
Setbackground (table. getbackground ());
}

// Select the current value
Box. setselecteditem (value );

Return box;
}
Return NULL;
}
}
And Renderer.
Public class mytablecelleditor extends actcelleditor implements
Tablecelleditor {
// This is the component that will handle the editing of the cell value
Jcombobox box;

// This method is called when a cell value is edited by the user.
Public component gettablecelleditorcomponent (jtable table, object value,
Boolean isselected, int rowindex, int vcolindex ){
// 'Value' is value contained in the cell located at (rowindex, vcolindex)
// Jcombobox box = NULL;
Vector VV = NULL;
// If (value instanceof vector ){
VV = (vector) value;

Box = new jcombobox (vv );

If (isselected ){
Setforeground (table. getselectionforeground ());
Box. setbackground (table. getselectionbackground ());
} Else {
Setforeground (table. getforeground ());
Setbackground (table. getbackground ());
}

// Select the current value
Box. setselecteditem (value );

Return box;
//}
// Return NULL;
}
We can use jcombobox to modify the vector in a cell. If it is a Boolean cell, we can use checkbox. in fact, the most important thing is to understand the jtable mechanism. now, the problem has been solved. If you have any questions, please contact me.

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.