I have a JTabbedPane implementing a SCROLL_TAB_LAYOUT display option.
I use setSelectedIndex to select a tab that is not being displayed at
a particular instant in time (in other words out-of-range but
accessible
using the scroll arrows). The selection is successful, but the tab is
not brought into the display area. Scrolling to the selected tab
indicates that it has indeed been selected. What can one do to bring
the selected tab into the scroll range when the tab is being selected
programmaticaally using setSelectedIndex? Below is an oldish message on
this BB about a similar problem, but there was, unfortunately, no
reply.
Kind regards, Willem Ferguson
From: SunDog - view profile
Date: Thurs, May 8 2003 5:57 pm
Email: wind_of_f...@earthlink.com (SunDog)
Groups: comp.lang.java.programmer
Hello,
I am using a JTabbedPane to allow a user to switch between seeing
multiple components. Due to screen size limitations, there can only
be one row of tabs. We have working code to add tabs as necessary.
The problem comes when many tabs are already in place (so that
scrolling is active), and then more tabs are added. I want to set the
new tab to be selected -- which works just fine. However, the new tab
is not visible until the user presses the scroll-right button to get
to it.
I.e., whichever tabs were visible before adding the new tab are the
same tabs which are visible after setting them. The new tab is way
off to the right, hidden away where the user can't see it or know that
it's active. Since our application is targetted at novice computer
users, this is likely to lead to much confusion.
The question is: can I set which tabs are visible in a scrolled
JTabbedPane? Alternatively, can I set what the leftmost visible tab
is?
Thank you in advance for any help.
Michael Dunn - 08 Aug 2006 19:51 GMT
>I have a JTabbedPane implementing a SCROLL_TAB_LAYOUT display option.
> I use setSelectedIndex to select a tab that is not being displayed at
[quoted text clipped - 37 lines]
>
> Thank you in advance for any help.
works OK in 1.5.0_05
simple (rough) demo
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Testing extends JFrame
{
JTabbedPane tp = new JTabbedPane();
public Testing()
{
setLocation(300,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
tp.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
JMenu tabsMenu = new JMenu("Show Tab:");
MenuItemListener mil = new MenuItemListener();
for(int x = 0; x < 12; x++)
{
JPanel p = new JPanel();
p.add(new JLabel(""+x));
String letter = ""+(char)(x+65);
tp.addTab(letter,p);
JMenuItem menuItem = new JMenuItem(letter);
tabsMenu.add(menuItem);
menuItem.addActionListener(mil);
}
JMenuBar menuBar = new JMenuBar();
menuBar.add(tabsMenu);
setJMenuBar(menuBar);
getContentPane().add(tp);
pack();
}
class MenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
int selectedTab = (int)(((JMenuItem)ae.getSource()).getActionCommand().charAt(0))-65;
tp.setSelectedIndex(selectedTab);
}
}
public static void main(String[] args){new Testing().setVisible(true);}
}
WillemF - 09 Aug 2006 07:57 GMT
> works OK in 1.5.0_05
> simple (rough) demo
Thanks very much indeed, Michael.
Willem