0

in my code i'm creating a Jlist with A and B and i'm trying to construct a JList depending on what the user will click( A or B ). So if the user clicks A then i want list2 to appear in my frame. If he clicks B then list3 must appear. But right now i have to click to the frame in order for the list to appear. Can someone explain to me how is this possible and what's wrong with my code?

import java.awt.*;       
import java.awt.event.*; 
import javax.swing.*; 


public class test extends JFrame implements MouseListener
{
    private static JList list1 = new JList();
    private static JList list2 = new JList();
    private static JList list3 = new JList();
    private static JPanel jp1;
    private static JScrollPane listScroller2,listScroller3;
    private DefaultListModel listModel1,listModel2,listModel3;

    public test() 
    {
    setTitle("Menu");
    JTabbedPane jtp = new JTabbedPane();
    setSize(600,600);
    getContentPane().add(jtp);
    jp1 = new JPanel(); 
    jp1.setLayout(new BorderLayout());


    listModel1 = new DefaultListModel();
    listModel1.addElement("A");
    listModel1.addElement("B");

    list1 = new JList(listModel1);
    JScrollPane listScroller1 = new JScrollPane(list1); 
    listScroller1.setPreferredSize(new Dimension(100, 100));  
    jp1.add(listScroller1, BorderLayout.WEST);

    listModel2 = new DefaultListModel();
    listModel2.addElement("A1");
    listModel2.addElement("A2");


    list2 = new JList(listModel2);
    listScroller2 = new JScrollPane(list2); 
    listScroller2.setPreferredSize(new Dimension(100,100));  


    listModel3 = new DefaultListModel();
    listModel3.addElement("B1");
    listModel3.addElement("B2");


    list3 = new JList(listModel3);
    listScroller3 = new JScrollPane(list3); 
    listScroller3.setPreferredSize(new Dimension(100,100));  


    jtp.addTab("Letters", jp1);

    list1.addMouseListener(this); 
    }

    public void mouseClicked(MouseEvent e) 
    {
        int i = list1.getSelectedIndex();
        if(i == 0)
        {
            jp1.add(listScroller2, BorderLayout.CENTER); 
        }
        else if ( i == 1 )
        {
            jp1.add(listScroller3, BorderLayout.CENTER); 
        }
    }

public void mouseExited(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseReleased(MouseEvent event){}
public void mousePressed(MouseEvent event){}


     public static void main(String[] args) 
     {
        test tp = new test();
        tp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tp.setVisible(true);

     }
}

Edit: And how to switch through the lists all the time and not just staying in list2 or list3?

Discordgr
  • 13
  • 5
  • 1
    Use a `ListSelectionListener` in a way similar to [this](http://stackoverflow.com/a/3191882/230513). – trashgod May 28 '16 at 19:44
  • 1
    Rather than adding components dynamically, it would be easier to add a list at start-up, and populate the list model when the user makes a choice. Alternately, put three cards in a `CardLayout` consisting of list 1, list 2 and a blank panel. Flip between them as needed. – Andrew Thompson May 28 '16 at 20:34
  • I see...I tried both and they worked...Thanks a lot!! – Discordgr May 29 '16 at 12:47

0 Answers0