Creating a drop-down list in a Job

author
Shicong Hong
EnrichVersion
6.4
6.3
6.2
6.1
6.0
EnrichProdName
Talend Big Data Platform
Talend Big Data
Talend Data Integration
Talend Data Services Platform
Talend ESB
Talend Open Studio for Data Integration
Talend Data Fabric
Talend Open Studio for ESB
Talend Open Studio for MDM
Talend Real-Time Big Data Platform
Talend MDM Platform
Talend Data Management Platform
Talend Open Studio for Big Data
task
Design and Development > Designing Jobs
EnrichPlatform
Talend Studio

What is a drop-down list?

A drop-down list is a user interface control GUI element ("widget" or "control"), that displays a list of values and allows the user to select one value from the list at runtime.

Creating a Mysql database for the drop-down list

In order to create a drop-down list in a Job in the following examples, you should have a Mysql database.

Before replicating the following examples, make sure you have created a Mysql table named employee. The table stores people information as shown below.

id name email sex department position
1 Elisa elisa@company.com F RD manager
2 Nicolas nicolas@company.com M RD developer
3 Sabrina sabrina@company.com F Community developer
4 Stephane stephane@company.com M Sales manager
5 Jim jim@company.com M Sales pre-sales

Creating a drop-down list

This Job shows how to use a MySQL database table to create a drop-down list in a Talend job.

This Job uses the following components:

  • a tJava component to prepare the drop-down list and store the value selected by the user at runtime in the context variable.
  • a tMysqlInput component to query the people information from the employee table.
  • a tLogRow component to print the result on the console.

Procedure

  1. In your Talend Studio, create a new Job named DropDownListExample.
  2. Click the Contexts tab and define a context variable of String type, call it name. This variable stores the drop-down value selected by the user at runtime.
  3. Drop a tJava and a tMysqlInput component from the Palette onto the design workspace. Connect the tJava and a tMysqlInput component with a Trigger > OnSubjobOk link.
  4. Drop a tLogRow component and connect it with a Row > Main link to tMysqlInput.
  5. Double-click tJava and:
    1. add the following piece of Java code to prepare the drop-down list:
      String[] nameStrings = { "Elisa", "Nicolas", "Cedric", "Rabbit", "Mike","Sabrina","Stephane","Jim","John"};
                                  JFrame frame = new JFrame("Input Dialog Example");
                                  String returnValue = (String) JOptionPane.showInputDialog(frame,
                                  "Select the name you want to query?","Employee Information",JOptionPane.QUESTION_MESSAGE,null,nameStrings,nameStrings[0]);
                                  context.name=returnValue; //store the value to the context variable
    2. Store the value selected by the user at runtime in the context variable,
    3. Click Advanced settings and import the full path of the class used in this Job.
  6. Configure the tMysqlInput to query the people information from the employee table:
    1. in the Query box, use a context variable to select the information of the employee chosen by the user at runtime,
      "select * from employee where name='"+context.name+"'"
    2. in the Edit Schema dialog box, define the data structure of the employee table.
  7. Double-click tLogrow and configure its settings to print the results on the Studio console.
  8. Execute the Job, and select one of the names from the drop-down list when prompted.
  9. For example, select Stephane from the list. The information is queried from the table and printed on the console.

Creating a dynamic drop-down list

In the first scenario, you have hard-coded the list in a tJava component. However, the items of the list might come from a data source. For example, the items can be stored in a database table and their number can be unknown.

This Job uses the following components:

  • a tMysqlInput component to query the people information from the employee table,
  • a tJavaFlex component to prepare the dynamic drop-down list and store the value selected by the user at runtime, in the context variable,
  • a second tMysqlInput component to generate the information processed by tJavaFlex,
  • a tLogRow component to print the result in the console.

Procedure

  1. Drop a tMysqlInput component and a tJavaFlex component from the Palette onto the design workspace. Connect the two components using a Row > Main link.
  2. Drop a second tMysqlInput component and a tLogRow component from the Palette onto the design workspace. Connect the two components using a Row > Main link. Connect the first subjob to the second using a Trigger > On Subjob Ok connection.
  3. Click the Context tab and define a context variable named named of String type:
  4. Double-click the first tMysql and configure it as in the above example:
    1. add the following query in the query box:
      "select name from employee"
    2. Edit Schema and set this schema.
  5. Click on tJavaFlex to open its Basic settings view and add the following code.
    1. In the Start code box:
      java.util.ArrayList<String> list=new
                                          java.util.ArrayList<String> ();
    2. In the Main code box:
      list.add(row1.name);
    3. In the End code box:
      String [] nameList=new String[list.size()];
                                      nameList=list.toArray(nameList);
                                      JFrame frame = new JFrame("Input Dialog Example");
                                      String returnValue = (String) JOptionPane.showInputDialog(frame,
                                      "Select the name you want to query?","Employee Information",JOptionPane.QUESTION_MESSAGE,null,nameList,nameList[0]);
                                      context.name=returnValue;
  6. In the Advanced Settings panel of tJava, import the full path of the class used in this Job.
  7. Configure the second tMysqlInput component to query the people information from the employee table, and use the tLogRow to print the result in the console, as in the above scenario.
  8. Execute the Job. All the names in the table will be the items of the list as shown below:
  9. For example, select Nicolas from the list. The information related to Nicolas is queried from the table and printed in the console.