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

AWT/Swing Java Discussion :

JScrollPane et JPanel


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2010
    Messages
    175
    Détails du profil
    Informations personnelles :
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2010
    Messages : 175
    Par défaut JScrollPane et JPanel
    Bonjour,
    Je viens vers vous parce que je commence à réellement désespérer une chose complètement stupide...
    Je développe actuellement une interface graphique. La fenêtre principale est une JFrame. J'ai un panel utilisé pour afficher un certain nombre de JCheckBox, JButton, ... Etant donné que le nombre d'élément peut être important, j'ai crée un JScrollPane dans lequel j'"add" mon JPanel.

    Si j'ajoute ce JScrollPane directement à ma JFrame, tout fonctionne correctement. Cependant, si je souhaite ajouter ce JPanel à l'intérieur d'un autre JPanel, je n'ai plus ma scrollbar et donc plus la possibilité de scroller.
    Est ce que quelqu'un est capable de m'expliquer pourquoi celà ne fonctionne pas et m'aiguiller?

    Cordialement,
    Julien

  2. #2
    Membre Expert Avatar de Ivelios
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juillet 2008
    Messages
    1 031
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 031
    Par défaut
    Bonjour,

    Le ScrollPane est un conteneur. Donc c'est lui qu'il faut ajouter dans l'autre Panel. et pas le panel qu'il contient.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    scroll.add(panel1);
    panel2.add(scroll);
    panel2.add(panel1);// <--- MAUVAIS

  3. #3
    Membre confirmé
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2010
    Messages
    175
    Détails du profil
    Informations personnelles :
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2010
    Messages : 175
    Par défaut
    Bonjour,
    Merci pour votre réponse. J'ajoute bel et bien le JScrollPane dans mon second JPanel d'où mon incompréhension :/

  4. #4
    Membre Expert Avatar de Ivelios
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juillet 2008
    Messages
    1 031
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 031
    Par défaut
    Peux tu envoyer le code s'il te plait pour que l'on puisse regarder (et tester).

    Merci

  5. #5
    Membre confirmé
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2010
    Messages
    175
    Détails du profil
    Informations personnelles :
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2010
    Messages : 175
    Par défaut
    Merci de ton aide.

    Pour le code, j'ai une classe Main des plus basic
    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
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {        
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run(){
                    MainWindow mwd = new MainWindow("Automatic detection of similarities");
                    mwd.pack();
                    mwd.setVisible(true);
                }
            });
        }
    }
    et une classe MainWindow qui porte problème
    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
    public class MainWindow extends JFrame {
        private static final int WIN_WIDTH = 700;
        private static final int WIN_HEIGHT = 675;
        private static final int PAN_WIDTH = WIN_WIDTH - 20;
     
        // Informations we need to get
        private JTextField tFilesDirectory = new JTextField();
        private JTextField tSkeletonDirectory = new JTextField();
        private List<Algorithm> lAlgorithm = new ArrayList<Algorithm>();
        private Component selectMatchingPanel;
     
        public MainWindow(String name) {
            super();
            setWindowFont(); // pour set la police d'écriture
            buildWindow(name); // créer la fenêtre basic
            buildMenuBar(); // créer la barre de menu
            buildContentPane(); // remplir la fenêtre
        }
     
        private void buildContentPane() {
            Container contentPane = getContentPane();
            contentPane.add(basicOptionsPanel(), BorderLayout.PAGE_START);
            JPanel panel = new JPanel();
            panel.add(selectMatchingPanel());
            contentPane.add(panel, BorderLayout.CENTER);
            contentPane.add(selectProcessPanel(), BorderLayout.PAGE_END);
        }
     
        private JPanel basicOptionsPanel() {
            JPanel panel = new JPanel();
     
            ... remplissage du panel ...
     
            return panel;
        }
     
        private JScrollPane selectMatchingPanel() {
            JPanel panel = new JPanel();
            panel.setBorder(BorderFactory.createTitledBorder("Algorithm selection"));
            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
     
            try {
                List<String> tmp = Utils.getClasses("dssc"); // Récupération de la liste des classes dans le package dssc
                int i = 0;
                for (Iterator it = tmp.iterator(); it.hasNext();i++) {
                    String sAlgorithm = (String)it.next();
                    JCheckBox cAlgorithm = new JCheckBox(sAlgorithm);
                    cAlgorithm.setName(sAlgorithm);
                    Algorithm aAlgorithm = new Algorithm(cAlgorithm);
                    // Adding the CheckBox to a list
                    lAlgorithm.add(aAlgorithm);
                    // Creating
                    JPanel pAlgorithm = new JPanel();
                    pAlgorithm.setBorder(BorderFactory.createTitledBorder(""));
                    GroupLayout gAlgorithm = new GroupLayout(pAlgorithm);
                    pAlgorithm.setLayout(gAlgorithm);
                    gAlgorithm.setAutoCreateGaps(true);
                    gAlgorithm.setAutoCreateContainerGaps(true);
     
                    JPanel pPreprocessors = fillPreprocessorsWindow(aAlgorithm, i);
                    if ((i % 2) == 0) {
                        cAlgorithm.setBackground(Color.GRAY);
                        pAlgorithm.setBackground(Color.GRAY);
                        pPreprocessors.setBackground(Color.GRAY);
                    }
                    pPreprocessors.setBorder(BorderFactory.createTitledBorder("Preprocessors"));
                    gAlgorithm.setHorizontalGroup(gAlgorithm.createSequentialGroup()
                            .addComponent(cAlgorithm)
                            .addComponent(pPreprocessors));
                    gAlgorithm.setVerticalGroup(gAlgorithm.createSequentialGroup()
                            .addComponent(cAlgorithm)
                            .addComponent(pPreprocessors));               
     
                    panel.add(pAlgorithm);
                    if (it.hasNext())
                        panel.add(Box.createRigidArea(new Dimension(0, 10)));
                }
            } catch (Exception e) {}
     
            return new JScrollPane(panel);
        }
     
        private JPanel fillPreprocessorsWindow(Algorithm algorithm, int i) {
            JPanel panel = new JPanel();
            panel.setBorder(BorderFactory.createLineBorder(Color.red));
            try {
                List<String> tmp = Utils.getClasses("maingui");
                for (Iterator it = tmp.iterator(); it.hasNext();) {
                    String sPreprocessor = (String)it.next();
                    JCheckBox cPreprocessor = new JCheckBox(sPreprocessor);
                    cPreprocessor.setName(sPreprocessor);
                    if ((i % 2) == 0)
                        cPreprocessor.setBackground(Color.GRAY);
                    panel.add(cPreprocessor);
                    algorithm.addPreprocessor(cPreprocessor);
                    if (it.hasNext())
                        panel.add(Box.createRigidArea(new Dimension(10, 0)));
                }
            } catch (Exception e) {}
            return panel;
        }
     
        private JPanel selectProcessPanel() {
            JPanel panel = new JPanel();
            panel.setPreferredSize(new Dimension(PAN_WIDTH, 50));
            panel.setMaximumSize(new Dimension(PAN_WIDTH, 50));
     
            JButton process = new JButton("Process files");
            process.setPreferredSize(new Dimension(100, 40));
            process.setMaximumSize(new Dimension(100, 40));
            process.setAlignmentX(Component.CENTER_ALIGNMENT);
            process.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    showAlgoAndPreproList();
                }
            });
     
            panel.add(process, BorderLayout.CENTER);
            return panel;
        }
     
        private void setWindowFont() {
            UIManager.put("Button.font", new Font("Serif", Font.PLAIN, 12));
            UIManager.put("CheckBox.font", new Font("Serif", Font.PLAIN, 12));
            UIManager.put("Label.font", new Font("Serif", Font.PLAIN, 12));
            UIManager.put("TitledBorder.font", new Font("Serif", Font.BOLD, 12));
        }
     
        private void buildWindow(String name) {
            setTitle(name);
            setPreferredSize(new Dimension(WIN_WIDTH, WIN_HEIGHT));
    //        setResizable(false); // Avoid resize
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close the application when user click on the cross
        }
     
        private void buildMenuBar() {
            JMenuBar menuBar = new JMenuBar();
            // File Menu
            JMenu file = new JMenu("File");
            JMenuItem exit = new JMenuItem(new ExitAction("Exit"));
            file.add(exit);
            // Help menu
            JMenu help = new JMenu("Help");
            JMenuItem aPropos = new JMenuItem(new AboutAction(this, "About"));
            help.add(aPropos);
            // adding menus
            menuBar.add(file);
            menuBar.add(help);
            // Setting the MenuBar
            setJMenuBar(menuBar);
        }
    }

Discussions similaires

  1. JScrollPane et JPanel
    Par Yopii dans le forum AWT/Swing
    Réponses: 6
    Dernier message: 27/10/2009, 11h28
  2. JScrollPane et JPanel
    Par Schyzophrenic dans le forum Agents de placement/Fenêtres
    Réponses: 6
    Dernier message: 04/07/2008, 19h48
  3. JscrollPane et Jpanel
    Par restricteur dans le forum Agents de placement/Fenêtres
    Réponses: 1
    Dernier message: 21/05/2008, 21h34
  4. JScrollPane et JPanel
    Par elfiestador dans le forum AWT/Swing
    Réponses: 5
    Dernier message: 26/12/2006, 16h43
  5. JScrollPane Graphics/ JPanel
    Par daedric dans le forum AWT/Swing
    Réponses: 8
    Dernier message: 26/05/2006, 12h28

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