1.11M Members

how to know which jList is selected and according to that fetch the value


I am Using netBeans..

I have 4 JLists (i.e jList1 , jList2, jList3 , jList4) in 4 different tabs in frame.

Each List has the value of employee_id from database (i successfully did it already)
there are many field in tabs ( i.e rows of databse with employee id as primary key)

How to know which jList is selected so that i get the value of employee_id from the correct jList (i.e which is selected)
to fetch data from database using selected employee id from selected jList with a common handler

A part of my program is

` private void get_empid_detail(java.awt.event.MouseEvent evt) {
try {

   Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/hrms","root","soft");

   Statement stmt=con.createStatement();

   boolean list1notSelected = jList1.isSelectionEmpty();     // caution or error whatever-> list1notSelected not in use
   boolean list2notSelected = jList2.isSelectionEmpty();     // same for list2notSelected
   boolean list3notSelected = jList3.isSelectionEmpty();     //same for list3notSelected
   boolean list4notSelected = jList4.isSelectionEmpty();     // same for list4notSelected

    String id;

    if ( list1notSelected = false){
         id  = (String) jList1.getSelectedValue();         // caution or error whetever
   else if(list2notSelected = false){                           // every id in loop is not in use 
          id  = (String) jList2.getSelectedValue();
   else if(list3notSelected = false){
          id  = (String) jList3.getSelectedValue();
   else if(list4notSelected = false){
          id  = (String) jList3.getSelectedValue();

     String sql1 = "Select * from emp where emp_id = '" + (id) + "'";  //error -> variable id might not have been initised
    ResultSet rs = stmt.executeQuery(sql1);
        while (rs.next())
        String empid = rs.getString("emp_id");
        String f_name = rs.getString("first_name");
        String l_name = rs.getString("last_name");
        String qual = rs.getString("qualification");
        String dept = rs.getString("department");

        jTextField27.setText(""+ empid);
        jTextField28.setText(""+ f_name);
        jTextField29.setText(""+ l_name);
        jTextField30.setText(""+ qual);
        jTextField31.setText(""+ dept);

catch(Exception e) {
    JOptionPane.showMessageDialog(this, e.getMessage());

Please Help . I have to submitt my project on monday and this is the only bug remaining..
Every part of software is working properly except this problem.



How to know which jList is selected

Each JList can have a selected item in it. What do you mean by "JList is selected"? They all can have a selected item. Have you looked at the API doc for the JList class? It has lots of methods that could help you. The method you are using would tell you if a JList has nothing selected.

One problem I see in your code is that you are using an assignment operator = instead of an equality test operator == in the if statements.

Why are you using negative logic: notSelected instead of positive: isSelected?
You are asking: is this JList not not selected instead of is it selected


Instead of this:

if ( list1notSelected = false){
id = (String) jList1.getSelectedValue(); // caution or error whetever

Try this:

if ( list1.isSelected()){
id = (String) jList1.getSelectedValue(); 

As i said earlier ..
i want to use a common handler for 'mouse clicked event' for all 4 jLists..
whenever user click value ( i.e emp_id populated already in list) on any of the jList in 4 tabs , the vale of selected emp_id is to be used for fetching column details from the database..
I am successful in getting data from database using emp_id selected in the list , but only one list ..


HOW to know from which jList the value is selected so that i can use if-else loop ?


Thanx Mr Mehfooz but the solution given by you is not working i think ..


what is list ? i am using jList and NetBeans says error when i use
if (jList1.isSelected())

Please help man!


i have tried this

`boolean list1Selected = !jList1.isSelectionEmpty();
boolean list2Selected = !jList2.isSelectionEmpty();
boolean list3Selected = !jList3.isSelectionEmpty();
boolean list4Selected = !jList4.isSelectionEmpty();

String id;
    if ( list1Selected == true ){
        id  = (String) jList1.getSelectedValue();
   else if(list2Selected ==true){
        id  = (String) jList2.getSelectedValue();
   else if(list3Selected ==true){
        id  = (String) jList3.getSelectedValue();
   else if(list4Selected ==true){
          id  = (String) jList3.getSelectedValue();

    String sql1 = "Select * from emp where emp_id = '" + (id) + "'"; // error is here with id - variable id might not have been initialised `

Give id a default value when you define it.

The if/else if chain will stop testing ater the first true is found. What if more than one list has a selecton?


Norm1 good point ..
Thank a lot ..
i have solved the problem if more than one list have selected.
all lists are in different tabs , when user press another tab , then getfocused event of that tab clears the selection of other jfields using jList.clearSelection()

The problem is Solved..
Thanx a lot guys..
I Love This Site..

Question Answered as of 2 Years Ago by NormR1 and mehfooz
This question has already been solved: Start a new discussion instead
Start New Discussion
Tags Related to this Article