2010-11-21 7 views

Respuesta

6

Yo sugeriría que mira sobre el tutorial de, How to Use Tabbed Panes y vaya a la sección titulada aquí con componentes personalizados.

Además, si observa el enlace example index dentro de esa sección, se proporciona el código de muestra.

3

De hecho, acabo de crear una implementación de esto yo mismo. :)

Algo como esto:

/* These need to be final so you can reference them in the MouseAdapter subclass 
* later. I personally just passed them to a method to add the tab, with the 
* parameters marked as final. 
* i.e., public void addCloseableTab(final JTabbedPane tabbedPane, ...) 
*/ 
final Component someComponent = ...; //Whatever component is being added 
final JTabbedPane tabbedPane = new JTabbedPane(); 
//I had my own subclass of AbstractButton, but that's irrelevant in this case 
JButton closeButton = new JButton("x"); 

/* 
* titlePanel is initialized containing a JLabel with the tab title, 
* and closeButton. (I don't recall the tabbed pane showing a title itself after 
* setTabComponentAt() is called) 
*/ 
JPanel titlePanel = ...; 
tabbedPane.add(someComponent); 
tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(someComponent), titlePanel); 

closeButton.addMouseListener(new MouseAdapter() { 
    @Override 
    public void mouseClicked(MouseEvent e) { 
     tabbedPane.remove(someComponent); 
    } 
}); 
+0

Ah, y un posible inconveniente, setTabComponentAt() sólo está disponible a partir de Java 6 ... así que esto no funcionará en versiones anteriores. – swilliams

Cuestiones relacionadas