Java Swing Application JComboBox drop-down box linkage query

Source: Internet
Author: User

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

Related Article

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.