Cómo modificamos el componente JTabbedPane para agregarle un botón con una X para cerrar la pestaña.
Clase Principal
package Clases;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.UnsupportedLookAndFeelException;
public class Principal extends JApplet {
JTabbedPane pestañas;
public void init(){
colocarSkin();
pestañas=new JTabbedPane();
colocarPestañasPredeterminadas();
JPanel pboton=new JPanel();
JButton restaurar=new JButton("Restaurar");
restaurar.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
for(int i=0;i<pestañas.getTabCount();i++){
pestañas.removeTabAt(i);
}
colocarPestañasPredeterminadas();
}
});
pboton.add(restaurar);
add(pestañas);
add(pboton,BorderLayout.SOUTH);
}
public void colocarPestañasPredeterminadas() {
JPanel p1=new JPanel();
p1.add(new JLabel("Java Zone"));
JPanel p2=new JPanel();
p2.add(new JLabel("JTabbedPane con X como imagen"));
JPanel p3=new JPanel();
p3.add(new JLabel("JTabbedPane con X dibujada"));
PanelTab btc=new PanelTab(pestañas,0);
PanelTab btc2=new PanelTab(pestañas,1);
PanelTab btc3=new PanelTab(pestañas,0);
pestañas.addTab("Prueba", p1);
pestañas.addTab("Prueba Java Zone", p2);
pestañas.addTab("Prueba Java Zone", p3);
pestañas.setTabComponentAt(0, btc);
pestañas.setTabComponentAt(1, btc2);
pestañas.setTabComponentAt(2, btc3);
}
public void colocarSkin(){
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
}
Clase PanelTab
package Clases;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
public class PanelTab extends JPanel {
JTabbedPane pestañas;
int tipo;
public PanelTab(JTabbedPane p,int op) {
if (p != null) {
this.pestañas = p;
tipo=op;
setOpaque(false);
JLabel titulo = new JLabel() {
public String getText() {
int i = pestañas.indexOfTabComponent(PanelTab.this);
if (i != -1) {
return pestañas.getTitleAt(i);
}
return null;
}
};
add(titulo);
titulo.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
JButton button = new BotonX(pestañas,this,tipo);
add(button);
}
}
}
Clase BotonX
package Clases;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
public class BotonX extends JButton implements MouseListener {
JTabbedPane panel;
PanelTab btc;
int tipo;
public BotonX(JTabbedPane pane,PanelTab btc,int op) {
panel=pane;
this.btc=btc;
tipo=op;
int size = 17;
setPreferredSize(new Dimension(size, size));
setToolTipText("Cerrar Pestaña");
setUI(new BasicButtonUI());
setContentAreaFilled(false);
setFocusable(false);
setBorder(BorderFactory.createEtchedBorder());
setBorderPainted(false);
addMouseListener(this);
setRolloverEnabled(true);
addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int i = panel.indexOfTabComponent(BotonX.this.btc);
if (i != -1) {
panel.remove(i);
}
}
});
}
public void updateUI() {
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
if(tipo==0){
if (getModel().isPressed()) {
g2.translate(1, 1);
}
g2.setStroke(new BasicStroke(3));
g2.setColor(Color.BLACK);
if (getModel().isRollover()) {
g2.setColor(Color.RED);
}
g2.drawLine(5, 5, 12, 12);
g2.drawLine(12, 6, 6, 12);
g2.dispose();
}else{
ImageIcon img=new ImageIcon(this.getClass().getResource("../lib/cerrar.png"));
g2.drawImage(img.getImage(), 1, ,14,14, this);
g2.dispose();
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
Component component = e.getComponent();
if (component instanceof AbstractButton) {
AbstractButton button = (AbstractButton) component;
button.setBorderPainted(true);
}
}
@Override
public void mouseExited(MouseEvent e) {
Component component = e.getComponent();
if (component instanceof AbstractButton) {
AbstractButton button = (AbstractButton) component;
button.setBorderPainted(false);
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
visto en El Rincon de tu casa
No hay comentarios:
Publicar un comentario