DakkVader
DakkVader

Reputation: 142

Java: List of names (String[]), how / can i display them using scrollbar?

I have a String[] with 115 names. Now of course, i cannot fit all these names at once on a screen that i've set to be 1280 x 720.

I would like to display let's say 15-20 of these as buttons from the top of the screen until they hit the bottom, and the right side of the screen is an image, so we're lookin for a GridLayout(115, 1);

How would i go about to create this so that i can see a few names, and the rest of the are further down in the list and i access them with a scrollbar? My program extends JFrame and the item to the right is a 720 x 720 JLabel that is one big picture.

Did not add my code since there is no specific questions asked here, merely what classes i'd be using and how i would go about to proceed and use them. I've done some research on JScrollPane , JList and such.

Upvotes: 0

Views: 184

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347194

Start by taking a look at

Basically, a scroll pane controls a single component which acts as the view. The scroll pane acts like a window, allowing you to see a position of the view if it is larger then the scroll panes viewable size...

JList iist = new JList();
List.setModel(...);
JScrollPane sp = new JScrollPane(list);
// add sp to something ...

Upvotes: 2

snickers10m
snickers10m

Reputation: 1749

Using a JScrollBar, you can add a class with myScrollBar.addAdjustmentListener(this) so it will call your AdjustmentListener's adjustmentValueChanged(AdjustmentEvent e) method. In this method, use e.getValue() to get the value of the scroll bar. Interpret this value (with some good 'ol mathematics) to update the visibility/invisibility and x/y positions of your buttons!

For more, read up a tutorial on JScrollBar's.

Don't forget to set the orientation, min and max values, etc.

Upvotes: 0

Related Questions