omlfc be
omlfc be

Reputation: 147

A JList rolls down when a JButton is clicked ? JAVA

I basically want to make a JList (whatToSearch) roll-down, or simply show its content, for selection, once a JButton (popDownButton) is clicked.

//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);

This bit of code works, but I'm looking for the content of the JList to be shown in the same interface, next to the button, rather than in another pop-up interface.

Upvotes: 1

Views: 93

Answers (2)

UkFLSUI
UkFLSUI

Reputation: 5672

Hope this helps:

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

Upvotes: 1

No Em
No Em

Reputation: 59

You can try this code, it's really easy:

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

More ways: Scroll JScrollPane to bottom

Upvotes: 2

Related Questions