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

Interfaces Graphiques en Java Discussion :

Click button : Mon code marche mais l'affichage ne suis pas !


Sujet :

Interfaces Graphiques en Java

  1. #1
    Inactif  
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    497
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 497
    Points : 312
    Points
    312
    Par défaut Click button : Mon code marche mais l'affichage ne suis pas !
    Bonjour, je fais une petite application Jframe, et j'ai ce 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
    
     boolean  etat = false; // Panel Proprio
        private void click(java.awt.event.MouseEvent evt) {                       
            // TODO add your handling code here:
            if(etat){
                jPanel1.setSize(new Dimension(jPanel1.getSize().width,120)); //120
                etat = false;
            }else {
                jPanel1.setSize(new Dimension(jPanel1.getSize().width,40));//40
                etat = true;
            }
            jLabel1.setText(jLabel5.getText());
        }
    Normalement quand je click sur l'endroit approprié, je modifie une propriété text d'un label et le panel doit se redimensionner, mais il ne le fait qu'au bout du 2ieme click.

    Le premier click il renomme le jLabel1.setText, pas de redimensionnement.
    le deuxieme, le redimensionnement fonctionne.

    par contre quand je me met en mode debugguer, il passe bien par les differences bloc de conditions, des les permiers click.



    Mais si je retire la ligne :

    jLabel1.setText(jLabel5.getText());

    alors le redimensionnement marche des le 1er click
    Si vous avez la solution. je vous en remercie par avance.

  2. #2
    Membre éclairé
    Avatar de sironimo
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    669
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations forums :
    Inscription : Mai 2004
    Messages : 669
    Points : 756
    Points
    756
    Par défaut
    Bonjour et si tu initialisais ton booléen etat dans ta méthode ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
     
        private void click(java.awt.event.MouseEvent evt) {  
            boolean  etat = false; // Panel Proprio                     
            // TODO add your handling code here:
            if(etat){
                jPanel1.setSize(new Dimension(jPanel1.getSize().width,120)); //120
                etat = false;
            }else {
                jPanel1.setSize(new Dimension(jPanel1.getSize().width,40));//40
                etat = true;
            }
            jLabel1.setText(jLabel5.getText());
        }

  3. #3
    Inactif  
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    497
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 497
    Points : 312
    Points
    312
    Par défaut
    En faite le problem ne vient pas de la variable booleene mais de l affichage, car, comme je l'avais mentionné un peuplus haut, en mode debuuger, je rentre bien dans les instructions :

    jPanel1.setSize(new Dimension(jPanel1.getSize().width,120)); //120

    et

    jPanel1.setSize(new Dimension(jPanel1.getSize().width,40)); //40

    Mais le dimensionnement, ne se fait qu'apres avoir fait ca :

    jLabel1.setText(jLabel5.getText());


    mais aussi, si je retire cette ligne la, jLabel1.setText(jLabel5.getText()); le dimensionnement marche parfaitement des le premier coups !

  4. #4
    Membre éclairé
    Avatar de sironimo
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    669
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations forums :
    Inscription : Mai 2004
    Messages : 669
    Points : 756
    Points
    756
    Par défaut
    Nouvelle possibilité : si tu effectues le setText avant d'effectuer le redimensionnement et que tu fais un tonPanel.validate() après tes tests ?

    ps : de plus je n'avais pas fait gaffe mais n'utilise pas la méthode setSize préfère lui la méthode setPreferredSize

  5. #5
    Inactif  
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    497
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 497
    Points : 312
    Points
    312
    Par défaut
    static boolean etat = false; // Panel Proprio
    private void click(java.awt.event.MouseEvent evt) {

    jLabel1.setText(jLabel5.getText());
    if(!etat){

    jPanel1.setPreferredSize(new Dimension(jPanel1.getSize().width,40));
    etat = true;
    }else {
    jPanel1.setPreferredSize(new Dimension(jPanel1.getSize().width,120));
    etat = false;
    }
    jPanel1.validate();
    }

    marche po ?

    mais je comprends pas bien le rapport entre le fait de modifier un text d'un label et redimensionner un panel ?
    c'est tout dans le meme block de l evenement Click !!!

    pourquoi quand je retire le : jLabel1.setText(jLabel5.getText());

    ca marche nikwel !

    et quand je le garde, il me fait le redimensionnement au bout de deux click !

    il est pas capable le Java, de faire deux choses consécutives differentes ? lol

  6. #6
    Membre éclairé
    Avatar de sironimo
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    669
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations forums :
    Inscription : Mai 2004
    Messages : 669
    Points : 756
    Points
    756
    Par défaut
    Citation Envoyé par sironimo Voir le message
    et que tu fais un jPanel1.validate() après tes tests ?

  7. #7
    Inactif  
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    497
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 497
    Points : 312
    Points
    312
    Par défaut
    oui oui, ca change rien du tout, lol

    il me redimensionne le jPanel1 visuellement (bien qu'il rentre dans la ligne d'execution ...) , seulement apres avoir modifier le text de jLabel1 ...

    C'est du n'importe quoi ! lol , j'y comprends plus rien ...

  8. #8
    Expert éminent sénior
    Avatar de adiGuba
    Homme Profil pro
    Développeur Java/Web
    Inscrit en
    Avril 2002
    Messages
    13 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java/Web
    Secteur : Transports

    Informations forums :
    Inscription : Avril 2002
    Messages : 13 938
    Points : 23 190
    Points
    23 190
    Billets dans le blog
    1
    Par défaut
    Salut,

    Citation Envoyé par 19cmos83 Voir le message
    il me redimensionne le jPanel1 visuellement (bien qu'il rentre dans la ligne d'execution ...) , seulement apres avoir modifier le text de jLabel1 ...
    Que veux-tu dire exactement par "après" ? Pourrais-tu nous fournir un code minimum compilable qui reproduit le problème ?

    a++

  9. #9
    Inactif  
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    497
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 497
    Points : 312
    Points
    312
    Par défaut
    Bun ca veut dire que si j'ecrit ce premier code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     static boolean etat = false; // Panel Proprio
        private void click(java.awt.event.MouseEvent evt) {                       
            // TODO add your handling code here:
           // jLabel1.setText(jLabel5.getText());
            if(!etat){
                jPanel1.setSize(new Dimension(jPanel1.getSize().width,120)); //120
                etat = true;
            }else {
                jPanel1.setSize(new Dimension(jPanel1.getSize().width,40));//40
                etat = false; 
            }
            
        }
    Le redimensionnement de mon Panel marche tres bien !


    mais si je decommente la ligne verte.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     static boolean etat = false; // Panel Proprio
        private void click(java.awt.event.MouseEvent evt) {                       
            // TODO add your handling code here:
           jLabel1.setText(jLabel5.getText());
            if(!etat){
                jPanel1.setSize(new Dimension(jPanel1.getSize().width,120)); //120
                etat = true;
            }else {
                jPanel1.setSize(new Dimension(jPanel1.getSize().width,40));//40
                etat = false; 
            }
            
        }

    Et bien, il va faire :

    jLabel1.setText(jLabel5.getText());

    ensuite

    jPanel1.setSize(new Dimension(jPanel1.getSize().width,120)); //120


    sans que cela ne modifie en rien la taille du Panel a l affichage !



    ca ne sera qu'au bout d'un autre click qu'il dimensionnerra ( à 40 px) la fenetre correctement par :

    jPanel1.setSize(new Dimension(jPanel1.getSize().width,40));//40



    ...


    comme je l'ai dit, si je retire :

    // jLabel1.setText(jLabel5.getText());


    ca marche du premier coups.
    mon appli ne contient, qu'un Panel avec un Label sur lequel je click, et un autre label en dehors du Panel qui recoit le nom du Label clické !

    rien d'extraordinaire ... :-(


    EDIT : je suis sous Netbeans 5.5.1, y a pitetre des petits bug ??

  10. #10
    Expert éminent sénior
    Avatar de adiGuba
    Homme Profil pro
    Développeur Java/Web
    Inscrit en
    Avril 2002
    Messages
    13 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java/Web
    Secteur : Transports

    Informations forums :
    Inscription : Avril 2002
    Messages : 13 938
    Points : 23 190
    Points
    23 190
    Billets dans le blog
    1
    Par défaut
    Citation Envoyé par 19cmos83 Voir le message
    EDIT : je suis sous Netbeans 5.5.1, y a pitetre des petits bug ??
    L'EDI n'a rien à voir avec les bugs de ton applications !

    Comme tu ne veux pas poster un bout de code fonctionnel il va te falloir répondre à plein de question :
    • As-tu invalider ton composant avant d'appeler validate() (sinon cela ne sert à rien). Le mieux serait d'appeler directement revalidate()
    • Quel est le type du composant parent (celui qui comporte le panel et les label et d'où semble sortir la méthode click()) ?
    • Et quel est son LayoutManager ?
    • La méthode click() est-elle appelé dans l'EDT ?


    a++

  11. #11
    Inactif  
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    497
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 497
    Points : 312
    Points
    312
    Par défaut
    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
    /*
     * Ident.java
     *
     * Created on 1 octobre 2007, 14:25
     */
    
    package principal;
    
    import java.awt.Dimension;
    
    /**
     *
     * @author  cmos
     */
    public class Ident extends javax.swing.JFrame {
        
        /** Creates new form Ident */
        public Ident() {
            initComponents();
            
        }
        
        
        /** 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() {
            jLabel1 = new javax.swing.JLabel();
            jPanel1 = new javax.swing.JPanel();
            jLabel4 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jTextField2 = new javax.swing.JTextField();
            jSeparator1 = new javax.swing.JSeparator();
            jLabel5 = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Projet Java Geometrie");
            jLabel1.setFont(new java.awt.Font("Andalus", 1, 18));
            jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            jLabel1.setText("xxx");
    
            jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
            jLabel4.setText("Prenom");
    
            jLabel3.setText("Nom");
    
            jLabel2.setText("Code");
    
            jTextField1.setText("jTextField1");
    
            jTextField2.setText("jTextField2");
    
            jLabel5.setFont(new java.awt.Font("Aharoni", 0, 14));
            jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            jLabel5.setText("Identification Prorietaire");
              jLabel5.addMouseListener(new java.awt.event.MouseAdapter() {
              public void mousePressed(java.awt.event.MouseEvent evt) {
                    click(evt);
                }
            });
    
            org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jLabel3)
                        .add(jLabel4)
                        .add(jLabel2))
                    .add(21, 21, 21)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jTextField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                        .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE))
                    .addContainerGap())
                .add(org.jdesktop.layout.GroupLayout.TRAILING, jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                .add(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jLabel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .add(jLabel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 11, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jLabel2)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jLabel3)
                        .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jLabel4)
                        .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
            );
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(51, 51, 51)
                            .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 296, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(layout.createSequentialGroup()
                            .addContainerGap()
                            .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(53, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jLabel1)
                    .add(16, 16, 16)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(205, Short.MAX_VALUE))
            );
            pack();
        }// </editor-fold>                       
                            
        
        
        static boolean etat = false; // Panel Proprio
        private void click(java.awt.event.MouseEvent evt) {                       
            // TODO add your handling code here:
            jLabel1.setText(jLabel5.getText());
            jPanel1.invalidate();
    
            if(!etat){
                jPanel1.setSize(new Dimension(jPanel1.getSize().width,120)); //120
                etat = true;
            }else {
                jPanel1.setSize(new Dimension(jPanel1.getSize().width,40));//40
                etat = false;
            }
            jPanel1.validate();
        }   
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Ident().setVisible(true);
                    
                }
            });
        }
        
        // Variables declaration - do not modify                     
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JSeparator jSeparator1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        // End of variables declaration                   
        
    }


    Non, ca ne change rien, quand j'utilise : jPanel1.invalidate(); jPanel1.validate();

    ou encore :

    jPanel1.revalidate();

  12. #12
    Expert éminent sénior
    Avatar de adiGuba
    Homme Profil pro
    Développeur Java/Web
    Inscrit en
    Avril 2002
    Messages
    13 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java/Web
    Secteur : Transports

    Informations forums :
    Inscription : Avril 2002
    Messages : 13 938
    Points : 23 190
    Points
    23 190
    Billets dans le blog
    1
    Par défaut
    Je ne connais pas le GroupLayout, mais il faudrait que tu te documentes sur sa manière de gérer les tailles des composants puisqu'apparemment tu lui spécifies des tailles lors de l'ajout des composants...

    a++

  13. #13
    Inactif  
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    497
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 497
    Points : 312
    Points
    312
    Par défaut
    Bonjour,


    Et bien en fait, je ne precise rien, j utilise le WysiWyg de netbeans, donc quand je place un composant sur ma Form, il me genere un tas de code, non modifiable directement sur le source ...

Discussions similaires

  1. Réponses: 1
    Dernier message: 14/06/2010, 08h48
  2. aide pour alléger mon code simple mais long
    Par tuco_benedicto dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 13/03/2010, 20h52
  3. mon pc demarre mais aucun affichage
    Par kroma23 dans le forum Composants
    Réponses: 6
    Dernier message: 13/06/2009, 14h49
  4. Réponses: 29
    Dernier message: 26/07/2007, 15h24
  5. code execute mais zero affichage
    Par cels dans le forum Servlets/JSP
    Réponses: 7
    Dernier message: 22/12/2006, 15h55

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