package layout;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TabbedPaneWithComponent extends JFrame {
final private JTabbedPane tabbedPane;
final private JButton button;
public TabbedPaneWithComponent() {
setDefaultCloseOperation( EXIT_ON_CLOSE );
setSize(200,200);
setLocationRelativeTo( null );
JPanel panel = new JPanel();
panel.setLayout( new OverlayLayout(panel) );
getContentPane().add(panel);
tabbedPane = new JTabbedPane();
tabbedPane.add("1", new JTextField("one"));
tabbedPane.add("2", new JTextField("two"));
tabbedPane.add("3", new JTextField("three"));
tabbedPane.add("4", new JTextField("four"));
tabbedPane.add("5", new JTextField("five"));
tabbedPane.add("6", new JTextField("six"));
tabbedPane.add("7", new JTextField("seven"));
tabbedPane.add("8", new JTextField("eight"));
tabbedPane.add("9", new JTextField("nine"));
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
tabbedPane.setAlignmentX(Component.RIGHT_ALIGNMENT);
tabbedPane.setAlignmentY(Component.TOP_ALIGNMENT);
button = new JButton();
button.setBackground(Color.GREEN);
button.setMaximumSize(new Dimension(15,7));
button.setAlignmentX(Component.RIGHT_ALIGNMENT);
button.setAlignmentY(Component.TOP_ALIGNMENT);
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
final JPopupMenu pop = new JPopupMenu();
int max = tabbedPane.getTabCount();
for (int i = 0; i < max; i++) {
JMenuItem mi = new JMenuItem(tabbedPane.getTitleAt(i));
mi.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
int index = pop.getComponentIndex((Component) e.getSource());
tabbedPane.setSelectedIndex(index);
}
});
pop.add(mi);
}
pop.show(button, 0, button.getHeight());
}
});
panel.add( button );
panel.add(tabbedPane);
}
public static void main(String args[]) {
new TabbedPaneWithComponent().setVisible(true);
}
}