# Wie binde ich folgendes ein? (JTabbedPane mit Button)



## JPaul (14. Mrz 2008)

Guten Abend

Bitte wendet eure Aufmerksamkeit auf die zwei Links. Was ich damit erreichen möchte ist das die Reiter schließbar sind.
Meine Frage ist wie binde ich dies nun ein?

Ich habe folgendes eingebunden:

*Internetseiten Link:*
http://java.sun.com/docs/books/tutorial/uiswing/components/tabbedpane.html

*Quellcode Link:*
http://java.sun.com/docs/books/tuto...roject/src/components/ButtonTabComponent.java


----------



## JPaul (14. Mrz 2008)

ist es dies:?


```
public void actionPerformed(ActionEvent e) {
	    int i = pane.indexOfTabComponent(new SchließbareRegisterkarten.this);
	    if (i != -1) {
	    pane.remove(i);
	    }
	}
```


----------



## PELLE (14. Mrz 2008)

beispiel von Sun:



```
public class TabSample {
  static void addIt(JTabbedPane tabbedPane, String text) {
    JLabel label = new JLabel(text);
    JButton button = new JButton(text);
    JPanel panel = new JPanel();
    panel.add(label);
    panel.add(button);
    tabbedPane.addTab(text, panel);
    tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, new JTextField(text));
  }

  public static void main(String args[]) {
    JFrame f = new JFrame("Got JTabbedPane?");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTabbedPane tabbedPane = new JTabbedPane();
    addIt(tabbedPane, "Tab One");
    addIt(tabbedPane, "Tab Two");
    addIt(tabbedPane, "Tab Three");
    addIt(tabbedPane, "Tab Four");
    addIt(tabbedPane, "Tab Five");
    f.add(tabbedPane, BorderLayout.CENTER);
    f.setSize(300, 200);
    f.setVisible(true);

  }
}
```


----------



## tutnixzursache (14. Mrz 2008)

Bei swinglabs.org gibt es entsprechende Komponenten


----------



## JPaul (14. Mrz 2008)

Danke PELLE, aber wie binde ich folgenden Code ein?? Das war meine Frage.


```
package components;

import javax.swing.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.event.*;

/**
 * Component to be used as tabComponent;
 * Contains a JLabel to show the text and 
 * a JButton to close the tab it belongs to 
 */ 
public class ButtonTabComponent extends JPanel {
    private final JTabbedPane pane;

    public ButtonTabComponent(final JTabbedPane pane) {
        //unset default FlowLayout' gaps
        super(new FlowLayout(FlowLayout.LEFT, 0, 0));
        if (pane == null) {
            throw new NullPointerException("TabbedPane is null");
        }
        this.pane = pane;
        setOpaque(false);
        
        //make JLabel read titles from JTabbedPane
        JLabel label = new JLabel() {
            public String getText() {
                int i = pane.indexOfTabComponent(ButtonTabComponent.this);
                if (i != -1) {
                    return pane.getTitleAt(i);
                }
                return null;
            }
        };
        
        add(label);
        //add more space between the label and the button
        label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
        //tab button
        JButton button = new TabButton();
        add(button);
        //add more space to the top of the component
        setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
    }

    private class TabButton extends JButton implements ActionListener {
        public TabButton() {
            int size = 17;
            setPreferredSize(new Dimension(size, size));
            setToolTipText("close this tab");
            //Make the button looks the same for all Laf's
            setUI(new BasicButtonUI());
            //Make it transparent
            setContentAreaFilled(false);
            //No need to be focusable
            setFocusable(false);
            setBorder(BorderFactory.createEtchedBorder());
            setBorderPainted(false);
            //Making nice rollover effect
            //we use the same listener for all buttons
            addMouseListener(buttonMouseListener);
            setRolloverEnabled(true);
            //Close the proper tab by clicking the button
            addActionListener(this);
        }

        public void actionPerformed(ActionEvent e) {
            int i = pane.indexOfTabComponent(ButtonTabComponent.this);
            if (i != -1) {
                pane.remove(i);
            }
        }

        //we don't want to update UI for this button
        public void updateUI() {
        }

        //paint the cross
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g.create();
            //shift the image for pressed buttons
            if (getModel().isPressed()) {
                g2.translate(1, 1);
            }
            g2.setStroke(new BasicStroke(2));
            g2.setColor(Color.BLACK);
            if (getModel().isRollover()) {
                g2.setColor(Color.MAGENTA);
            }
            int delta = 6;
            g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
            g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
            g2.dispose();
        }
    }

    private final static MouseListener buttonMouseListener = new MouseAdapter() {
        public void mouseEntered(MouseEvent e) {
            Component component = e.getComponent();
            if (component instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(true);
            }
        }

        public void mouseExited(MouseEvent e) {
            Component component = e.getComponent();
            if (component instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(false);
            }
        }
    };
}
```


----------



## Guest (14. Mrz 2008)

Ich hab es geschafft, stand doch mit dem Kopf Richtung Wand gerichtet.

Danke


----------



## C_A (18. Mai 2011)

Ich stand vor dem gleichen Problem. Das letzte Posting "Ich hab's geschafft" war für mich nicht hilfreich - mich hätte interessiert, WIE er es geschafft hat. Also, für alle, die irgendwann vor dem gleichen Problem stehen: 

```
public FS_FTP()
    {
        initComponents();
        tbbServer.add("myServer", new ServerPanel());
        tbbServer.setTabComponentAt(0, new ButtonTabComponent(tbbServer));
    }
```
tbbServer = meine JTabbedPane
ServerPanel = abgeleitet von JPanel
0 ist der Index des Tabs


----------

