problem with scroll tab layout in JtabbedPane
Pls. if anyone know this, help me
I have a problem, my scroll tab layout policy does not working when I set it to UI.
import javax.swing.*;
import javax.swing.plaf.basic.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
publicclass Components_Areaextends JFrame{
JTabbedPane tab =new JTabbedPane(JTabbedPane.TOP,JTabbedPane.SCROLL_TAB_LAYOUT);
JLabel l[] =new JLabel[5];
public Components_Area(){
tab.setUI(new TestPlaf());
tab.addTab("Untitled",l[0]);
tab.addTab("Untitled",l[1]);
tab.addTab("Untitled",l[2]);
tab.addTab("Untitled",l[3]);
tab.addTab("Untitled",l[4]);
getContentPane().add(tab);
}
publicstaticvoid main(String args []){
Components_Area frame =new Components_Area();
frame.setSize(200,200);
frame.show();
}
staticclass TestPlafextends BasicTabbedPaneUI{
protected LayoutManager createLayoutManager(){
returnnew TestPlafLayout();
}
protected Insets getTabInsets(int tabPlacement,int tabIndex){
Insets defaultInsets = (Insets)super.getTabInsets(tabPlacement,tabIndex).clone();
defaultInsets.right += 50;
defaultInsets.top += 2;
return defaultInsets;
}
class TestPlafLayoutextends TabbedPaneLayout{
//a list of our close buttons
java.util.ArrayList closeButtons =new java.util.ArrayList();
publicvoid layoutContainer(Container parent){
super.layoutContainer(parent);
//ensure that there are at least as many close buttons as tabs
while(tabPane.getTabCount() > closeButtons.size()){
closeButtons.add(new CloseButton(closeButtons.size()));
}
tabPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
Rectangle rect =new Rectangle();
int i;
for(i = 0; i < tabPane.getTabCount();i++){
rect = getTabBounds(i,rect);
JButton closeButton = (JButton)closeButtons.get(i);
//shift the close button 3 down from the top of the pane and 20 to the left
closeButton.setLocation(rect.x+rect.width-20,rect.y+4);
closeButton.setSize(15,15);
tabPane.add(closeButton);
}
for(;i < closeButtons.size();i++){
//remove any extra close buttons
tabPane.remove((JButton)closeButtons.get(i));
}
}
// implement UIResource so that when we add this button to the
// tabbedpane, it doesn't try to make a tab for it!
class CloseButtonextends JButtonimplements javax.swing.plaf.UIResource{
public CloseButton(int index){
super(new CloseButtonAction(index));
setBorder( BorderFactory.createEmptyBorder() );
setToolTipText("Close this tab");
//remove the typical padding for the button
setMargin(new Insets(0,0,0,0));
addMouseListener(new MouseAdapter(){
publicvoid mouseEntered(MouseEvent e){
setBorder( BorderFactory.createLineBorder(Color.black, 1 ));
}
publicvoid mouseExited(MouseEvent e){
setBorder( BorderFactory.createEmptyBorder() );
}
});
}
}
class CloseButtonActionextends AbstractAction{
int index;
public CloseButtonAction(int index){
super("x");
this.index = index;
}
publicvoid actionPerformed(ActionEvent e){
tabPane.remove(index);
}
}
}
}
}
Message was edited by:
henyo

