après avoir cherché et trouvé et trouvé à plusieurs solutions pas toujours très simple j'ai décidé de m'attaquer un peut au sujet et de faire un mix de ce que j'ai trouvé pour avoir un JTabbedPane avec bouton fermé

voici le code

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
 
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTabbedPane;
 
 
/**
 *
 * @author  alain
 */
public class JTabbedPaneWithCloseIcons extends javax.swing.JTabbedPane{
    JTabbedPaneWithCloseIcons moi; //pour avoir acces au l'element this dans les class internes
	public JTabbedPaneWithCloseIcons() {
		super();
        moi =this;
	}
 
	public void addTab(String title, Component component,int endroit) {
            super.addTab(title, component); //on ajoute une Tab à JTabbedPane
            super.setTabComponentAt(endroit, new CloseTabPanel(title)); //on applique le closeTabPanel a l'element "endroit"
	}
 
		//fonction qui permet d'affiché le bouton close
        public void afficheIconAt(int endroit){
            ((CloseTabPanel)moi.getTabComponentAt(endroit)).afficheIcon(true);
        }
		//fonction qui permet d'enlever le bouton close
        public void cacheIconAt(int endroit){
            ((CloseTabPanel)moi.getTabComponentAt(endroit)).afficheIcon(false);
        }
 
 
 
class CloseTabPanel extends JPanel{
        JButton button; 
 
	//constructeur sans boolean  qui de base met un bouton close
    public CloseTabPanel(String titre) {
            super(new FlowLayout(FlowLayout.LEFT, 0, 0));
            setOpaque(false);
            JLabel label = new JLabel(titre);
            add(label);
            button = new TabButton();
            add(button);
            setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
	}
	//constructeur avec boolean  qui permet de choisir si oui ou non on veux un bouton close
		public CloseTabPanel(String titre, boolean b){
            super(new FlowLayout(FlowLayout.LEFT, 0, 0));
            setOpaque(false);
            JLabel label = new JLabel(titre);
            add(label);
            button = new TabButton();
            if(b){
            add(button);
            }
            //add more space to the top of the component
            setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
        }
        //permet d'afficher ou cacher le bouton close
        public void afficheIcon(boolean b){
            if(b){
                if(this.getComponentCount()==1)
                    this.add(button);
            }else{
                if(this.getComponentCount()>1)
                    this.remove(button);
            }
        }
}
class TabButton extends JButton implements ActionListener {
    public TabButton() {
            int size = 17;
            setPreferredSize(new Dimension(size, size));
            setToolTipText("Fermer cet onglet");
            //Make the button looks the same for all Laf's
            setUI(new BasicButtonUI());
            //Rends le bouton transparent
            setContentAreaFilled(false);
            //pas besoin d'avoir le focus
            setFocusable(false);
            setBorder(BorderFactory.createEtchedBorder());
            setBorderPainted(false);
            addActionListener(this);            
        }
		/*
		* fonction qui ferme l'onglet du bouton close sur lequel on a cliqué
		*/
    public void actionPerformed(ActionEvent e) {
            int X = new Double(((JButton)e.getSource()).getMousePosition().getX()).intValue();
            int Y = new Double(((JButton)e.getSource()).getMousePosition().getY()).intValue();
 
            int i = moi.getUI().tabForCoordinate((JTabbedPane)moi, X,Y);
            if (i != -1) {
                moi.remove(i);
            }
        }
 
        //we don't want to update UI for this button
        public void updateUI() {
        }
 
        //dessine la croix dans le bouton
        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();
        }
    }
}
peut etre qu'il y a des optimisation possible, mais au moins c'est pas trop trop compliquer à comprendre