Java – how do I change the contents of the JScrollPane significantly each time I call the actionlistener?

Every time I call the action listener, I want the JScrollPane to change its list to a completely different list I'm not quite sure how to do this I've tried to change JList, redraw scrollpane and revalidate it, but it didn't bring the expected effect I wrote an editable Mini program to explain my problem 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 JCombo@R_781_2419@<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 JCombo@R_781_2419@<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, whenever I change JCombo@R_781_2419 @I want the list in JScrollPane to switch between arrays strings1 and strings2 I also hope this change is visible because I changed the content of the combo box, not later As you can see from the compiled code, it doesn't work that well What on earth did I do wrong? Why don't I want it to change the list?

Solution

Creating a new scroll pane does not add the scroll pane to the panel

In any case, there is no need to continue creating a new scroll pane Just change the components displayed in the scroll Pane:

//theScrollPane = new JScrollPane(availableList);
//theScrollPane.revalidate();
//theScrollPane.repaint();
theScrollPane.setViewportView(availableList);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>