Question

I use CardLayout and want to use two Buttons with .next() and .previous() to scroll through the Items. It works great with only one problem: I want to stop scrolling if the current Item is the first/last Item.

I can't find a Method to get the Index of the current Item or a flag to find out wether the current Item is first/last or not.

Can you tell me a simple way to find out if the current item is the first/last?

Since there are the methods .first() and .last() to jump, i guessed there must be a method to find out if the current is the first/last.

Was it helpful?

Solution

I would simply use a List<String> containing all the names of the components in the card layout, and a field containing the index of the currently displayed component.

To know if you're at the beginning, you would test if index == 0. To know if you're at the last component, you would test if index == list.size() - 1. To show the next component, you would use index++; layout.show(parent, list.get(index));.

OTHER TIPS

i extended CardLayout, overriding several methods

That was my solution as well. See Card Layout Focus for my implementation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top