java - Why I cannot make the setSelectedIndex work? -
so i'm trying make buttons go through each of tab panels , works fine. have make them repeat running through tabs every time 1 of tabs reach end. made "previous" button work, , works, can't seem right numeric expression make "next" button work. i've tried many different numeric expressions have far:
next = new jbutton("next"); next.addactionlistener( new actionlistener() { @override public void actionperformed(actionevent e) { tabs.setselectedindex(tabs.getselectedindex()+1); tabs.setselectedindex(tabs.getselectedindex()-7); } }); previous = new jbutton("previous"); previous.addactionlistener( new actionlistener() { @override public void actionperformed(actionevent e) { tabs.setselectedindex(tabs.getselectedindex()-1); tabs.setselectedindex(tabs.getselectedindex()+6); } });
and exception gives me every time try next button:
exception in thread "awt-eventqueue-0" java.lang.indexoutofboundsexception: index: -6, tab count: 6
when work, skips last tab, don't know doing wrong, have feeling because of numeric expression.
you should use cycle edges cases.
something this.
next = new jbutton("next"); next.addactionlistener( new actionlistener() { @override public void actionperformed(actionevent e) { int nextindex = tabs.getselectedindex()+1; tabs.setselectedindex( (nextindex < tabs.gettabcount())?nextindex:0 ); } }); previous = new jbutton("previous"); previous.addactionlistener( new actionlistener() { @override public void actionperformed(actionevent e) { int previousindex = tabs.getselectedindex()-1; tabs.setselectedindex((previousindex < 0)?tabs.gettabcount()-1:previousindex); } });
Comments
Post a Comment