In Web projects, drop-down boxes, jquery, and Ajax can be used to implement a pull-down box linkage query.
For example, when you inquire about a place, the page is: Province:< dropdown Box province > City:< dropdown box City > County:< dropdown Box County Township > Street:< Street dropdown box > query
For example, you chose the province: Jiangsu Province, then in the urban area will only show the city of Jiangsu Province
For example: (online map)
Concrete detailed implementation can refer to, write very good: http://blog.csdn.net/sinat_24491773/article/details/50810471
So how to implement linkage query in swing form?
In fact, it is also very simple, think about the Web implementation process, is only in the first province after the dropdown box selection, set the Listener event, and then use Ajax to get the data behind the drop-down box, and then update it
So, on the basis of this idea, is swing going to be okay?
1.jcombobox settings Add ItemListener event;
2. Rewrite the itemstatechanged method of ItemListener;
3. Empty the original data of the subsequent drop-down boxes;
4. Reset the original data for subsequent drop-down boxes.
The code is as follows:
Jcb_college.additemlistener (New ItemListener () {//For college drop-down box set listener @Overridepublic void itemstatechanged ( ItemEvent e) {jcb_grade.removeallitems ();//clear the drop-down box's raw data, Jcb_grade.setmodel (new defaultcomboboxmodel<> ( GetField (Constantdata.grade)));//Obtain new data Jcb_class.removeallitems (); Jcb_class.setmodel (New DefaultComboBoxModel <> (GetField (Constantdata.class));});
The GetField () method in the previous generation is to obtain the grade and class of the selected college from the database.
The pseudo code is as follows:
1. Obtain the data selected by the College drop-down box; selected = jcb_college.getselect (); 2. Obtain selected college from the database all grades select grade from grade where college = = selected;
Quote the Bolg in the above summary: In fact, all the same, they do it is still very happy.
Java Swing Application JComboBox drop-down box linkage query