简体   繁体   中英

How do I visibly change the contents of my JScrollPane each time I call the ActionListener?

I want this JScrollPane to change its list to a completely different one every time the Action Listener is called. I'm not entirely sure on how to it. I've tried changing the JList, repainting the ScrollPane and revalidating it but it doesn't bring the desired effect. I wrote a compilable mini-program that sort of explains my question here:

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;

@SuppressWarnings("serial")
public class TestFrame extends JFrame {
public final Dimension SCROLL_PANE_DIMENSION = new Dimension(200, 100);
private JComboBox<String> comboNames;
private JPanel panel = new JPanel();
private JList<String> availableList;
private JScrollPane theScrollPane;
private boolean string1Used = true;
String[] strings = { "Aaardvark", "Adam", "Alms" };
String[] strings2 = { "Bad", "Bugs", "Bunny" };

public static void main(String[] args){
    try {
        TestFrame frame = new TestFrame();
        frame.setVisible(true);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public TestFrame() throws SQLException {
    String[] comboStrings = { "Doesn't", "matter", "here" };

    this.setSize(300,200);

    availableList = new JList<String>(strings);

    theScrollPane = new JScrollPane(availableList);

    theScrollPane = new JScrollPane(availableList);
    theScrollPane.setPreferredSize(SCROLL_PANE_DIMENSION);
    theScrollPane.setSize(SCROLL_PANE_DIMENSION);
    theScrollPane.setBorder(BorderFactory.createTitledBorder(
            BorderFactory.createEtchedBorder(), "Sub Conditions",
            TitledBorder.CENTER, 0));

    availableList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    availableList.setSelectedIndex(0);
    availableList.setVisibleRowCount(5);
    availableList.setSize(SCROLL_PANE_DIMENSION);

    comboNames = new JComboBox<String>(comboStrings);

    comboNames.addActionListener(new ConditionComboListener());

    panel = new JPanel();


    this.add(panel);


    panel.add(comboNames);
    panel.add(theScrollPane);

}

private class ConditionComboListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        refreshSubConditionList();
    }

    private void refreshSubConditionList() {
        if (string1Used) {
            System.out.println("Switching to Strings2");
            availableList = new JList<String>(strings2);
            string1Used = false;
        } else{
            System.out.println("Switching to Strings");
            availableList = new JList<String>(strings);
            string1Used = true;
        }
        theScrollPane = new JScrollPane(availableList);
        theScrollPane.revalidate();
        theScrollPane.repaint();
    }

}

}

In this example, I want the list inside of the JScrollPane to switch between the arrays strings1 and strings2 everytime I change the contents in the JComboBox. I also want this change to be visible AS SOON AS I change the contents in the combobox, not later. As you can see from the compiled code, it doesn't quite work out that way. What am I doing wrong? Why doesn't it change the list when I want it to?

Creating a new scrollpane doesn't add the scrollpane to the panel.

Anyway, it is not necessary to keep creating a new scrollpane. Just change the component that is displayed in the scrollpane:

//theScrollPane = new JScrollPane(availableList);
//theScrollPane.revalidate();
//theScrollPane.repaint();
theScrollPane.setViewportView(availableList);

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