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

avec Java Discussion :

Code qui ne compile pas


Sujet :

avec Java

  1. #1
    Membre éclairé
    Inscrit en
    Novembre 2005
    Messages
    188
    Détails du profil
    Informations forums :
    Inscription : Novembre 2005
    Messages : 188
    Par défaut Code qui ne compile pas
    Bonjour,

    Je ne comprends pas pourquoi ce code ne compile pas.


    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
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
     
    package com.tbo;
     
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import java.util.List;
    import java.util.function.Function;
    import java.util.function.Supplier;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
     
    public class PortfolioAndRuleApplicationV6 {
     
        public static void main(String[] args) {
            JFrame mainFrame = new JFrame("Portfolio and Rule Application V6");
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainFrame.setSize(500, 300);
            mainFrame.setLayout(new FlowLayout());
     
            JButton choosePortfolioButton = new JButton("Choose Portfolios");
            JButton chooseRuleButton = new JButton("Choose Rules");
            mainFrame.add(choosePortfolioButton);
            mainFrame.add(chooseRuleButton);
     
            SelectionDialog<Portfolio, PortfolioToExclude> portfolioDialog =
                    new SelectionDialog<>(mainFrame, "Portfolio Selection",
                            () -> fetchPortfoliosFromDatabase(),
                            PortfolioToExclude::new);
     
            SelectionDialog<Rule, RuleToExclude> ruleDialog =
                    new SelectionDialog<>(mainFrame, "Rule Selection",
                            () -> fetchRulesFromDatabase(),
                            RuleToExclude::new);
     
            choosePortfolioButton.addActionListener(e -> portfolioDialog.showDialog());
            chooseRuleButton.addActionListener(e -> ruleDialog.showDialog());
     
            mainFrame.setVisible(true);
        }
     
        static List<Portfolio> fetchPortfoliosFromDatabase() {
            List<Portfolio> portfolios = new ArrayList<>();
            portfolios.add(new Portfolio("Portfolio Alpha", 1, false));
            portfolios.add(new Portfolio("Portfolio Beta", 2, true));
            portfolios.add(new Portfolio("Portfolio Gamma", 3, false));
            return portfolios;
        }
     
        static List<Rule> fetchRulesFromDatabase() {
            List<Rule> rules = new ArrayList<>();
            rules.add(new Rule("Rule A", 1, false));
            rules.add(new Rule("Rule B", 2, true));
            rules.add(new Rule("Rule C", 3, false));
            return rules;
        }
     
        static class SelectionDialog<T, U extends Excludable> {
            private final JDialog dialog;
            private final Map<U, JCheckBox> checkBoxMap = new HashMap<>();
            private final List<U> allItemsToExclude;
            private final JPanel checkBoxPanel;
            private final Set<Integer> tempExcludedItems;
     
            public SelectionDialog(Frame parentFrame, String title,
                                   Supplier<List<T>> fetchFunction,
                                   Function<T, U> mapper) {
                allItemsToExclude = mapToExcludable(fetchFunction.get(), mapper);
                tempExcludedItems = new HashSet<>();
                dialog = new JDialog(parentFrame, title, Dialog.ModalityType.APPLICATION_MODAL);
                dialog.setSize(300, 400);
                dialog.setLayout(new BorderLayout());
     
                JTextField filterField = new JTextField();
                dialog.add(filterField, BorderLayout.NORTH);
     
                checkBoxPanel = new JPanel();
                checkBoxPanel.setLayout(new BoxLayout(checkBoxPanel, BoxLayout.Y_AXIS));
                JScrollPane scrollPane = new JScrollPane(checkBoxPanel);
                dialog.add(scrollPane, BorderLayout.CENTER);
     
                JButton saveButton = new JButton("Save Selection");
                dialog.add(saveButton, BorderLayout.SOUTH);
                saveButton.addActionListener(e -> saveSelections());
     
                filterField.getDocument().addDocumentListener(new DocumentListener() {
                    @Override
                    public void insertUpdate(DocumentEvent e) {
                        updateCheckBoxes(filterField.getText());
                    }
     
                    @Override
                    public void removeUpdate(DocumentEvent e) {
                        updateCheckBoxes(filterField.getText());
                    }
     
                    @Override
                    public void changedUpdate(DocumentEvent e) {
                        updateCheckBoxes(filterField.getText());
                    }
                });
     
                updateCheckBoxes("");
            }
     
            private void updateCheckBoxes(String filterText) {
                String searchText = filterText.toLowerCase();
                checkBoxPanel.removeAll();
                for (U itemToExclude : allItemsToExclude) {
                    if (searchText.isEmpty() || itemToExclude.getLabel().toLowerCase().contains(searchText)) {
                        JCheckBox checkBox = checkBoxMap.computeIfAbsent(itemToExclude, k -> new JCheckBox(itemToExclude.getLabel(), itemToExclude.isChecked()));
                        checkBox.addActionListener(e -> handleCheckBoxAction(itemToExclude, checkBox.isSelected()));
                        checkBoxPanel.add(checkBox);
                    }
                }
                checkBoxPanel.revalidate();
                checkBoxPanel.repaint();
            }
     
            public void showDialog() {
                dialog.setVisible(true);
            }
     
            private void handleCheckBoxAction(U itemToExclude, boolean isSelected) {
                if (isSelected) {
                    tempExcludedItems.add(itemToExclude.getId());
                } else {
                    tempExcludedItems.remove(itemToExclude.getId());
                }
            }
     
            private void saveSelections() {
                // Logic to save selections
            }
     
            private List<U> mapToExcludable(List<T> items, Function<T, U> mapper) {
                List<U> itemsToExclude = new ArrayList<>();
                for (T item : items) {
                    itemsToExclude.add(mapper.apply(item));
                }
                return itemsToExclude;
            }
        }
     
        interface Excludable {
            int getId();
     
            String getLabel();
     
            boolean isChecked();
     
            void setChecked(boolean isChecked);
        }
     
        static class Portfolio {
            private final String label;
            private final int id;
            private final boolean isChecked;
     
            public Portfolio(String label, int id, boolean isChecked) {
                this.label = label;
                this.id = id;
                this.isChecked = isChecked;
            }
     
            // Getters pour label, id et isChecked
        }
     
        static class Rule {
            private final String description;
            private final int id;
            private final boolean isChecked;
     
            public Rule(String description, int id, boolean isChecked) {
                this.description = description;
                this.id = id;
                this.isChecked = isChecked;
            }
     
            // Getters pour description, id et isChecked
        }
     
        static class PortfolioToExclude implements Excludable {
            private final int id;
            private final String label;
            private boolean isChecked;
     
            public PortfolioToExclude(int id, String label, boolean isChecked) {
                this.id = id;
                this.label = label;
                this.isChecked = isChecked;
            }
     
            @Override
            public int getId() {
                return id;
            }
     
            @Override
            public String getLabel() {
                return label;
            }
     
            @Override
            public boolean isChecked() {
                return isChecked;
            }
     
            @Override
            public void setChecked(boolean isChecked) {
                this.isChecked = isChecked;
            }
        }
     
        static class RuleToExclude implements Excludable {
            private final int id;
            private final String description;
            private boolean isChecked;
     
            public RuleToExclude(int id, String description, boolean isChecked) {
                this.id = id;
                this.description = description;
                this.isChecked = isChecked;
            }
     
            @Override
            public int getId() {
                return id;
            }
     
            @Override
            public String getLabel() {
                return description;
            }
     
            @Override
            public boolean isChecked() {
                return isChecked;
            }
     
            @Override
            public void setChecked(boolean isChecked) {
                this.isChecked = isChecked;
            }
        }
    }
    J'ai les deux erreurs suivantes:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Bad return type in lambda expression: List<Portfolio> cannot be converted to List<T>
    Bad return type in lambda expression: List<Rule> cannot be converted to List<T>
    et je vois pas pourquoi T ne peut pas être inféré avec Portfolio et Rule.

    Merci

  2. #2
    Membre éclairé
    Inscrit en
    Novembre 2005
    Messages
    188
    Détails du profil
    Informations forums :
    Inscription : Novembre 2005
    Messages : 188
    Par défaut
    Finalement, j'ai trouvé :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
            SelectionDialog<Portfolio, PortfolioToExclude> portfolioDialog =
                    new SelectionDialog<Portfolio, PortfolioToExclude>(mainFrame, "Portfolio Selection",
                            () -> fetchPortfoliosFromDatabase(),
                            portfolio -> new PortfolioToExclude(portfolio.getId(), portfolio.getLabel(), portfolio.isChecked()));
     
            SelectionDialog<Rule, RuleToExclude> ruleDialog =
                    new SelectionDialog<Rule, RuleToExclude>(mainFrame, "Rule Selection",
                            () -> fetchRulesFromDatabase(),
                            rule -> new RuleToExclude(rule.getId(), rule.getLabel(), rule.isChecked));
    Comme ceci, ça passe

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

Discussions similaires

  1. Code qui ne compile pas
    Par zsoufianz dans le forum Débuter avec Java
    Réponses: 4
    Dernier message: 04/12/2014, 21h17
  2. Code Java qui ne compile pas
    Par bawilar dans le forum Débuter avec Java
    Réponses: 2
    Dernier message: 03/11/2009, 09h25
  3. code java qui ne compile pas
    Par donny dans le forum Débuter avec Java
    Réponses: 9
    Dernier message: 19/07/2009, 20h06
  4. code qui se compile pas
    Par zangaloni dans le forum Interfaces Graphiques en Java
    Réponses: 4
    Dernier message: 26/04/2009, 16h53
  5. un code qui ne compile pas
    Par muslim1987 dans le forum Débuter avec Java
    Réponses: 3
    Dernier message: 19/06/2008, 11h55

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