Hi,

I wanted to know how you can link two list Boxes together.
This is what I'm trying to do:

I'm creating two list boxes, list box A and list Box B. List Box A is a list of different departments. When the user clicks on a particular department, the courses within that department show up in list Box B. So when the user clicks on another department, different courses show up in list box B depending on what department is clicked on in list box A.

I know how to create a list Box but I'm having trouble to do the above^

Please could you help.

Thank you.

You can add a ListSelectionListener to the first list. It will be called whenever the user changes his selection in the list. When that is called you can query which line is selected and use that to put the appropriate data in the second list.

Could someone please assist me more in this? Thanks.

What have you done so far given the instructions in my previous post?

This is all of what I've done so far.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.ListSelectionModel;

public class ListFrame extends JFrame 
{
    
public ListFrame()
{
super();
setLayout(new FlowLayout());

String[] DeptNames = {"Computing", "Media", "Creative Technology", "Mathematics"};

String[] CompNames = {"BSc Business Computing", "BSc Computing", "BSc Computer Science", "BSc Computer Science for Games", 
        "BSc Computing and Information Systems", "BSc Computer Systems Administration", "BSc ICT", "BSc Internet Computing", 
        "BSc Mobile Computing", "BSc Multimedia Computing","BSc Robotics with artificial intelligence", "BEng Software Engineering"};

String[] MediaNames = {"BA Digital and Creative Enterprise", "BA Digital Media", "BA Film Studies", "BA Media Studies", 
        "BA Photography for Digital Media", "BA TV Production", 
        "BSc Creative Media and Technologies", "BSc Web Design and Technology", "BSc Media Technology and Production"};
        
String[] CreativeNames = {"BA Computer Animation", "BSc Computer Animation and Special Effects", "BA Design for Computer Games", 
         "BSc Interactive Systems and Video Games Design"};
         
String[] MathsNames = {"BSc Computational Mathematics"};
        
JList list;
JList listtwo;
ListSelectionModel listSelectionModel;


list = new JList(DeptNames);
listSelectionModel  = list.getSelectionModel();
listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());

list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

listtwo = new JList(CompNames);
listtwo = new JList(MediaNames);
listtwo = new JList(CreativeNames);
listtwo = new JList(MathsNames);

listSelectionModel  = listtwo.getSelectionModel();
listtwo.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


list.setVisibleRowCount(3);
add(new JScrollPane(list));


listtwo.setVisibleRowCount(3);
add(new JScrollPane(list));


JScrollPane listPane = new JScrollPane(list);
JScrollPane listPane = new JScrollPane(listtwo);


JPanel controlPane = new JPanel();


this.setVisible(true);
this.setLocation( 100, 100 );
this.setSize( 400, 300 );

OK, good start. Next step is to write a SharedListSelectionHandler (unless you have already). Have a go and post it back here if you get stuck.

I've tried it out but I don't know how to write the SharedListSelectionHandler. Also the coding doesn't work. Could you please explain to me how to do this. Thank you.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.ListSelectionModel;

public class ListFrame extends JFrame 
{
    
public ListFrame()
{
super();
setLayout(new FlowLayout());

String[] DeptNames = {"Computing", "Media", "Creative Technology", "Mathematics"};

String[] CompNames = {"BSc Business Computing", "BSc Computing", "BSc Computer Science", "BSc Computer Science for Games", 
        "BSc Computing and Information Systems", "BSc Computer Systems Administration", "BSc ICT", "BSc Internet Computing", 
        "BSc Mobile Computing", "BSc Multimedia Computing","BSc Robotics with artificial intelligence", "BEng Software Engineering"};

String[] MediaNames = {"BA Digital and Creative Enterprise", "BA Digital Media", "BA Film Studies", "BA Media Studies", 
        "BA Photography for Digital Media", "BA TV Production", 
        "BSc Creative Media and Technologies", "BSc Web Design and Technology", "BSc Media Technology and Production"};
        
String[] CreativeNames = {"BA Computer Animation", "BSc Computer Animation and Special Effects", "BA Design for Computer Games", 
         "BSc Interactive Systems and Video Games Design"};
         
String[] MathsNames = {"BSc Computational Mathematics"};
        
JList list;
JList listtwo;
ListSelectionModel listSelectionModel;


list = new JList(DeptNames);
listSelectionModel  = list.getSelectionModel();
listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());

list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

listtwo = new JList(CompNames);
listtwo = new JList(MediaNames);
listtwo = new JList(CreativeNames);
listtwo = new JList(MathsNames);

listSelectionModel  = listtwo.getSelectionModel();
listtwo.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


list.setVisibleRowCount(3);
add(new JScrollPane(list));


listtwo.setVisibleRowCount(3);
add(new JScrollPane(list));


JScrollPane listpane = new JScrollPane(list);
JScrollPane listpane = new JScrollPane(listtwo);


JPanel controlPane = new JPanel();


this.setVisible(true);
this.setLocation( 100, 100 );
this.setSize( 400, 300 );
}
}

class SharedListSelectionHandler implements ListSelectionListener {
public void valueChanged(ListSelectionEvent event) { 

if ((JList)event.getSource() == "Computing") 
     listtwo = JList(CompNames);
     
else if ((JList)event.getSource() == "Media") 
     listtwo = JList(MediaNames);

else if ((JList)event.getSource() == "Creative Technology") 
     listtwo = JList(CreativeNames);
     
else if ((JList)event.getSource() == "Mathematics") 
      listtwo = JList(MathsNames);

else 
    listtwo = "";

}
                

}

Can anybody help me on this please :(

First, read, understand, and fix your compile errors. Then:

You need to populate the first list with the four option names.

event.getSource() gives the control where the event originated - in this case it's your first list, so thst's not the method you need. You need to get the index of the first selected line in the list box (see the JList API doc) to see which line (and therefore which option) was selected.

Check the JList API to find the right method to populate listtwo with one of your four String arrays, depending on which line was selected in the first list.

What do you mean by populate the first list with the four option names and where can I find the JList API doc? Thanks for your help.

Line 20 you declare an array of dept names - I guess you intended to display these in the first list? Unless I missed it, you forgot to put those names in the list.

You find all the reference material for the Java API (also sample code etc) at http://download.oracle.com/javase/index.html
You can also download the API doc so its on your hard disk and is faster to access.

I made these changes to the code

class SharedListSelectionHandler implements ListSelectionListener {  

02 public void valueChanged(ListSelectionEvent event) {   

03    

04 listtwo = list.getSelectedValue();  

05    

06    

07             if (list.getSelectedValue.equals("Computing"))  

08             listtwo.equals("CompNames");  

09         

10             else if (list.getSelectedValue.equals("Media"))  

11             listtwo.equals("MediaNames");  

12    

13             else if (list.getSelectedValue.equals("Creative Technology"))  

14             listtwo.equals("CreativeNames");  

15         

16             else if (list.getSelectedValue.equals("Mathematics"))  

17             listtwo.equals("MathsNames");  

18    

19             else 

20             listtwo.equals("");  

21                

22             }  

23 }

But it is still not working. Could somebody please help me out. :( thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.