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 :

[jList][checkBox]


Sujet :

Composants Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Mars 2003
    Messages
    32
    Détails du profil
    Informations forums :
    Inscription : Mars 2003
    Messages : 32
    Par défaut [jList][checkBox]
    Bonjour,

    Voilà Comme dans beaucoup de post je voudrais ajouter des checkboxs dans ma jlist.
    Pour ca j'ai ecrit une classe qui herite de checkbox et qui implemente listCellRenderer.

    Par contre pour moi le but est de ne permettre de cocher la checkbox que si la ligne est selectionner. En fait je n'arrive pas a separer le cochage de la checkbox et la selection de la ligne.

    Il vaut mieux separer ma jlist en 2?

    Merci pour vos reponses

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 904
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 904
    Billets dans le blog
    54
    Par défaut
    J'ai eut le meme pb lors de la representation de JCheckBox dans un JTree.

    Voici la solution que j'ai utilise meme si je ne la trouve pas super-elegante. En fait je n'etend pas : je delegue.

    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
     
      // NOTE: split back the renderer and the editor into two separate class as it provoked rendering issues when the editing occured.
     
      /** Class that serves as the tree renderer (displays check box).
       * @author Fabrice Bouyé (fabriceb@spc.int)
       * @version 1.0
       */
      private class CheckRenderer
          extends DefaultTreeCellRenderer {
        /** The delegated checkbox.
         */
        private JCheckBox check = new JCheckBox();
     
        /** Creates a new instance.
         */
        public CheckRenderer() {
          super();
          check.setOpaque(false);
          check.setHorizontalTextPosition(AbstractButton.LEADING);
          check.setFocusPainted(true);
        }
     
        /** {@inheritDoc}
         */
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
          //Component component = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
          DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
          TreeCell cell = (TreeCell) node.getUserObject();
          //check.setForeground(component.getForeground());
          //check.setBackground(component.getBackground());
          check.setSelected(cell.displayed);
          check.setText(cell.screenname);
          check.setEnabled(cell.enabled);
          String tooltip = ExtendedConstants.HTML_LABEL_PREFIX + cell.description + ExtendedConstants.HTML_LABEL_SUFFIX;
          check.setToolTipText(tooltip);
          return check;
        }
      }
     
      /** Class that serves as the tree editor (displays check box).
       * @author Fabrice Bouyé (fabriceb@spc.int)
       * @version 1.0
       */
      private class CheckEditor
          extends DefaultTreeCellEditor implements ActionListener {
        /** The delegated checkbox.
         */
        private JCheckBox check = new JCheckBox();
     
        /** A hack that prevents the check box from beeing unselectd the first time the user clicks on it.
         */
        private boolean firstClick = true;
     
        /** The editor value.
         */
        private Object value;
     
        /** The previous value.
         */
        private boolean oldDisplayed;
     
        /** Creates a new instance.
         * @param displayTree The parent tree.
         */
        public CheckEditor(JTree displayTree) {
          super(displayTree, (DefaultTreeCellRenderer) displayTree.getCellRenderer());
          check.setOpaque(false);
          check.setHorizontalTextPosition(AbstractButton.LEADING);
          check.addActionListener(this);
          check.setFocusPainted(true);
        }
     
        /** {@inheritDoc}
         */
        public void cancelCellEditing() {
          super.cancelCellEditing();
          //DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
          //TreeCell cell = (TreeCell) node.getUserObject();
          //cell.displayed = oldDisplayed;
          //System.err.println("cancelCellEditing");
          firstClick = true;
        }
     
        /** {@inheritDoc}
         */
        public Object getCellEditorValue() {
          //System.err.println("getCellEditorValue");
          return value;
        }
     
        /** {@inheritDoc}
         */
        public boolean isCellEditable(EventObject anEvent) {
          //System.err.println("isCellEditable");
          boolean result = false;
          if (anEvent instanceof MouseEvent) {
            MouseEvent mouseEvent = (MouseEvent) anEvent;
            // How to detect the mouse click happened ontop of the check area, not on the check label?
            //if (mouseEvent.getClickCount() >= 2) {
            TreePath path = displayTree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY());
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
            TreeCell cell = (TreeCell) node.getUserObject();
            result = cell.enabled;
            //}
          }
          return result;
        }
     
        /** {@inheritDoc}
         */
        public boolean stopCellEditing() {
          boolean result = super.stopCellEditing();
          //System.err.println("stopCellEditing " + result);
          firstClick = true;
          return result;
        }
     
        /** {@inheritDoc}
         */
        public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
          //Component component = super.getTreeCellEditorComponent(tree, value, isSelected, expanded, leaf, row);
          //System.err.println("getTreeCellEditorComponent");
          this.value = value;
          DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
          TreeCell cell = (TreeCell) node.getUserObject();
          this.oldDisplayed = cell.displayed;
          //check.setForeground(component.getForeground());
          //check.setBackground(component.getBackground());
          check.setSelected(cell.displayed);
          check.setText(cell.screenname);
          check.setEnabled(cell.enabled);
          String tooltip = ExtendedConstants.HTML_LABEL_PREFIX + cell.description + ExtendedConstants.HTML_LABEL_SUFFIX;
          check.setToolTipText(tooltip);
          //firstClick = true;
          return check;
        }
     
        /** {@inheritDoc}
         */
        public void actionPerformed(ActionEvent event) {
          //System.err.println("actionPerformed");
          if (firstClick) {
            check.setSelected(!check.isSelected());
            firstClick = false;
            return;
          }
          DefaultMutableTreeNode node = (DefaultMutableTreeNode) getCellEditorValue();
          TreeCell treeCell = (TreeCell) node.getUserObject();
          boolean value = check.isSelected();
          treeCell.displayed = value;
          enableChildren(node, value);
          displayTree.repaint();
        }
     
        /** Enable/disable the children of the given node.
         * @param parent The parent node.
         * @param value If <CODE>true</CODE> the node will be disabled.
         */
        private void enableChildren(DefaultMutableTreeNode parent, boolean value) {
          int childNumber = parent.getChildCount();
          for (int i = 0; i < childNumber; i++) {
            DefaultMutableTreeNode child = (DefaultMutableTreeNode) parent.getChildAt(i);
            TreeCell cell = (TreeCell) child.getUserObject();
            cell.enabled = value;
            enableChildren(child, value);
          }
        }
      }
    A toi maintenant de voir si c'est adaptable a des renderers et des editors pour des listes. Je n'aime pas trop cette solution mais elle a le merite de fonctionner (dans la plus part des cas en tout cas).
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  3. #3
    Membre averti
    Inscrit en
    Mars 2003
    Messages
    32
    Détails du profil
    Informations forums :
    Inscription : Mars 2003
    Messages : 32
    Par défaut
    ok merci beaucoup
    je regarde ça....

Discussions similaires

  1. JList avec Checkboxes, problème d'accès à une variable
    Par olivier57b dans le forum Composants
    Réponses: 1
    Dernier message: 27/02/2012, 17h00
  2. Initialiser une JList composée de checkbox
    Par Goupsy dans le forum Composants
    Réponses: 3
    Dernier message: 12/08/2008, 16h50
  3. checkbox dans JList
    Par lazzeroni dans le forum Composants
    Réponses: 2
    Dernier message: 07/07/2006, 00h07
  4. Couleur d'un CheckBox
    Par benj63 dans le forum C++Builder
    Réponses: 4
    Dernier message: 15/07/2002, 14h48
  5. CheckBox en Read Only
    Par MrJéjé dans le forum C++Builder
    Réponses: 7
    Dernier message: 23/06/2002, 15h00

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