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 :

JScrollPane - FlowLayout


Sujet :

Composants Java

  1. #1
    Invité
    Invité(e)
    Par défaut JScrollPane - FlowLayout
    Bonjour à tous !
    J'aurais besoin de votre aide pour un projet sur lequel je travaille depuis plusieurs jours et sur lequel je coince depuis 24h...

    Je voudrais ajouter une barre de scroll à l'application dont les dimensions sont fixes puisque le contenu dépasse du JPanel principal:
    Nom : log.JPG
Affichages : 346
Taille : 84,6 Ko

    J'ai beau chercher sur internet une solution, je n'y arrive pas... Si vous pouviez m'aider, ce serait vraiment génial:

    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
    package View;
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
     
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    import javax.swing.border.Border;
     
     
    public class Vue extends JFrame{
    	private static final long serialVersionUID = 1588912919136344858L;
     
    	// Déclaration des variables de classe
    		private JMenuBar menuBar = new JMenuBar();
    		private JMenu fichier = new JMenu("Fichier");
    		private JMenu panier = new JMenu("Panier");
    		private JMenuItem quitter = new JMenuItem("Quitter");
    		private JMenuItem voirPanier = new JMenuItem("Voir panier");
     
    		private JButton recherche;
    		private JTextField champRecherche;
     
    		private JCheckBox ajouterAuPanier_Iphone1;
    		private JButton details_Iphone1;
    		private JCheckBox ajouterAuPanier_Iphone4;
    		private JButton details_Iphone4;
    		private JCheckBox ajouterAuPanier_SamsungGalaxyS;
    		private JButton details_SamsungGalaxyS;
    		private JCheckBox ajouterAuPanier_NokiaLumia735;
    		private JButton details_NokiaLumia735;
     
     
    	public Vue(){		
    			this.setTitle("Projet: Application e-commerce - GARRETT");
    			this.setSize(800, 600);
     
    		// Retourne un JPanel qui contient notre vue
    			this.setContentPane( ensemble() );
    			this.setResizable(false);
    			this.setLocationRelativeTo(null);
    			this.setVisible(true);
    			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
     
    	// Fonction pour initialiser nos variables de classes
    		private JPanel ensemble(){
     
    			champRecherche = new JTextField("Tapez le nom du produit recherché.");
    			champRecherche.setPreferredSize( new Dimension(200, 20));
    			champRecherche.setHorizontalAlignment(JTextField.CENTER);
     
    			recherche = new JButton("Recherche");
    			details_Iphone1 = new JButton("Détails");
    			details_Iphone4 = new JButton("Détails");
    			details_SamsungGalaxyS = new JButton("Détails");
    			details_NokiaLumia735 = new JButton("Détails");
     
    			ajouterAuPanier_Iphone1 = new JCheckBox("Ajouté au panier");
    			ajouterAuPanier_Iphone4 = new JCheckBox("Ajouté au panier");
    			ajouterAuPanier_SamsungGalaxyS = new JCheckBox("Ajouté au panier");
    			ajouterAuPanier_NokiaLumia735 = new JCheckBox("Ajouté au panier");
     
    			Border border = BorderFactory.createLineBorder(Color.BLACK);
     
     
    			JPanel pan = new JPanel();
    			pan.setLayout(new FlowLayout(FlowLayout.CENTER));
     
     
    		// Menu
    			fichier.add(quitter);
    			panier.add(voirPanier);
    			menuBar.add(fichier);
    			menuBar.add(panier);
    			setJMenuBar(menuBar);
    			setVisible(true);
     
    			fichier.setMnemonic('F');
    			quitter.setMnemonic('Q');
     
    		// Zone de recherche
    			JPanel zoneDeRecherche = new JPanel();
    			zoneDeRecherche.add(recherche);
    			zoneDeRecherche.add(champRecherche);
     
     
    		// Zone de téléphones
    			JPanel zoneDeTelephones = new JPanel();
    			zoneDeTelephones.setBorder(border);
    			zoneDeTelephones.setLayout(new BoxLayout(zoneDeTelephones, BoxLayout.PAGE_AXIS));
    			getContentPane().add(zoneDeTelephones);
     
     
    		// iPhone 1	
    			JPanel iphone1 = new JPanel(new GridLayout(1, 2));
    			iphone1.setBorder(border);
    			iphone1.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
    			iphone1.setPreferredSize(new Dimension(800, 150));
     
    			JPanel gauche_Iphone1 = new JPanel(new GridLayout(2, 1));
    			gauche_Iphone1.setBorder(border);
    				JLabel resume_Iphone1 = new JLabel("<html><body><br/>Nom: iPhone<br /><br />Prix: 400.5</body></html>");
    				gauche_Iphone1.add(resume_Iphone1);
     
    			JPanel droite_Iphone1 = new JPanel(new BorderLayout());
    			droite_Iphone1.setBorder(border);
    				JPanel photo_Iphone1 = new JPanel();
    				droite_Iphone1.add(photo_Iphone1, BorderLayout.NORTH);
    				JLabel img_Iphone1 = new JLabel(new ImageIcon("images/iphone1.png"));
    				photo_Iphone1.add(img_Iphone1);
     
    				JPanel options_Iphone1 = new JPanel();
    				droite_Iphone1.add(options_Iphone1, BorderLayout.SOUTH);
    				options_Iphone1.add(ajouterAuPanier_Iphone1);
    				options_Iphone1.add(details_Iphone1);
     
    			iphone1.add(gauche_Iphone1, BorderLayout.WEST);
    			iphone1.add(droite_Iphone1, BorderLayout.EAST);
     
    		// iPhone 4
    			JPanel iphone4 = new JPanel(new GridLayout(1, 2));
    			iphone4.setBorder(border);
    			iphone4.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
    			iphone4.setPreferredSize(new Dimension(800, 150));
     
    			JPanel gauche_Iphone4 = new JPanel(new GridLayout(2, 1));
    			gauche_Iphone4.setBorder(border);
    				JLabel resume_Iphone4 = new JLabel("<html><body><br/>Nom: iPhone 4<br /><br />Prix: 456.0</body></html>");
    				gauche_Iphone4.add(resume_Iphone4);
     
    			JPanel droite_Iphone4 = new JPanel(new BorderLayout());
    			droite_Iphone4.setBorder(border);
    				JPanel photo_Iphone4 = new JPanel();
    				droite_Iphone4.add(photo_Iphone4, BorderLayout.NORTH);
    				JLabel img_Iphone4 = new JLabel(new ImageIcon("images/iphone4.png"));
    				photo_Iphone4.add(img_Iphone4);
     
    				JPanel options_Iphone4 = new JPanel();
    				droite_Iphone4.add(options_Iphone4, BorderLayout.SOUTH);
    				options_Iphone4.add(ajouterAuPanier_Iphone4);
    				options_Iphone4.add(details_Iphone4);
     
    			iphone4.add(gauche_Iphone4, BorderLayout.WEST);
    			iphone4.add(droite_Iphone4, BorderLayout.EAST);
     
    			// Samsung Galaxy S
    			JPanel samsungGalaxyS = new JPanel(new GridLayout(1, 2));
    			samsungGalaxyS.setBorder(border);
    			samsungGalaxyS.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
    			samsungGalaxyS.setPreferredSize(new Dimension(800, 150));
     
    			JPanel gauche_SamsungGalaxyS = new JPanel(new GridLayout(2, 1));
    			gauche_SamsungGalaxyS.setBorder(border);
    				JLabel resume_SamsungGalaxyS = new JLabel("<html><body><br/>Nom: Samsung Galaxy S<br /><br />Prix: 456.0</body></html>");
    				gauche_SamsungGalaxyS.add(resume_SamsungGalaxyS);
     
    			JPanel droite_SamsungGalaxyS = new JPanel(new BorderLayout());
    			droite_SamsungGalaxyS.setBorder(border);
    				JPanel photo_SamsungGalaxyS = new JPanel();
    				droite_SamsungGalaxyS.add(photo_SamsungGalaxyS, BorderLayout.NORTH);
    				JLabel img_SamsungGalaxyS = new JLabel(new ImageIcon("images/samsungGalaxyS.png"));
    				photo_SamsungGalaxyS.add(img_SamsungGalaxyS);
     
    				JPanel options_SamsungGalaxyS = new JPanel();
    				droite_SamsungGalaxyS.add(options_SamsungGalaxyS, BorderLayout.SOUTH);
    				options_SamsungGalaxyS.add(ajouterAuPanier_SamsungGalaxyS);
    				options_SamsungGalaxyS.add(details_SamsungGalaxyS);
     
    			samsungGalaxyS.add(gauche_SamsungGalaxyS, BorderLayout.WEST);
    			samsungGalaxyS.add(droite_SamsungGalaxyS, BorderLayout.EAST);
     
    			// Nokia Lumia 735
    			JPanel nokiaLumia735 = new JPanel(new GridLayout(1, 2));
    			nokiaLumia735.setBorder(border);
    			nokiaLumia735.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
    			nokiaLumia735.setPreferredSize(new Dimension(800, 150));
     
    			JPanel gauche_NokiaLumia735 = new JPanel(new GridLayout(2, 1));
    			gauche_NokiaLumia735.setBorder(border);
    				JLabel resume_NokiaLumia735 = new JLabel("<html><body><br/>Nom: Nokia Lumia 735<br /><br />Prix: 229.0</body></html>");
    				gauche_NokiaLumia735.add(resume_NokiaLumia735);
     
    			JPanel droite_NokiaLumia735 = new JPanel(new BorderLayout());
    			droite_NokiaLumia735.setBorder(border);
    				JPanel photo_NokiaLumia735 = new JPanel();
    				droite_NokiaLumia735.add(photo_NokiaLumia735, BorderLayout.NORTH);
    				JLabel img_NokiaLumia735 = new JLabel(new ImageIcon("images/nokiaLumia735.png"));
    				photo_NokiaLumia735.add(img_NokiaLumia735);
     
    				JPanel options_NokiaLumia735 = new JPanel();
    				droite_NokiaLumia735.add(options_NokiaLumia735, BorderLayout.SOUTH);
    				options_NokiaLumia735.add(ajouterAuPanier_NokiaLumia735);
    				options_NokiaLumia735.add(details_NokiaLumia735);
     
    			nokiaLumia735.add(gauche_NokiaLumia735, BorderLayout.WEST);
    			nokiaLumia735.add(droite_NokiaLumia735, BorderLayout.EAST);
     
     
    			// Ajout des lignes au JPanel principal
    				pan.add(zoneDeRecherche);
    				pan.add(zoneDeTelephones);
    				zoneDeTelephones.add(iphone1, BorderLayout.CENTER);
    				zoneDeTelephones.add(iphone4, BorderLayout.CENTER);
    				zoneDeTelephones.add(samsungGalaxyS, BorderLayout.CENTER);
    				zoneDeTelephones.add(nokiaLumia735, BorderLayout.CENTER);
     
    			// ScrollPane
    				JScrollPane scrollPane = new JScrollPane(pan);
    				setPreferredSize(new Dimension(800,600));
    				add(scrollPane, BorderLayout.CENTER);
     
    		return pan;
    	}
     
     
     
    }
    Merci d'avance !

  2. #2
    Membre éclairé
    Homme Profil pro
    Inscrit en
    Novembre 2013
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Novembre 2013
    Messages : 49
    Par défaut
    Bonjour,

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    this.setContentPane( ensemble() );
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    JScrollPane scrollPane = new JScrollPane(pan);
    				setPreferredSize(new Dimension(800,600));
    				add(scrollPane, BorderLayout.CENTER);
    return pan;
    Tu te compliques la vie : la ce que tu fais c'est que tu créais un JScrollPane qui va scroller ton pan (jusque la on est d'accord), ensuite tu ajoutes ton scroll au centre de ta fenêtre et par la suite tu l'"écrase" avec ton pan (vu que tu return pan; et que tu le mets sur toute ta fenêtre avec setContentPane). Le JScrollPane est un Container comme un autre, il remplace donc facilement le JPanel, donc plutôt qu'un long discours, tu n'as qu'a faire :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    private Container ensemble() {
    ....
    JScrollPane scrollPane = new JScrollPane(pan);
    				setPreferredSize(new Dimension(800,600));
    				//add(scrollPane, BorderLayout.CENTER); cette ligne devient et était inutile.
    return scrollPane;

  3. #3
    Invité
    Invité(e)
    Par défaut
    Je vais méditer dessus, en tout cas ça fonctionne!
    Merci beaucoup et problème résolu.

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

Discussions similaires

  1. FlowLayout dans JScrollPane
    Par lvr dans le forum Composants
    Réponses: 15
    Dernier message: 01/02/2010, 17h42
  2. [JFrame][JScrollPane] rafraichissement
    Par iuz dans le forum Agents de placement/Fenêtres
    Réponses: 5
    Dernier message: 14/05/2004, 12h08
  3. [JSCROLLPANE][JTABLE] Probleme de raf...
    Par thibaud dans le forum Composants
    Réponses: 5
    Dernier message: 04/05/2004, 12h26
  4. [JScrollPane]Comment savoir quand une scrollbar apparait ?
    Par FrigoAcide dans le forum Composants
    Réponses: 4
    Dernier message: 29/04/2004, 11h10
  5. [JScrollPane] élargir la taille
    Par nyny_p dans le forum Agents de placement/Fenêtres
    Réponses: 4
    Dernier message: 03/05/2003, 11h54

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