package tabbed;
/*
* TabsAusblenden.java
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
public class TabsAusblenden extends JFrame{
private boolean tab2aus;
private JToggleButton btaus;
private JTabbedPane tp;
public TabsAusblenden(){
super("Tabs Ausblenden Demo");
setSize(500,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
tp = createTabbedPane(JTabbedPane.TOP);
add(tp);
btaus = new JToggleButton("Tab2 aus/einblenden");
add(btaus, BorderLayout.NORTH);
btaus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doBtAction();
}
});
}
private void doBtAction(){
tab2aus = false;
if(btaus.isSelected()){
tab2aus = true;
if(tp.getSelectedIndex() == 1){
tp.setSelectedIndex(0);
}
}
tp.revalidate();
tp.repaint();
}
private JTabbedPane createTabbedPane(final int tabPlacement){
JTabbedPane tabbedpane = new JTabbedPane();
tabbedpane.setTabPlacement(tabPlacement);
tabbedpane.setUI(new TabsUI());
tabbedpane.addTab("panel 1", new JLabel("panel 1"));
tabbedpane.addTab("panel 2", new JLabel("panel 2"));
tabbedpane.addTab("panel 3", new JLabel("panel 3"));
return tabbedpane;
}
private class TabsUI extends BasicTabbedPaneUI{
public TabsUI(){
super();
}
protected int calculateTabWidth(final int tabPlacement, final int tabIndex,
final FontMetrics metrics){
int width = super.calculateTabWidth(tabPlacement,tabIndex,metrics);
if(tab2aus && tabIndex == 1){
width = 0;
}
return width;
}
}
public static void main(final String[] args){new TabsAusblenden().setVisible(true);}
}