简体   繁体   中英

Multiple JLists on one JPanel

I am making a program that prints out the items on a JList and am trying to use the least memory possible. Therefore, I only want to use one JList variable but still have two lists on the screen that I can read from. I have come in to problems in finding how to find values in the first list that I initialize and add to the JPanel.

JPanel initialization method:

public  void run() {
    panel = new JPanel(null);
    //Initalize components

    //List 1
    ArrayList<String> st = new ArrayList<String>();
    for(int i=0;i<10;i++) st.add("Number: " + i);
    jli = new JList(st.toArray());
    jli.setName("MyList1");
    jli.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jli.setLayoutOrientation(JList.VERTICAL);
    jli.setSelectedIndex(-1);
    jli.setVisibleRowCount(-1);
    JScrollPane jsp = new JScrollPane(jli);
    jsp.setSize(new Dimension(120, 130));
    jsp.setLocation(5, 5);
    panel.add(jsp);

    //List 2
    st = new ArrayList<String>();
    for(int i=0;i<10;i++) st.add("#: " + i);
    jli = new JList(st.toArray());
    jli.setName("MyList2");
    jli.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jli.setLayoutOrientation(JList.VERTICAL);
    jli.setSelectedIndex(-1);
    jli.setVisibleRowCount(-1);
    jsp = new JScrollPane(jli);
    jsp.setSize(new Dimension(120, 130));
    jsp.setLocation(150, 5);
    panel.add(jsp);

    //Button 1
    jb = new JButton("Print 1");
    jb.addActionListener(this);
    jb.setLocation(5, 140);
    jb.setSize(jb.getPreferredSize());
    panel.add(jb);

    //Button 2
    jb = new JButton("Print 2");
    jb.addActionListener(this);
    jb.setLocation(120, 140);
    jb.setSize(jb.getPreferredSize());
    panel.add(jb);
}

ActionPreformed Method:

public void actionPerformed(ActionEvent e) {
    switch (e.getActionCommand()) {
        case "Print 1":
            //This is where I need help
            break;
        case "Print 2":
            System.out.println(jli.getSelectedValue());
            break;
    }
}

In short, How do I find data from the first JList created and the Second JList created without making two JList variables?

Although I find it pretty weird, but if you insist on having only one variable of each type, then you will have no direct access to the first instantiated list. Assuming your panel and jli are defined globally, you can find the list by searching the child components of the panel :

@Override
    public void actionPerformed(ActionEvent e) {
        Component[] components = panel.getComponents();
        for (Component component : components) {
            // child component must be a JList, but not jli
            if(component instanceof JList && !component.equals(jli)) {
                JList firstList = (JList) component;
            }
        }
    }

I would still like to repeat that this code is a complete overhead that can be simply eliminated by introducing another JList variable. Instead of accessing the list directly, you will have to search for it, consuming cpu time. Also, what would you do if you would have a third JList ? You couldn't then distinct the two inaccessible JList s from each other. If you still insist on having one variable, consider using a List of JList s:

List<JList> allMyJLists = new LinkedList<>();
JList jli = new JList(); // first JList
allMyJLists.add(jli);
jli = new JList(); // second JList
allMyJLists.add(jli);

Now access first JList with

allMyJLists.get(0);

and the second one with

allMyJLists.get(1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM