2016-12-26 30 views
1

我基本上要做出JList(whatToSearch)滾下來,或者乾脆顯示其內容,以供選擇,一旦JButton(popDownButton)被點擊。單擊JButton時,JList是否會滾降? JAVA

//SEARCH OPTIONS 
popDownButton = new JButton(new ImageIcon(new ImageIcon("downArrow.png").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT))); 
whatToSearch = new JList(elementsToSearch); 
whatToSearch.setVisibleRowCount(3); 
whatToSearch.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
scroll = new JScrollPane(whatToSearch); 

popDownButton.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent event) { 

     JOptionPane.showMessageDialog(null, scroll.getViewport()); 
    } 
}); 
add(popDownButton); 

這段代碼的工作,但我正在尋找在同一界面中顯示的JList的內容,旁邊的按鈕,而不是在另一個彈出界面。

回答

1

希望這有助於:

popDownButton.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent event) { 
     whatToSearch.setSelectedIndex(elementsToSearch.getSize() - 1); 
     whatToSearch.ensureIndexIsVisible(elementsToSearch.getSize() - 1); 
    } 
}); 
2

你可以試試這個代碼,它真的很簡單:

popDownButton.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent event) { 
      JScrollBar vertical = scroll.getVerticalScrollBar(); 
      vertical.setValue(vertical.getMaximum()); 
    } 
}); 

更多方式:Scroll JScrollPane to bottom

+0

它沒有工作,我很害怕,當我點擊按鈕時,沒有任何反應 –