sgamier | Merci à tous pour toutes ces réponses 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 Je vous met donc ici le resultat final
En 1er la classe pour surcharger le JCheckBox
Code :
- package outils.ui;
- import javax.swing.*;
- /**
- * <p>Title: CustomCheckBox</p>
- * <p>Description: Permet la création d'un checkbox customisé</p>
- * <p>Licence: GPL</p>
- * @author http://www.gamier.net
- * @version 20041222
- */
- public class CustomCheckBox extends JCheckBox {
- static{
- //il faut mofidifier CustomCheckUI pour modifier l'apparence
- UIManager.getDefaults().put("monCheckUI", "outils.ui.CustomCheckUI" );
- }
- public CustomCheckBox() {this(null, null, false);}
- public CustomCheckBox(Icon icon) {this(null, icon, false);}
- public CustomCheckBox(Icon icon, boolean sel) {this(null, icon, sel);}
- public CustomCheckBox(String text) {this(text, null, false);}
- public CustomCheckBox(String text, boolean sel) {this(text, null, sel);}
- public CustomCheckBox(String text, Icon icon, boolean sel) {
- super(text, icon, sel);
- }
-
- public String getUIClassID(){
- return "monCheckUI";
- }
- }
|
Puis la classe de Customisation elle même
Code :
- package outils.ui;
- import java.awt.*;
- import java.io.Serializable;
- import javax.swing.*;
- import javax.swing.plaf.*;
- import javax.swing.plaf.metal.*;
- /**
- * <p>Title: CustomCheckUI</p>
- * <p>Description: Customisation des checkbox</p>
- * <p>Licence: GPL</p>
- * @author http://www.gamier.net
- * @version 20042212
- */
- public class CustomCheckUI extends MetalCheckBoxUI{
- private final static CustomCheckUI ui= new CustomCheckUI();
- private boolean defaults_initialized = false;
-
- public static ComponentUI createUI(JComponent c) {
- return ui;
- }
-
- // si on veut surcharger aussi le texte qui accompagne c'est ici
- // public void paint(Graphics g, JComponent c)
- // { }
-
-
- /**
- * Surcharge de l'initialisation de l'objet
- **/
- public void installDefaults(AbstractButton b) {
- super.installDefaults(b);
- if (!defaults_initialized) {
- icon = new myCheckBoxIcon();
- defaults_initialized = true;
- }
- }
-
- /**
- * Inner class de surcharge graphique
- **/
- private static class myCheckBoxIcon implements Icon, Serializable{
-
- final static int csize = 13; //taille par defaut d'un checkbox
- final static Color _backGroundColor = UIManager.getColor("CheckBox.background" );
- final static Color _selectedBackGroundColor = Color.orange;
- final static Color _shadowColor = UIManager.getColor("CheckBox.shadow" );
- final static Color _checkColor = Color.black;
- final static Color _highlightColor = UIManager.getColor("CheckBox.highlight" );
- final static Color _darkShadowColor = UIManager.getColor("CheckBox.darkShadow" );
-
- /**
- * Surcharge de la methode paintIcon
- * @param c Le composant
- * @param g L'objet graphique
- * @param x La position X
- * @param y La position Y
- * @author sgamier
- * @version 20041223
- */
- public void paintIcon(Component c, Graphics g, int x, int y) {
-
- JCheckBox cb = (JCheckBox) c;
- ButtonModel model = cb.getModel();
-
- //en dehors du pourtour
- if (!cb.isBorderPaintedFlat()) {
- g.setColor(_backGroundColor);
- g.fill3DRect(x, y, csize, csize, false);
- //dans le pourtour
- if (c.hasFocus()) {//avec focus
- g.setColor(_selectedBackGroundColor);
- } else {//sans le focus
- g.setColor(_shadowColor);
- }
- //remplissage
- g.fill3DRect(x + 1, y + 1, csize - 2, csize - 2, false);
-
- //dans la boite
- if ((model.isPressed() && model.isArmed()) ||!model.isEnabled()) {
- g.setColor(_backGroundColor);
- } else {
- g.setColor(_highlightColor);
- }
-
- g.fillRect(x + 2, y + 2, csize - 4, csize - 4);
- } else {
-
- if (c.hasFocus()) {
- g.setColor(Color.yellow);
- } else {
- g.setColor(_shadowColor);
- }
-
- g.drawRect(x + 1, y + 1, csize - 3, csize - 3);
-
- if ((model.isPressed() && model.isArmed()) ||!model.isEnabled()) {
- g.setColor(_backGroundColor);
- } else {
- g.setColor(_highlightColor);
- }
-
- g.fillRect(x + 2, y + 2, csize - 4, csize - 4);
- }
-
- if (model.isEnabled()) {
- if (c.hasFocus()) {
- g.setColor(_highlightColor);
- } else {
- g.setColor(_darkShadowColor);
- }
- } else {
- g.setColor(_shadowColor);
- }
-
- g.setColor(_checkColor); //init couleur avant dessin du check
- //tracage du check
- if (model.isSelected()) {
- g.drawLine(x + 9, y + 3, x + 9, y + 3);
- g.drawLine(x + 8, y + 4, x + 9, y + 4);
- g.drawLine(x + 7, y + 5, x + 9, y + 5);
- g.drawLine(x + 6, y + 6, x + 8, y + 6);
- g.drawLine(x + 3, y + 7, x + 7, y + 7);
- g.drawLine(x + 4, y + 8, x + 6, y + 8);
- g.drawLine(x + 5, y + 9, x + 5, y + 9);
- g.drawLine(x + 3, y + 5, x + 3, y + 5);
- g.drawLine(x + 3, y + 6, x + 4, y + 6);
- }
-
- }
-
- /**
- * Method getIconWidth
- * @return la longueur de l'icone
- */
- public int getIconWidth() {
- return csize;
- }
-
- /**
- * Method getIconHeight
- * @return la hauteur de l'icone
- */
- public int getIconHeight() {
- return csize;
- }
- }
-
-
- }
|
et bien sur je m'en sert toujours comme cela :
Code :
- private myCheckBox _chkPrincipale = new OcpCheckBox();
|
Merci et joyeux Noel Sébastien |