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

Composants Java Discussion :

comment faire un jTree transparent


Sujet :

Composants Java

  1. #1
    Membre éclairé Avatar de ABN84
    Inscrit en
    Octobre 2007
    Messages
    410
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Octobre 2007
    Messages : 410
    Par défaut comment faire un jTree transparent
    bonjour,
    je cherche à avoir un jTree Transparent. "jTree1.setOpaque(false)", ne marche pas. quelcun saurait-il commen je pourrais m'y prendre svp?
    merci

  2. #2
    Membre chevronné Avatar de xixi31
    Inscrit en
    Juin 2005
    Messages
    423
    Détails du profil
    Informations personnelles :
    Âge : 44

    Informations forums :
    Inscription : Juin 2005
    Messages : 423
    Par défaut
    qu'est-ce que tu entends par transparent?

  3. #3
    Membre éclairé Avatar de ABN84
    Inscrit en
    Octobre 2007
    Messages
    410
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Octobre 2007
    Messages : 410
    Par défaut
    bonjour,
    j'aimerais arriver à ce type de rendu:

    le Fond de ce tree n'est pas unicolor(j'ai tronqué l'image), c'est une image.
    ne pouvant pas mettre une image en arriere plan d'un jTree, mon idée est de mettre le jTree dans jPanel, de mette l'image en arriere plan du panel, et de faire en sotre que jTree soit transparent afin de laisser voir le jPanel.
    j'arrive à rendre transparent un jPanel, un jButton, un jTextField avec setOpaque(false), mais avec un jTree, le setOpaque(false) ne marche apparemment pas.
    qu'est-ce que tu entends par transparent?
    en gros il laisse voir ce qui est derriere

  4. #4
    Membre chevronné Avatar de xixi31
    Inscrit en
    Juin 2005
    Messages
    423
    Détails du profil
    Informations personnelles :
    Âge : 44

    Informations forums :
    Inscription : Juin 2005
    Messages : 423
    Par défaut
    j'imagine que tu as mis ton JTree dans un JScrollPane. Donc, tu as un JScrollPane qui contient un JViewport qui contient ton JTree (schématiquement).
    C'est peut-être bourrin mais tu as essayé de rendre le JScrollPane et le viewport non opaques?

  5. #5
    Membre éclairé Avatar de ABN84
    Inscrit en
    Octobre 2007
    Messages
    410
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Octobre 2007
    Messages : 410
    Par défaut
    je n'ai pas mis le tree directement dans le srollpane mais dans un panel et le panel dans le scrollpane.
    dans mes essais, j'ai meme mis tous les composants en non opaque, mais il restait tjrs que le fond du jtree restait blanc.

  6. #6
    Membre chevronné Avatar de xixi31
    Inscrit en
    Juin 2005
    Messages
    423
    Détails du profil
    Informations personnelles :
    Âge : 44

    Informations forums :
    Inscription : Juin 2005
    Messages : 423
    Par défaut
    tu peux fournir un peu de code que je pourrais éxécuter simplement histoire de pouvoir t'aider plus efficacement?

  7. #7
    Membre éclairé Avatar de ABN84
    Inscrit en
    Octobre 2007
    Messages
    410
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Octobre 2007
    Messages : 410
    Par défaut
    tout la methode initcomponent est generée par l'IDE.
    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
    /*
     * jGMSHControlTree.java
     *
     * Created on 14 octobre 2007, 07:18
     */
     
    package jGMSHPack;
     
    import java.awt.Graphics;
    import java.awt.Image;
    import java.io.*;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
     
     
    public class jControlTree extends javax.swing.JPanel {
     
        /** Creates new form jGMSHControlTree */
        public jControlTree() {
            initComponents();
        }
     
        // <editor-fold desc="Background">
        public Icon getBackGround() {
            return this.backgroundic;
        }
     
        public void setBackGround(Icon backgroundic) {
            this.backgroundic=backgroundic;
            backgroundim=((ImageIcon)backgroundic).getImage();
        }
     
        public void paintComponent(Graphics g)
        {
    	g.drawImage(backgroundim,0,50,GeometryTree.getSize().width,GeometryTree.getSize().height,GeometryTree);
        }// </editor-fold>
     
     
     
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            jTabbedPane = new javax.swing.JTabbedPane();
            GeometryPanel = new javax.swing.JPanel();
            GeometryScrollPane = new javax.swing.JScrollPane();
            GeometryTree = new javax.swing.JTree();
            MeshPanel = new javax.swing.JPanel();
            MeshScrollPane = new javax.swing.JScrollPane();
            MeshTree = new javax.swing.JTree();
            SolutionPanel = new javax.swing.JPanel();
            SolutionScrollPane = new javax.swing.JScrollPane();
            SolutionTree = new javax.swing.JTree();
     
            GeometryScrollPane.setOpaque(false);
     
            GeometryTree.setOpaque(false);
            GeometryScrollPane.setViewportView(GeometryTree);
     
            javax.swing.GroupLayout GeometryPanelLayout = new javax.swing.GroupLayout(GeometryPanel);
            GeometryPanel.setLayout(GeometryPanelLayout);
            GeometryPanelLayout.setHorizontalGroup(
                GeometryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(GeometryScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
            );
            GeometryPanelLayout.setVerticalGroup(
                GeometryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(GeometryScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
            );
     
            jTabbedPane.addTab("", new javax.swing.ImageIcon(getClass().getResource("/images/geometry.PNG")), GeometryPanel, "Geometry Manager"); // NOI18N
     
            MeshScrollPane.setOpaque(false);
     
            MeshTree.setOpaque(false);
            MeshScrollPane.setViewportView(MeshTree);
     
            javax.swing.GroupLayout MeshPanelLayout = new javax.swing.GroupLayout(MeshPanel);
            MeshPanel.setLayout(MeshPanelLayout);
            MeshPanelLayout.setHorizontalGroup(
                MeshPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(MeshScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
            );
            MeshPanelLayout.setVerticalGroup(
                MeshPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(MeshScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
            );
     
            jTabbedPane.addTab("", new javax.swing.ImageIcon(getClass().getResource("/images/mesh.PNG")), MeshPanel, "Mesh Manager"); // NOI18N
     
            SolutionScrollPane.setOpaque(false);
     
            SolutionTree.setOpaque(false);
            SolutionScrollPane.setViewportView(SolutionTree);
     
            javax.swing.GroupLayout SolutionPanelLayout = new javax.swing.GroupLayout(SolutionPanel);
            SolutionPanel.setLayout(SolutionPanelLayout);
            SolutionPanelLayout.setHorizontalGroup(
                SolutionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(SolutionScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
            );
            SolutionPanelLayout.setVerticalGroup(
                SolutionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(SolutionScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
            );
     
            jTabbedPane.addTab("", new javax.swing.ImageIcon(getClass().getResource("/images/solve.PNG")), SolutionPanel, "Solution Manager"); // NOI18N
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
            );
        }// </editor-fold>
     
     
        // Variables declaration - do not modify
        private javax.swing.JPanel GeometryPanel;
        private javax.swing.JScrollPane GeometryScrollPane;
        private javax.swing.JTree GeometryTree;
        private javax.swing.JPanel MeshPanel;
        private javax.swing.JScrollPane MeshScrollPane;
        private javax.swing.JTree MeshTree;
        private javax.swing.JPanel SolutionPanel;
        private javax.swing.JScrollPane SolutionScrollPane;
        private javax.swing.JTree SolutionTree;
        private javax.swing.JTabbedPane jTabbedPane;
        // End of variables declaration
     
        Image backgroundim;
        Icon backgroundic;
     
     
    }

  8. #8
    Membre chevronné Avatar de xixi31
    Inscrit en
    Juin 2005
    Messages
    423
    Détails du profil
    Informations personnelles :
    Âge : 44

    Informations forums :
    Inscription : Juin 2005
    Messages : 423
    Par défaut
    les images en plus, ca aurait été cool

  9. #9
    Membre chevronné Avatar de xixi31
    Inscrit en
    Juin 2005
    Messages
    423
    Détails du profil
    Informations personnelles :
    Âge : 44

    Informations forums :
    Inscription : Juin 2005
    Messages : 423
    Par défaut
    excuse moi, mais dans ton exemple, pour le premier onglet, tu veux voir en fond la couleur de fond du panneau GeometryPanel, c'est ca?

  10. #10
    Membre éclairé Avatar de ABN84
    Inscrit en
    Octobre 2007
    Messages
    410
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Octobre 2007
    Messages : 410
    Par défaut
    la classe en question se trouve ici : jGMSHPack\jControlTree.java :
    http://fs04n4.sendspace.com/dl/d1864...f3clja/src.zip

  11. #11
    Membre éclairé Avatar de ABN84
    Inscrit en
    Octobre 2007
    Messages
    410
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Octobre 2007
    Messages : 410
    Par défaut
    ce que j'ai fait pour l'instant, c'est mettre tous les composants en transparent.
    l'image images\background.png est en arriere plan dans le jPanel principal. je l'ai decalé de 5à pixel vers le bas qui est la hauteur des onglets. ce qui fait que tous les composant apparaiisnet comme s'ils avaient cette image en arriere plan si ce n'etait des jTrees qui restent opaques.
    je crois que j'ai dis une grosse betise tou à l'heure:
    je n'ai pas mis le tree directement dans le srollpane mais dans un panel et le panel dans le scrollpane.
    je jTree est un dans un jScrollPane. et le ScrollPane dans un jPanel
    excuse moi, mais dans ton exemple, pour le premier onglet, tu veux voir en fond la couleur de fond du panneau GeometryPanel, c'est ca?

  12. #12
    Membre chevronné Avatar de xixi31
    Inscrit en
    Juin 2005
    Messages
    423
    Détails du profil
    Informations personnelles :
    Âge : 44

    Informations forums :
    Inscription : Juin 2005
    Messages : 423
    Par défaut
    ce qui est embetant c'est que matisse ne te montrera pas forcément le bon résultat :

    ajoute ces deux lignes après le initComponents();

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
        this.GeometryScrollPane.setOpaque(false);
        this.GeometryScrollPane.getViewport().setOpaque(false);
    et crée toi une classe du style :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
     
        public static void main(String[] args)
        {
        javax.swing.JFrame frame = new javax.swing.JFrame();
        frame.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
     
        frame.getContentPane().add(new jControlTree());
     
        frame.setSize(400, 400);
        frame.setVisible(true);
        }
    Pour t'assurer que ce que tu vois en fin est bien GeometryPanel, mets son background en jaune.

  13. #13
    Membre Expert Avatar de herve91
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    1 282
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 282
    Par défaut
    Je pense que le code suivant devrait répondre à ton besoin :
    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
        static class TransparentCellRenderer extends DefaultTreeCellRenderer {
     
            public TransparentCellRenderer() {
                setBackgroundNonSelectionColor(null);
                setBackground(null);
            }
     
            public Component getTreeCellRendererComponent(JTree tree, Object value,
                    boolean sel, boolean expanded, boolean leaf, int row,
                    boolean hasFocus) {
                tree.setBackground(null);
                return super.getTreeCellRendererComponent(tree, value, sel, expanded,
                        leaf, row, hasFocus);
            }
        }
     
     
        MeshTree.setCellRenderer(new TransparentCellRenderer());

  14. #14
    Membre chevronné Avatar de xixi31
    Inscrit en
    Juin 2005
    Messages
    423
    Détails du profil
    Informations personnelles :
    Âge : 44

    Informations forums :
    Inscription : Juin 2005
    Messages : 423
    Par défaut
    effectivement, une fois ton problème d'opacité réglé sur les parents de ton JTree, il faudra sans doute que tu utilises des composants non opaque en guise de renderers (idem pour les éditeurs).

  15. #15
    Membre éclairé Avatar de ABN84
    Inscrit en
    Octobre 2007
    Messages
    410
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Octobre 2007
    Messages : 410
    Par défaut
    j'ai reglé le probleme d'opacité.
    il manquait cette ligne:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
        this.GeometryScrollPane.getViewport().setOpaque(false);
    à present à travers le tree je vois l'arriere plan du panel.
    pour mettre un image dans un jPanel j'utilisais ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
        public void paintComponent(Graphics g){
    	g.drawImage(backgroundim,0,0,this.getSize().width,this.getSize().height,null);
        }
    ecrit de cette facon ça ne peut fonctionner que pour le panel parent:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    public class jControlTree extends javax.swing.JPanel {
    j'ai donc essayé ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
        public void paintComponent(Graphics g){
    	g=GeometryPanel.getGeometry();
            g.drawImage(backgroundim,0,0,this.getSize().width,this.getSize().height,GeometryPanel);
        }
    cette fois, l'image est bien affichée mais elle cache l'arboressence du tree, et si je change l'onglet et retourne, l'image disparait. j'en conclut donc, qu'il s'ajit d'un aleas et que ce n'est pas la bonne syntaxe pour mettre l'image dans GeometryPanel

  16. #16
    Membre éclairé Avatar de ABN84
    Inscrit en
    Octobre 2007
    Messages
    410
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Octobre 2007
    Messages : 410
    Par défaut
    bonjour,
    j'y suis arrive en changeant les 3 jPanels (GeometryPanel, MeshPanel, et Solution Panel) par un element IPanel qui n'est autre qu'un jPanel auxquel j'ai ajouté ces lignes:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
        // <editor-fold desc="Background">
        public Icon getBackGround() {
            return this.icon;
        }
     
        public void setBackGround(Icon icon) {
            this.icon=icon;
            image=((ImageIcon)icon).getImage();
        }
     
        public void paintComponent(Graphics g)
        {
    	g.drawImage(image,0,0,this.getSize().width,this.getSize().height,null);
        }// </editor-fold>
    j'obtiens donc ce rendu:

    la question que je me pose à present est si est possible que les sones ou est ecrit le texte soient aussi transparents?
    merci

  17. #17
    Membre Expert Avatar de herve91
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    1 282
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 282
    Par défaut
    As-tu lu mon message plus haut ???

  18. #18
    Membre éclairé Avatar de ABN84
    Inscrit en
    Octobre 2007
    Messages
    410
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Octobre 2007
    Messages : 410
    Par défaut
    oui,
    ça rend le tree transparent, mais pas les champs de textes

  19. #19
    Membre Expert Avatar de herve91
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    1 282
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 1 282
    Par défaut
    Tu parles bien de ce code là ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    MeshTree.setCellRenderer(new TransparentCellRenderer());
    Peut-être cela dépend-il du look&feel.

  20. #20
    Membre éclairé Avatar de ABN84
    Inscrit en
    Octobre 2007
    Messages
    410
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Octobre 2007
    Messages : 410
    Par défaut
    oui, c'est bien de celui la que je parle.
    Peut-être cela dépend-il du look&feel.
    tu veux dire que selon le PC ça va se comporter autrement?

Discussions similaires

  1. comment faire un background transparent?
    Par guigui1005 dans le forum Composants
    Réponses: 5
    Dernier message: 17/05/2012, 22h45
  2. Réponses: 7
    Dernier message: 18/12/2008, 11h20
  3. Comment faire un TDBCheckBox transparent ?
    Par dd16 dans le forum Composants VCL
    Réponses: 1
    Dernier message: 03/06/2008, 16h39
  4. Réponses: 1
    Dernier message: 09/06/2006, 11h42
  5. comment faire un Dialog Box demi-transparent?
    Par Zorgz dans le forum MFC
    Réponses: 3
    Dernier message: 08/01/2004, 15h02

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