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 :

setBorder sur JTextField


Sujet :

Composants Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Femme Profil pro
    Developpeur
    Inscrit en
    Février 2010
    Messages
    101
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations professionnelles :
    Activité : Developpeur

    Informations forums :
    Inscription : Février 2010
    Messages : 101
    Par défaut setBorder sur JTextField
    Bonjour,

    Je souhaiterais modifier l'apparence de mon JTextField en passant par un setBorder.

    Mon Border :
    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
    public class RectangleBordsArrondi implements Border {          
    	private int radius;   
    	//private Color couleurInterieur = Color.BLACK;
    	private Color couleurBord = InitGene.RENSEIGNEB;
     
    	public RectangleBordsArrondi(int radius) 
    	{	
    		this.radius = radius;
    	}    
     
    	public RectangleBordsArrondi() 
    	{	
    		this.radius = 15;         
    	}  
     
    	public RectangleBordsArrondi(Color c) 
    	{	
    		this.radius = 15;
    		couleurBord=c;
    	}  
     
    	public RectangleBordsArrondi(Color c, int radius) 
    	{	
    		this.radius = radius;
    		couleurBord=c;
    	} 
    	/*
    	public RectangleBordsArrondi(Color ci, Color cb) 
    	{	
    		this.radius = 15;
    		couleurBord=cb;
    		couleurInterieur=ci;
    	} 
    	 */
     
    	public Insets getBorderInsets(Component c) 
    	{             
    		return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius);        
    	}
     
    	public boolean isBorderOpaque() 
    	{             
    		return true;   
    	} 
     
    	public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) 
    	{   
    		Graphics2D g2D = (Graphics2D)g;
     
    		g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    		g2D.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
     
    		/*
            //Intérieur composant
    		g2D.setColor(GRIS);
            //g.setColor(couleurInterieur);
    		g2D.fillRoundRect(x,y,width-1,height-1,radius,radius);
    		*/
     
    		//Bordure du composant
    		g2D.setColor(couleurBord);
    		g2D.setStroke(new BasicStroke(2));
    		g2D.drawRoundRect(x,y,width-1,height-1,radius,radius);   
     
    	}     
    }
    Mon JTexField :
    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
    public class JTextFieldBordsArrondi extends JTextField {
    	/**
             * Pour personnaliser l'apparence de mon JTextField
             */
    	private static final long serialVersionUID = 1L;
     
    	public JTextFieldBordsArrondi(String texte){
    		super(texte);
    		setPreferredSize(new Dimension(40, 40));
    		setBorder(new RectangleBordsArrondi(InitGene.DESACTIVE));
    		setVisible(false);
    		setOpaque (true);
     
    		addFocusListener(new java.awt.event.FocusListener(){
    			// Lorsque le focus est perdu
    			public void focusLost(FocusEvent e) {
    				setBorder(new RectangleBordsArrondi(InitGene.DESACTIVE));
    				paintComponent(getGraphics());
    			}
    			// Lorsque le focus est récupéré
    			public void focusGained(FocusEvent arg0) {
    				setBorder(new RectangleBordsArrondi(InitGene.ACTIVE));
    				paintComponent(getGraphics());
    			}
    		});
    	}
    }
    Le contour sort bien comme il faut et se modifie bien, mais le text de mon JtextField n'apparait pas.
    Ou est le problème ?

  2. #2
    Expert confirmé
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Par défaut
    Euh, pourquoi as tu fait un setVisible(false) sur ton textfield?

    Normal qu'il n'apparaisse pas...


    Bon sinon un example fonctionnel:

    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
    package swing.border;
     
    import java.awt.BasicStroke;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Insets;
    import java.awt.RenderingHints;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
     
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.border.Border;
     
    public class RectangleBordsArrondi implements Border {          
        private int radius;   
        //private Color couleurInterieur = Color.BLACK;
        private Color couleurBord = Color.black;
        private Color couleurBordDesactive = Color.yellow;
     
        public RectangleBordsArrondi(int radius) 
        {    
            this.radius = radius;
        }    
     
        public RectangleBordsArrondi() 
        {    
            this.radius = 15;         
        }  
     
        public RectangleBordsArrondi(Color c) 
        {    
            this.radius = 15;
            couleurBord=c;
        }  
     
        public RectangleBordsArrondi(Color c, int radius) 
        {    
            this.radius = radius;
            couleurBord=c;
        } 
        /*
        public RectangleBordsArrondi(Color ci, Color cb) 
        {    
            this.radius = 15;
            couleurBord=cb;
            couleurInterieur=ci;
        } 
         */
     
        public Insets getBorderInsets(Component c) 
        {             
            return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius);        
        }
     
        public boolean isBorderOpaque() 
        {             
            return true;   
        } 
     
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) 
        {   
            Graphics2D g2D = (Graphics2D)g.create();
     
            g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            g2D.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
     
            /*
            //Intérieur composant
            g2D.setColor(GRIS);
            //g.setColor(couleurInterieur);
            g2D.fillRoundRect(x,y,width-1,height-1,radius,radius);
            */
     
            //Bordure du composant
            System.out.println(c.hasFocus());
            if(c.hasFocus()){
                g2D.setColor(couleurBord);
            } else {
                g2D.setColor(couleurBordDesactive);
            }
            g2D.setStroke(new BasicStroke(2));
            g2D.drawRoundRect(x,y,width-1,height-1,radius,radius);   
            g2D.dispose();
     
        }     
     
     
        public static void main(String[] args) {
            JFrame f = new JFrame();
            final JTextField tf = new JTextField();
            tf.addFocusListener(new FocusListener() {
     
                @Override
                public void focusLost(FocusEvent arg0) {
                    tf.repaint();
                }
     
                @Override
                public void focusGained(FocusEvent arg0) {
                    tf.repaint();
                }
            });
     
            tf.setBorder(new RectangleBordsArrondi());
            tf.setOpaque (true);
            f.add(tf);
            f.pack();
            f.add(new JTextField(),BorderLayout.SOUTH);
            f.setLocationRelativeTo(null);
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setVisible(true);
        }
    }

  3. #3
    Membre confirmé
    Femme Profil pro
    Developpeur
    Inscrit en
    Février 2010
    Messages
    101
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations professionnelles :
    Activité : Developpeur

    Informations forums :
    Inscription : Février 2010
    Messages : 101
    Par défaut
    En fait, je met mes composants non visible au depart et je les rend visible plus tard.
    Le probleme n'est pas la puisque :
    Le contour sort bien comme il faut et se modifie bien, mais le text de mon JtextField n'apparait pas.
    Je me suis rendu compte que mon code fonctionne bien lorsque le JTextField est assez grand : 40 ok, 20 je vois plus le contenu ...

    J'ai cherché, et j'ai trouvé, dans mon code si je remplace
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    public Insets getBorderInsets(Component c) 
        {             
            return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius);        
        }
    par :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    public Insets getBorderInsets(Component c) {
    		return new Insets(5, 5, 5, 5);
    	}
    ca fonctionne !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. comment on fait un listener sur JTextField
    Par poulette3000 dans le forum Composants
    Réponses: 6
    Dernier message: 16/02/2010, 15h46
  2. Listener sur JtextField
    Par AnjouWeb dans le forum Composants
    Réponses: 2
    Dernier message: 25/06/2007, 01h48
  3. afficher sur JTextfield un entier
    Par ulysse031 dans le forum Composants
    Réponses: 4
    Dernier message: 24/04/2007, 15h23
  4. ChangeListener sur JTextField
    Par grabriel dans le forum Composants
    Réponses: 2
    Dernier message: 24/10/2006, 17h49
  5. Réponses: 2
    Dernier message: 09/05/2006, 23h13

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