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

Agents de placement/Fenêtres Java Discussion :

Resize des composants dans un JPanel


Sujet :

Agents de placement/Fenêtres Java

  1. #1
    Membre à l'essai
    Femme Profil pro
    Inscrit en
    Juillet 2011
    Messages
    31
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations professionnelles :
    Secteur : Enseignement

    Informations forums :
    Inscription : Juillet 2011
    Messages : 31
    Points : 20
    Points
    20
    Par défaut Resize des composants dans un JPanel
    Bonjour,

    Je souhaiterais avoir vos conseils concernant le gridBagLayout et en particulier le resize des composants à l'intérieur d'un JPanel:

    Voici le code utilisé

    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
    public class MyPanel extends JPanel {
     
      private final String checkBoxLabel;
      private final JTextFieldtextField textField= new JTextField();
      private final Double value;
      private final JCheckBox checkBox;
     
      public MyPanel(String checkBoxLabel, Double value) {
        super(new GridBagLayout());
        this.checkBoxLabel = checkBoxLabel;
        this.defaultVal = defaultVal;
        checkBox = new JCheckBox(this.checkBoxLabel);
        init() ;
      }
     
      private void init() {
        if (value != null) textField.setText(this.value.toString());
        textField.setVisible(false);
        textField.setPreferredSize(new Dimension(100, 20));
        add(checkBox, new GridBagConstraints(0, 0, GridBagConstraints.RELATIVE, 1, 0.1D, 0.0D, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        add(textField, new GridBagConstraints(1, 0, GridBagConstraints.REMAINDER, 1, 0.0D, 0.0D, GridBagConstraints.FIRST_LINE_END, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 50, 0));
     
           checkBox.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            textField.setVisible(checkBox.isSelected());
      }
     
     }
    }
    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
    public class AllPanel extends JPanel {
     
      public AllPanel () {
        super(new GridBagLayout());
     
        onePanel = new MyPanel("one label:", 0.d);
        twoPanel = new MyPanel("two label:", 0.d);
        threePanel = new MyPanel("three label:", 0.d);
     
        fourPanel = new MyPanel("four label:", 0.d);
        fivePanel = new MyPanel("five label:", 0.d);
        sixPanel = new MyPanel("six label:", 0.d);
        init();
    }
     
      private void init() {
     
        JPanel leftPanel = new JPanel(new GridBagLayout());
        JLabel label = new JLabel("left panel");
        leftPanel.add(label, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
        leftPanel.add(onePanel , new GridBagConstraints(0, 1, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
        leftPanel.add(twoPanel , new GridBagConstraints(0, 2, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
        leftPanel.add(threePanel , new GridBagConstraints(0, 3, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
     
        JPanel rightPanel = new JPanel(new GridBagLayout());
        postProcPanel.add(new JLabel("right panel"), new GridBagConstraints(0, 0, 1, 1, 0.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
            new Insets(5, 5, 5, 5), 0, 0));
        rightPanel.add(fourPanel , new GridBagConstraints(0, 1, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
        rightPanel.add(fivePanel , new GridBagConstraints(0, 2, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
        rightPanel.add(sixPanel, new GridBagConstraints(0, 3, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
     
        add(leftPanel , new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 20, 0, 5), 0, 0));
        add(rightPanel , new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 30, 0, 5), 0, 0));
     
    }
    Le problème est que quand je sélectionne ou pas les check box, j'ai sans arrêt un resize de mon panel. Comment éviter cela et avoir un panel fixe que les textField soient visibles ou non ?

    Merci par avance

  2. #2
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur R&D - Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Points : 29 131
    Points
    29 131
    Billets dans le blog
    2
    Par défaut
    Salut,

    Est-ce que c'est quelque chose comme ça que tu cherches à obtenir ?

    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
    public class MyPanel extends JPanel {
     
    	private final String checkBoxLabel;
    	private final JTextField textField = new JTextField();
    	private final Double value;
    	private final JCheckBox checkBox;
     
    	public MyPanel(String checkBoxLabel, Double value) {
    		super();
    		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
    		this.checkBoxLabel = checkBoxLabel;
    		this.value = value;
    		checkBox = new JCheckBox(this.checkBoxLabel);
    		init();
    	}
     
    	private void init() {
    		if (value != null)
    			textField.setText(this.value.toString());
    		textField.setVisible(false);
    		add(checkBox);
    		add(textField);
     
    		checkBox.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				textField.setVisible(checkBox.isSelected());
    				revalidate();
    				repaint();
    			}
     
    		});
    	}
     
    }

    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
    public class AllPanel extends JPanel {
     
    	  private MyPanel onePanel;
    	private MyPanel twoPanel;
    	private MyPanel threePanel;
    	private MyPanel fourPanel;
    	private MyPanel fivePanel;
    	private MyPanel sixPanel;
     
    	public AllPanel () {
    	    super(new GridLayout(0,2));
     
    	    onePanel = new MyPanel("one label:", 0.d);
    	    twoPanel = new MyPanel("two label:", 0.d);
    	    threePanel = new MyPanel("three label:", 0.d);
     
    	    fourPanel = new MyPanel("four label:", 0.d);
    	    fivePanel = new MyPanel("five label:", 0.d);
    	    sixPanel = new MyPanel("six label:", 0.d);
    	    init();
    	}
     
    	  private void init() {
     
    	    JPanel leftPanel = new JPanel(new GridBagLayout());
    	    leftPanel.add(new JLabel("left panel"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    	    leftPanel.add(onePanel , new GridBagConstraints(0, 1, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
    	    leftPanel.add(twoPanel , new GridBagConstraints(0, 2, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
    	    leftPanel.add(threePanel , new GridBagConstraints(0, 3, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
     
    	    JPanel rightPanel = new JPanel(new GridBagLayout());
    	  rightPanel.add(new JLabel("right panel"), new GridBagConstraints(0, 0, 1, 1, 0.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
    	        new Insets(5, 5, 5, 5), 0, 0));
    	    rightPanel.add(fourPanel , new GridBagConstraints(0, 1, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
    	    rightPanel.add(fivePanel , new GridBagConstraints(0, 2, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
    	    rightPanel.add(sixPanel, new GridBagConstraints(0, 3, 1, 1, 1.0D, 0.0D, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
     
    	    add(leftPanel );
    	    add(rightPanel );
     
    	}
     
    }
    Par ailleurs, quelques remarques :

    • REMAINDER et RELATIVE concernent le gridx, par le gridwidth
    • Pour saisir un Double, utiliser plutôt un JTextFormatedField qu'un JTextField
    • Pour réaliser ce que tu voulais faire, peut-être voir plutôt du côté du GroupLayout, pour gérer un meilleur alignement. Au lieu de faire un composant tel que MyPanel, faire une méthode de construction, pour éviter la duplication de code, en mettant tous les couples de composants dans le même conteneur/layout
    L'expression "ça marche pas" ne veut rien dire. Indiquez l'erreur, et/ou les comportements attendus et obtenus, et donnez un Exemple Complet Minimal qui permet de reproduire le problème.
    La plupart des réponses à vos questions sont déjà dans les FAQs ou les Tutoriels, ou peut-être dans une autre discussion : utilisez la recherche interne.
    Des questions sur Java : consultez le Forum Java. Des questions sur l'EDI Eclipse ou la plateforme Eclipse RCP : consultez le Forum Eclipse.
    Une question correctement posée et rédigée et vous aurez plus de chances de réponses adaptées et rapides.
    N'oubliez pas de mettre vos extraits de code entre balises CODE (Voir Mode d'emploi de l'éditeur de messages).
    Nouveau sur le forum ? Consultez Les Règles du Club.

Discussions similaires

  1. Taille des composants dans un JPanel
    Par vodasan dans le forum AWT/Swing
    Réponses: 2
    Dernier message: 17/11/2009, 13h08
  2. Insertion des composants dans une JPanel
    Par altaro dans le forum Débuter
    Réponses: 3
    Dernier message: 11/05/2009, 19h25
  3. Placer des composant dans un JPanel
    Par DiverSIG dans le forum Débuter
    Réponses: 0
    Dernier message: 26/03/2009, 15h42
  4. Placer correctement des Composants dans un JPanel
    Par Lady dans le forum AWT/Swing
    Réponses: 5
    Dernier message: 14/01/2007, 13h34
  5. [Delphi] Icone des composants dans la palette
    Par jacquesberger dans le forum Composants VCL
    Réponses: 2
    Dernier message: 13/04/2004, 15h42

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