IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

[JTabbedPane] close sur onglet


Sujet :

AWT/Swing Java

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Février 2006
    Messages
    198
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2006
    Messages : 198
    Points : 85
    Points
    85
    Par défaut [JTabbedPane] close sur onglet
    bonjour!!

    est-il possible de rendre un onglet closable?

    merci

  2. #2
    Membre éclairé Avatar de sozie9372
    Inscrit en
    Mai 2005
    Messages
    713
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Mai 2005
    Messages : 713
    Points : 724
    Points
    724
    Par défaut
    Yo !
    Pour supprimer un onglet :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    mesOnglets.removeTabAt(i);
    Après tu peux rajouter un bouton de fermeture a ton onglet qui appelle cette méthode dans le conteneur d'onglets.
    +++
    Ju

  3. #3
    Expert éminent sénior
    Avatar de Baptiste Wicht
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2005
    Messages
    7 431
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2005
    Messages : 7 431
    Points : 21 324
    Points
    21 324
    Par défaut
    Tu peut regarder le framework VlDocking, tu peut faire des onglets fermables avec ca

  4. #4
    Membre régulier
    Profil pro
    Inscrit en
    Février 2006
    Messages
    198
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2006
    Messages : 198
    Points : 85
    Points
    85
    Par défaut
    merci beucoup!

  5. #5
    Expert éminent sénior
    Avatar de Baptiste Wicht
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2005
    Messages
    7 431
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2005
    Messages : 7 431
    Points : 21 324
    Points
    21 324
    Par défaut
    Mais derien,

    par contre, quand tu l'auras testé, tu pourras me dire ce qu'il vaut, car je l'ai encore jamais essayé ?

  6. #6
    Membre régulier
    Profil pro
    Inscrit en
    Février 2006
    Messages
    198
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2006
    Messages : 198
    Points : 85
    Points
    85
    Par défaut
    lol
    j'ai regardé un peu, ça m'a l'air super compliqué!!

  7. #7
    Expert éminent sénior
    Avatar de Baptiste Wicht
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2005
    Messages
    7 431
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2005
    Messages : 7 431
    Points : 21 324
    Points
    21 324
    Par défaut
    lol dommage, mais persevere, les résultats ont l'air super

  8. #8
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    1
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 1
    Points : 1
    Points
    1
    Par défaut
    Mais non c'est pas super compliqué !

    C'est juste que rendre simple un concept complexe demande une "approche". Une fois qu'on a compris l'approche, le reste vient assez naturellement (enfin c'est ce que me reportent les utilisateurs, qui sont très satisfaits du produit).

    Pour démarrer, vous pouvez commencer par définir un workspace avec l'application Worskpace Editor (Java Web Start, depuis la page du produit) : ça crée un petit fichier XML décrivant le layout du workspace.

    Ensuite, il suffit d'enregistrer vos Dockables auprès du composant central (DockingDesktop), puis de charger le workspace (un Dockable c'est juste un JComponent qui implémente une petite interface, ou bien un wrapper de component : DockablePanel)

    Le tout doit prendre environ 10 lignes de code pour une version de base.

    desk.registerDockable(myDockable1);
    ..
    desk.registerDockable(myDockableN);
    desk.readXML(getClass().getResourceAsStream("workspace.xml");

    Une fois l'approche maîtrisée, vous pourrez passer à des chose plus amusantes, suivant vos besoins...

    Et puis, je suis toujours prêt à aider les débutants (le produit est open source, c'est pratique pour commencer).

    A votre disposition pour plus de précisions !

    Lilian Chamontin, VLSolutions
    ("l'auteur")

  9. #9
    Membre régulier
    Profil pro
    Inscrit en
    Février 2006
    Messages
    198
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2006
    Messages : 198
    Points : 85
    Points
    85
    Par défaut
    moi je veux juste mettre une croix sur un onglet, et je ne cromprend pas qu'est ce que les dockables

  10. #10
    Gfx
    Gfx est déconnecté
    Expert éminent
    Avatar de Gfx
    Inscrit en
    Mai 2005
    Messages
    1 770
    Détails du profil
    Informations personnelles :
    Âge : 42

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 770
    Points : 8 178
    Points
    8 178
    Par défaut
    Tu peux utiliser la nouvelle methode setTabComponentAt() de Java SE 6 Sinon il faut en effet le faire soi-meme, et c'est pas simple, ou utiliser une bibliotheque comme celle citee dans cette discussion.

  11. #11
    Membre du Club Avatar de bubu56
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    58
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 58
    Points : 65
    Points
    65
    Par défaut
    voici un closeable tabbedpane :

    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
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
     
    /*
     
    nextobjects Copyright (C) 2001-2005 Emmanuel Florent
     
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by the
    Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.
     
    This program is distributed in the hope that it will
    be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
    of MERCHANTABILITY or FITNESS FOR A PARTICULAR
    PURPOSE. See the GNU General Public License for more details.
     
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc., 59
    Temple Place - Suite 330, Boston, MA 02111-1307, USA.
     
    */
     
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
     
    import javax.swing.Icon;
    import javax.swing.JTabbedPane;
     
    /**
     * A JTabbedPane which has a close ('X') icon on each tab.
     *
     * To add a tab, use the method addTab(String, Component)
     *
     * To have an extra icon on each tab  use
     * the method addTab(String, Component, Icon).
     * Only clicking the 'X' closes the tab.
     */
     
    public class ClosableTabbedPane extends JTabbedPane implements MouseListener
    {
     /**
      * constructor
      */
     public ClosableTabbedPane(EdtMainFrame mainFrame)
     {
      super();
      this.mainFrame = mainFrame;
      addMouseListener(this);
     }
     
     
     /**
      * add a tabbed pane
      * @param component the component
      * @param title the title
      */
     public void addTab(String title, Component component)
     {
      this.addTab(title, component, null);
     }
     /**
      * add a tabbed pane
      * @param title title
      * @param component component
      * @param extraIcon icon
      */
     public void addTab(String title, Component component, Icon extraIcon)
     {
      super.addTab(title, new CloseTabIcon(extraIcon), component);
      bubu.ui.UITools.setBackground(this, EdtMainFrame.AppColor);
     }
     /**
      * What to do when mouse clicked
      * @param e <code>MouseEvent</code>
      */
     public void mouseClicked(MouseEvent e)
     {
      int tabNumber = getUI().tabForCoordinate(this, e.getX(), e.getY());
      if (tabNumber < 0)
       return;
     
      Rectangle rect = ((CloseTabIcon) getIconAt(tabNumber)).getBounds();
      if (rect.contains(e.getX(), e.getY()))
      {
          mainFrame.tabs.removeObject(this.getComponentAt(tabNumber));
          this.remove(tabNumber);
          System.gc();
      }
     }
     /**
      * What to di when mouse entered 
      * actually do nothing
      * @param e <code>MouseEvent</code>
      */
     public void mouseEntered(MouseEvent e)
     {
     }
     /**
      * What to di when mouse exited 
      * actually do nothing
      * @param e <code>MouseEvent</code>
      */
     public void mouseExited(MouseEvent e)
     {
     }
     /**
      * What to di when mouse pressed
      * actually do nothing
      * @param e <code>MouseEvent</code>
      */
     public void mousePressed(MouseEvent e)
     {
     }
     /**
      * What to di when mouse released 
      * actually do nothing
      * @param e <code>MouseEvent</code>
      */
     public void mouseReleased(MouseEvent e)
     {
     }
     
     EdtMainFrame mainFrame;
    }
     
    /**
     * The class which generates the 'X' icon for the tabs. The constructor
     * accepts an icon which is extra to the 'X' icon, so you can have tabs
     * like in JBuilder. This value is null if no extra icon is required.
     */
    class CloseTabIcon implements Icon
    {
     /**
      *  X position
      */
     private int x_pos;
     
     /**
      *  Y position
      */
     private int y_pos;
     
     /**
      *  Width
      */
     private int width;
     
     /**
      *  Height
      */
     private int height;
     /**
      *  Icon
      */
     private Icon fileIcon;
     
     /**
      * CloseTabIcon
      * @param fileIcon the file icon
      */
     public CloseTabIcon(Icon fileIcon)
     {
      this.fileIcon = fileIcon;
      width = 16;
      height = 16;
     }
     
     /**
      *  Paint icon
      * @param c component
      * @param g graphics g
      * @param x x
      * @param y y
      */
     public void paintIcon(Component c, Graphics g, int x, int y)
     {
      this.x_pos = x;
      this.y_pos = y;
     
      Color col = g.getColor();
     
      g.setColor(Color.black);
      int y_p = y + 2;
      g.drawLine(x + 1, y_p, x + 12, y_p);
      g.drawLine(x + 1, y_p + 13, x + 12, y_p + 13);
      g.drawLine(x, y_p + 1, x, y_p + 12);
      g.drawLine(x + 13, y_p + 1, x + 13, y_p + 12);
      g.drawLine(x + 3, y_p + 3, x + 10, y_p + 10);
      g.drawLine(x + 3, y_p + 4, x + 9, y_p + 10);
      g.drawLine(x + 4, y_p + 3, x + 10, y_p + 9);
      g.drawLine(x + 10, y_p + 3, x + 3, y_p + 10);
      g.drawLine(x + 10, y_p + 4, x + 4, y_p + 10);
      g.drawLine(x + 9, y_p + 3, x + 3, y_p + 9);
      g.setColor(col);
      if (fileIcon != null)
      {
       fileIcon.paintIcon(c, g, x + width, y_p);
      }
     }
     /**
      * get the icon width
      * @return the width
      */
     public int getIconWidth()
     {
      return width + (fileIcon != null ? fileIcon.getIconWidth() : 0);
     }
     /**
      * get icon height
      * @return height 
      */
     public int getIconHeight()
     {
      return height;
     }
     /**
      * get the boundaries
      * @return bounds
      */
     public Rectangle getBounds()
     {
      return new Rectangle(x_pos, y_pos, width, height);
     }
    }

  12. #12
    Membre du Club Avatar de bubu56
    Profil pro
    Inscrit en
    Octobre 2005
    Messages
    58
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2005
    Messages : 58
    Points : 65
    Points
    65
    Par défaut
    pour l'utiliser dans une jframe avec un borderlayout par exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    ttTabbedPane=new ClosableTabbedPane(this);
    getContentPane().add(ttTabbedPane, java.awt.BorderLayout.CENTER);
     
    //puis tu ajoutes des tabs à ton ttTabbedPane
    ttTabbedPane.add("nom",jpanel);

  13. #13
    Membre régulier
    Profil pro
    Inscrit en
    Février 2006
    Messages
    198
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2006
    Messages : 198
    Points : 85
    Points
    85
    Par défaut
    merci beucoup

    mais...

    qu'est ce que EdtMainFrame et bubu??

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    public void addTab(String title, Component component, Icon extraIcon) 
     { 
      super.addTab(title, new CloseTabIcon(extraIcon), component); 
      bubu.ui.UITools.setBackground(this, EdtMainFrame.AppColor); 
     }

  14. #14
    Membre régulier
    Profil pro
    Inscrit en
    Février 2006
    Messages
    198
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2006
    Messages : 198
    Points : 85
    Points
    85
    Par défaut
    une idée?

  15. #15
    Expert éminent sénior
    Avatar de Baptiste Wicht
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2005
    Messages
    7 431
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2005
    Messages : 7 431
    Points : 21 324
    Points
    21 324
    Par défaut
    Ca doit etre des classes spéciales de bubu56 je pense, mais je suis pas sur...

  16. #16
    Membre émérite
    Avatar de xavlours
    Inscrit en
    Février 2004
    Messages
    1 832
    Détails du profil
    Informations forums :
    Inscription : Février 2004
    Messages : 1 832
    Points : 2 410
    Points
    2 410
    Par défaut
    Oui, je crois que tu n'as pas besoin de cette ligne de code.

  17. #17
    Membre du Club
    Inscrit en
    Février 2006
    Messages
    64
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 64
    Points : 50
    Points
    50
    Par défaut Très simple
    Bonjour,
    je sais que c'est en retard mais cette solution peut aider d'autre java programmer,

    En fête, le pricincipe est hyper simple, c'est ajouter un évènement MouseReleased au objet tabbedpane comme suit :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
         monTabbedPane.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                if(evt.getClickCount()==2)
                 {
                monTabbedPane.remove(monTabbedPane.getSelectedComponent());
                 }
                 }
             });
    l'idée est qu'en faisant double clique sur le tabbedpane l'onglet selectionné sera fermé.

Discussions similaires

  1. Réponses: 10
    Dernier message: 14/11/2013, 20h31
  2. [JTabbedPane] changer d'onglet sur clic d'un bouton
    Par lazzeroni dans le forum AWT/Swing
    Réponses: 8
    Dernier message: 26/07/2006, 16h58
  3. JTabbedPane & JPopupMenu sur un onglet
    Par ®om dans le forum AWT/Swing
    Réponses: 1
    Dernier message: 21/07/2006, 14h50
  4. [JTabbedPane] Action sur clik d'onglet
    Par Sirkiller dans le forum AWT/Swing
    Réponses: 2
    Dernier message: 08/06/2006, 15h20
  5. [jTabbedPane] action apres click sur onglet
    Par tony_big_guy dans le forum Agents de placement/Fenêtres
    Réponses: 2
    Dernier message: 01/12/2005, 23h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo