簡體   English   中英

每次調用ActionListener時,如何明顯更改JScrollPane的內容?

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

每次調用Action Listener時,我都希望這個JScrollPane將其列表更改為完全不同的列表。 我不完全確定如何做到這一點。 我已經嘗試更改JList,重新繪制ScrollPane並重新驗證它,但它沒有帶來預期的效果。 我寫了一個可編輯的迷你程序,在這里解釋了我的問題:

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();
    }

}

}

在這個例子中,每當我更改JComboBox中的內容時,我希望JScrollPane內的列表在數組strings1和strings2之間切換。 我也希望這個改變是可見的,因為我改變了組合框中的內容,而不是以后。 正如您從編譯的代碼中看到的那樣,它並沒有那么好用。 我究竟做錯了什么? 為什么我不希望它改變列表?

創建新的滾動窗格不會將滾動窗格添加到面板。

無論如何,沒有必要繼續創建一個新的滾動窗格。 只需更改滾動窗格中顯示的組件:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM