Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
653 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  Surcharge graphique d'un Checkbox

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Surcharge graphique d'un Checkbox

n°930161
sgamier
Posté le 22-12-2004 à 16:52:11  profilanswer
 

Bonjour à tous.
 
J'expose mon problème.
 
Je veux surcharger les checkbox de mon application afin que ceux ci est le fond orange lorsqu'ils ont le focus.
 
Pour cela j'ai créé la classe ci dessous

Code :
  1. package ocp.outils.ui;
  2. import java.io.*;
  3. import java.awt.*;
  4. import javax.swing.*;
  5. import javax.swing.plaf.*;
  6. import javax.swing.plaf.metal.*;
  7. /**
  8. * <p>Title: </p>
  9. * <p>Description: </p>
  10. * <p>Copyright: </p>
  11. * <p>Company: </p>
  12. * @author sgamier
  13. * @version 20042212
  14. */
  15. public class CustomCheckUI extends MetalCheckBoxUI{
  16.    private final static CustomCheckUI ui= new CustomCheckUI();
  17.    private boolean defaults_initialized = false;
  18.    public static ComponentUI createUI(JComponent c) {
  19.        return ui;
  20.    }
  21.    public void installDefaults(AbstractButton b) {
  22.       super.installDefaults(b);
  23.       if (!defaults_initialized)
  24.       {
  25.          icon = new FmiCheckBoxIcon();
  26.          defaults_initialized = true;
  27.       }
  28.    }
  29.    private static class FmiCheckBoxIcon implements Icon, Serializable {
  30.        final static int csize = 13;
  31.        /**
  32.         * Method paintIcon
  33.         *
  34.         *
  35.         * @param c
  36.         * @param g
  37.         * @param x
  38.         * @param y
  39.         *
  40.         */
  41.        public void paintIcon(Component c, Graphics g, int x, int y) {
  42.            JCheckBox   cb                   = (JCheckBox) c;
  43.            ButtonModel model                = cb.getModel();
  44.            // outer bevel
  45.            if (!cb.isBorderPaintedFlat()) {
  46.                g.setColor(UIManager.getColor("CheckBox.background" ));
  47.                g.fill3DRect(x, y, csize, csize, false);
  48.                // inner bevel
  49.                if (c.hasFocus()) {
  50.                    g.setColor(Color.orange);
  51.                } else {
  52.                    g.setColor(UIManager.getColor("CheckBox.shadow" ));
  53.                }
  54.                g.fill3DRect(x + 1, y + 1, csize - 2, csize - 2, false);
  55.                // inside box
  56.                if ((model.isPressed() && model.isArmed()) ||!model.isEnabled()) {
  57.                    g.setColor(UIManager.getColor("CheckBox.background" ));
  58.                } else {
  59.                    g.setColor(UIManager.getColor("CheckBox.highlight" ));
  60.                }
  61.                g.fillRect(x + 2, y + 2, csize - 4, csize - 4);
  62.            } else {
  63.                if (c.hasFocus()) {
  64.                    g.setColor(Color.yellow);
  65.                } else {
  66.                    g.setColor(UIManager.getColor("CheckBox.shadow" ));
  67.                }
  68.                g.drawRect(x + 1, y + 1, csize - 3, csize - 3);
  69.                if ((model.isPressed() && model.isArmed()) ||!model.isEnabled()) {
  70.                    g.setColor(UIManager.getColor("CheckBox.background" ));
  71.                } else {
  72.                    g.setColor(UIManager.getColor("CheckBox.highlight" ));
  73.                }
  74.                g.fillRect(x + 2, y + 2, csize - 4, csize - 4);
  75.            }
  76.            if (model.isEnabled())
  77.            {
  78.                if (c.hasFocus()) {
  79.                    g.setColor(Color.black);
  80. //                 g.setColor(UIManager.getColor("CheckBox.highlight" ));
  81.                } else {
  82.                    g.setColor(UIManager.getColor("CheckBox.darkShadow" ));
  83.                }
  84.            }
  85.            else {
  86.                g.setColor(UIManager.getColor("CheckBox.shadow" ));
  87.            }
  88.            // paint check
  89.            if (model.isSelected()) {
  90.                g.drawLine(x + 9, y + 3, x + 9, y + 3);
  91.                g.drawLine(x + 8, y + 4, x + 9, y + 4);
  92.                g.drawLine(x + 7, y + 5, x + 9, y + 5);
  93.                g.drawLine(x + 6, y + 6, x + 8, y + 6);
  94.                g.drawLine(x + 3, y + 7, x + 7, y + 7);
  95.                g.drawLine(x + 4, y + 8, x + 6, y + 8);
  96.                g.drawLine(x + 5, y + 9, x + 5, y + 9);
  97.                g.drawLine(x + 3, y + 5, x + 3, y + 5);
  98.                g.drawLine(x + 3, y + 6, x + 4, y + 6);
  99.            }
  100.        }
  101.        /**
  102.         * Method getIconWidth
  103.         *
  104.         *
  105.         * @return
  106.         *
  107.         */
  108.        public int getIconWidth() {
  109.            return csize;
  110.        }
  111.        /**
  112.         * Method getIconHeight
  113.         *
  114.         *
  115.         * @return
  116.         *
  117.         */
  118.        public int getIconHeight() {
  119.            return csize;
  120.        }
  121.    }
  122. }


 
 
J'ai ensuite créé une classe pour surcharger le JCheckBox
 

Code :
  1. package ocp.outils.ui;
  2. import javax.swing.*;
  3. import outilsBean.*;
  4. /**
  5. * <p>Title: OcpCheckBox</p>
  6. * <p>Description: </p>
  7. * @author sgamier
  8. * @version 20041222
  9. */
  10. public class OcpCheckBox extends JCheckBox{
  11.   static{
  12.      UIManager.getDefaults().put("monCheckUI", "ocp.outils.ui.CustomCheckUI" );
  13.   }
  14.    public OcpCheckBox(){
  15.       super();
  16.    }
  17.    public String getUIClassID(){
  18.       return "monCheckUI";
  19.    }
  20. }


 
et je me sert du tout comme ceci
 

Code :
  1. private  myCheckBox _chkPrincipale = new OcpCheckBox();


 
Tout ceci fonctionne tres bien.
 
Lorsque le focus arrive sur le checkbox celui ci a le fond orange, je peut mettre ou enlever la coche... nickel.
 
Mais lorsque le checkbox perd le focus.. les couleurs ET la coche disparaisse !!!
 
Help me... j'ai l'impression d'avoir fait le plus dur et de coincé sur une  :kaola: d'erreur !
 
Si vous avez des idées merci d'avance.
 
Sébastien
 

mood
Publicité
Posté le 22-12-2004 à 16:52:11  profilanswer
 

n°931021
sgamier
Posté le 23-12-2004 à 16:10:19  profilanswer
 

Merci à tous pour toutes ces réponses  :wahoo:  
 
J'ai juste cherché pendant DEUX jours complets.. pour UNE ligne !!
 
Si si ... il manquait juste un  
g.setColor(Color.black);
avant le tracage du check.  
 
J'en suis tombé marteau mais je voulais pas lacher le morceau  :pt1cable:  
 
Je vous met donc ici le resultat final
 
En 1er la classe pour surcharger le JCheckBox  

Code :
  1. package outils.ui;
  2. import javax.swing.*;
  3. /**
  4. * <p>Title: CustomCheckBox</p>
  5. * <p>Description: Permet la création d'un checkbox customisé</p>
  6. * <p>Licence: GPL</p>
  7. * @author http://www.gamier.net
  8. * @version 20041222
  9. */
  10. public class CustomCheckBox extends JCheckBox {
  11.     static{
  12.         //il faut mofidifier CustomCheckUI pour modifier l'apparence
  13.         UIManager.getDefaults().put("monCheckUI", "outils.ui.CustomCheckUI" );
  14.     }
  15.     public CustomCheckBox() {this(null, null, false);}
  16.     public CustomCheckBox(Icon icon) {this(null, icon, false);}
  17.     public CustomCheckBox(Icon icon, boolean sel) {this(null, icon, sel);}
  18.     public CustomCheckBox(String text) {this(text, null, false);}
  19.     public CustomCheckBox(String text, boolean sel) {this(text, null, sel);}
  20.     public CustomCheckBox(String text, Icon icon, boolean sel) {
  21.         super(text, icon, sel);
  22.     }
  23.    
  24.     public String getUIClassID(){
  25.         return "monCheckUI";
  26.     }
  27. }


 
Puis la classe de Customisation elle même
 

Code :
  1. package outils.ui;
  2. import java.awt.*;
  3. import java.io.Serializable;
  4. import javax.swing.*;
  5. import javax.swing.plaf.*;
  6. import javax.swing.plaf.metal.*;
  7. /**
  8. * <p>Title: CustomCheckUI</p>
  9. * <p>Description: Customisation des checkbox</p>
  10. * <p>Licence: GPL</p>
  11. * @author http://www.gamier.net
  12. * @version 20042212
  13. */
  14. public class CustomCheckUI extends MetalCheckBoxUI{
  15.     private final static CustomCheckUI ui= new CustomCheckUI();
  16.     private boolean defaults_initialized = false;
  17.    
  18.     public static ComponentUI createUI(JComponent c) {
  19.         return ui;
  20.     }
  21.    
  22.     //  si on veut surcharger aussi le texte qui accompagne c'est ici
  23.     // public void paint(Graphics g, JComponent c)
  24.     // { }
  25.    
  26.    
  27.     /**
  28.      * Surcharge de l'initialisation de l'objet
  29.     **/
  30.     public void installDefaults(AbstractButton b) {
  31.         super.installDefaults(b);
  32.         if (!defaults_initialized) {
  33.             icon = new myCheckBoxIcon();
  34.             defaults_initialized = true;
  35.         }
  36.     }
  37.    
  38.     /**
  39.      * Inner class de surcharge graphique
  40.     **/
  41.     private static class myCheckBoxIcon implements Icon, Serializable{
  42.        
  43.         final static int csize = 13; //taille par defaut d'un checkbox
  44.         final static Color _backGroundColor = UIManager.getColor("CheckBox.background" );
  45.         final static Color _selectedBackGroundColor = Color.orange;
  46.         final static Color _shadowColor = UIManager.getColor("CheckBox.shadow" );
  47.         final static Color _checkColor = Color.black;
  48.         final static Color _highlightColor = UIManager.getColor("CheckBox.highlight" );
  49.         final static Color _darkShadowColor = UIManager.getColor("CheckBox.darkShadow" );
  50.        
  51.         /**
  52.          * Surcharge de la methode paintIcon
  53.          * @param c Le composant
  54.          * @param g L'objet graphique
  55.          * @param x La position X
  56.          * @param y La position Y
  57.          * @author sgamier
  58.          * @version 20041223
  59.          */
  60.         public void paintIcon(Component c, Graphics g, int x, int y) {
  61.            
  62.             JCheckBox   cb                   = (JCheckBox) c;
  63.             ButtonModel model                = cb.getModel();
  64.            
  65.             //en dehors du pourtour
  66.             if (!cb.isBorderPaintedFlat()) {
  67.                 g.setColor(_backGroundColor);
  68.                 g.fill3DRect(x, y, csize, csize, false);
  69.                 //dans le pourtour
  70.                 if (c.hasFocus()) {//avec focus
  71.                     g.setColor(_selectedBackGroundColor);
  72.                 } else {//sans le focus
  73.                     g.setColor(_shadowColor);
  74.                 }
  75.                 //remplissage
  76.                 g.fill3DRect(x + 1, y + 1, csize - 2, csize - 2, false);
  77.                
  78.                 //dans la boite
  79.                 if ((model.isPressed() && model.isArmed()) ||!model.isEnabled()) {
  80.                     g.setColor(_backGroundColor);
  81.                 } else {
  82.                     g.setColor(_highlightColor);
  83.                 }
  84.                
  85.                 g.fillRect(x + 2, y + 2, csize - 4, csize - 4);
  86.             } else {
  87.                
  88.                 if (c.hasFocus()) {
  89.                     g.setColor(Color.yellow);
  90.                 } else {
  91.                     g.setColor(_shadowColor);
  92.                 }
  93.                
  94.                 g.drawRect(x + 1, y + 1, csize - 3, csize - 3);
  95.                
  96.                 if ((model.isPressed() && model.isArmed()) ||!model.isEnabled()) {
  97.                     g.setColor(_backGroundColor);
  98.                 } else {
  99.                     g.setColor(_highlightColor);
  100.                 }
  101.                
  102.                 g.fillRect(x + 2, y + 2, csize - 4, csize - 4);
  103.             }
  104.            
  105.             if (model.isEnabled()) {
  106.                 if (c.hasFocus()) {
  107.                     g.setColor(_highlightColor);
  108.                 } else {
  109.                     g.setColor(_darkShadowColor);
  110.                 }
  111.             } else {
  112.                 g.setColor(_shadowColor);
  113.             }
  114.            
  115.             g.setColor(_checkColor); //init couleur avant dessin du check
  116.             //tracage du check
  117.             if (model.isSelected()) {
  118.                 g.drawLine(x + 9, y + 3, x + 9, y + 3);
  119.                 g.drawLine(x + 8, y + 4, x + 9, y + 4);
  120.                 g.drawLine(x + 7, y + 5, x + 9, y + 5);
  121.                 g.drawLine(x + 6, y + 6, x + 8, y + 6);
  122.                 g.drawLine(x + 3, y + 7, x + 7, y + 7);
  123.                 g.drawLine(x + 4, y + 8, x + 6, y + 8);
  124.                 g.drawLine(x + 5, y + 9, x + 5, y + 9);
  125.                 g.drawLine(x + 3, y + 5, x + 3, y + 5);
  126.                 g.drawLine(x + 3, y + 6, x + 4, y + 6);
  127.             }
  128.            
  129.         }
  130.        
  131.         /**
  132.          * Method getIconWidth
  133.          * @return la longueur de l'icone
  134.          */
  135.         public int getIconWidth() {
  136.             return csize;
  137.         }
  138.        
  139.         /**
  140.          * Method getIconHeight
  141.          * @return la hauteur de l'icone
  142.          */
  143.         public int getIconHeight() {
  144.             return csize;
  145.         }
  146.     }
  147.    
  148.    
  149. }


 
et bien sur je m'en sert toujours comme cela :

Code :
  1. private  myCheckBox _chkPrincipale = new OcpCheckBox();


 
Merci et joyeux Noel  :hello:  
 
Sébastien


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  Surcharge graphique d'un Checkbox

 

Sujets relatifs
Threads - 1 autre question sur interface graphiqueVBA EXCEL - copie de graphique
[C] Utilisation d'un interface graphique javamacro pour excel, parametrage auto de graphique
Interface graphique en C++Problème d'axe dans un graphique...
Changer la valeur par Défaut d'un CheckBox pour un visiteur !![class] Surcharge d'opérateur +
prob de surcharge de constructeurA quoi ça ressemble des drivers d'une carte graphique aujourd'hui?
Plus de sujets relatifs à : Surcharge graphique d'un Checkbox


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR