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

SWT/JFace Java Discussion :

Comment fonctionnent les scrolls


Sujet :

SWT/JFace Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    58
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 58
    Par défaut Comment fonctionnent les scrolls
    J'ai beau cherché je ne trouve pas comment faire marché les scrollbar sur un composite. Les scrollbar apparaissent mais ne déplace pas les composant à l'intérieur.


    Faut-il utiliser un scrolledComposite ? si oui comment ? En plus je n'arrive à mettre qu'un seul composant dedans faut mettre un composite qui contiendrait les autre composants ?

  2. #2
    Membre chevronné
    Profil pro
    Étudiant
    Inscrit en
    Septembre 2007
    Messages
    340
    Détails du profil
    Informations personnelles :
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2007
    Messages : 340
    Par défaut
    Salut j'avais mois aussi le même problème... puis un jour miracle... j'ai lu la doc et avec un peu de matière grise en plus ça marche, mais bon comme je l'avoue ce n'est pas bien évident je vais t'aider un peu.

    J'ai commencé à me lancer dans une explication mais c'est trop long d'expliquer comment faire mais je vais écrire une classe qui le fait et je la poste après ça sera plus simple et explicite, je poste le code quand j'ai fini.

  3. #3
    Membre chevronné
    Profil pro
    Étudiant
    Inscrit en
    Septembre 2007
    Messages
    340
    Détails du profil
    Informations personnelles :
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2007
    Messages : 340
    Par défaut
    Voilà le code, j'ai fait deux trois tests et il semble que ça marche mais si tu trouves un bug dis le moi, tu peux étendre cette classe au lieu de Composite pour utiliser sa gestion des barres de défilement. Elle s'utilise comme un simple Composite, à trois méthodes près pour la gestions des ascensseurs, tu peux l'utiliser comme parent de plusieurs controles et lui définir un gestionnaire de placement.

    a+

    Achille

    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
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
     
     
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.SWTException;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.graphics.Rectangle;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Event;
    import org.eclipse.swt.widgets.Listener;
    import org.eclipse.swt.widgets.ScrollBar;
    import org.eclipse.swt.widgets.Shell;
     
    /**
     * This class provides the implementation of a Composite which is automatically
     * scrolled when its size is smaller than a specified size.
     * <P>
     * <b>Styles :</b> (All styles of Composite).
     * <p>
     * <b>Events :</b> (none).
     * <p>
     * <b><i>Date :</i></b> 1 mai 2008.
     * @version 1.0
     * @author Achille Roussel
     * @see Composite
     * @see ScrollBar
     */
    public class ScrollableComposite extends Composite {
     
    	/** The vertical scroll bar. */
    	ScrollBar vScroll;
     
    	/** The horizontal scroll bar. */
    	ScrollBar hScroll;
     
    	/** The horizontal offset of the content. */
    	int offsetX;
     
    	/** The vertical offset of the content. */
    	int offsetY;
     
    	/** The minimal width of the control. */
    	int minWidth;
     
    	/** The minimal height of the control. */
    	int minHeight;
     
    	/** The flag to auto-hide or not the scroll bars. */
    	boolean alwaysShowScroll;
     
    	/**
             * Creates a new instance of ScrollableComposite.
             * @param parent the parent Composite of the control.
             * @param style the style which describes the look of the control.
             * @exception IllegalArgumentException <ul>
             *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
             * </ul>
             * @exception SWTException <ul>
             *    <li>ERROR_WIDGET_DISPOSED - if the parent has been disposed</li>
             *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread 
             *    that created the parent</li>
             * </ul>
             */
    	public ScrollableComposite(Composite parent, int style) {
    		super(parent, style);
    		offsetX = 0;
    		offsetY = 0;
    		minWidth = SWT.DEFAULT;
    		minHeight = SWT.DEFAULT;
    		alwaysShowScroll = true;
    		boolean showScrollV = (style & SWT.V_SCROLL) != 0;
    		boolean showScrollH = (style & SWT.H_SCROLL) != 0;
    		if (showScrollV || showScrollH) {
    			Listener listener = new Listener() {
     
    				@Override
    				public void handleEvent(Event event) {
    					switch (event.type) {
    					case SWT.Resize : onResize(); break;
    					case SWT.Selection : onSelection(event); break;
    					}
    				}
     
    			};
    			if (showScrollV) {
    				vScroll = getVerticalBar();
    				vScroll.setMinimum(0);
    				vScroll.addListener(SWT.Selection, listener);
    			}
    			if (showScrollH) {
    				hScroll = getHorizontalBar();
    				hScroll.setMinimum(0);
    				hScroll.addListener(SWT.Selection, listener);
    			}
    			addListener(SWT.Resize, listener);
    		}
    	}
     
    	/** Called when the control needs to be resized. */
    	void onResize() {
    		setLayoutDeferred(true);
    		Point p = getMinSize();
    		Point q = getSize();
    		Rectangle r = super.getClientArea();
    		boolean showScrollV = q.y < p.y;
    		boolean showScrollH = q.x < p.x;
    		if (q.y >= p.y) {
    			offsetY = 0;
    			vScroll.setSelection(0);
    		}
    		if (q.x >= p.x) {
    			offsetX = 0;
    			hScroll.setSelection(0);
    		}
    		if (showScrollV && (r.y + r.height) > (p.y - offsetY)) {
    			offsetY = p.y - (r.y + r.height);
    		}
    		if (showScrollH && (r.x + r.width) > (p.x - offsetX)) {
    			offsetX = p.x - (r.x + r.width);
    		}
    		if (vScroll != null) {
    			if (!alwaysShowScroll) vScroll.setVisible(showScrollV);
    			else if (!vScroll.getVisible()) vScroll.setVisible(true);
    			vScroll.setMaximum(p.y);
    			vScroll.setThumb(r.height); //adjusts the size of the vertical bar
    		}
    		if (hScroll != null) {
    			if (!alwaysShowScroll) hScroll.setVisible(showScrollH);
    			else if (!hScroll.getVisible()) hScroll.setVisible(true);
    			hScroll.setMaximum(p.x);
    			hScroll.setThumb(r.width); //adjusts the size of the horizontal bar
    		}
    		setLayoutDeferred(false);
    	}
     
    	/** Called when one of the scroll bars is selected. */
    	void onSelection(Event e) {
    		if (vScroll != null && e.widget == vScroll) {
    			offsetY = vScroll.getSelection();
    			layout(false);
    		}
    		else if (hScroll != null && e.widget == hScroll) {
    			offsetX = hScroll.getSelection();
    			layout(false);
    		}
    	}
     
    	/**
             * Sets the minimum size of the receiver to the given argument which can
             * be a positive value of <code>SWT.DEFAULT</code> to use the values given
             * by the <code>computeSize()</code> method. Every negatives values will be
             * set to zero. If there is no horizontal scroll bar the width value will
             * be ignored to keep the default behavior of a Composite and vice versa for
             * the vertical scroll bar.
             * @param width the minimum width of the receiver.
             * @param height the minimum height of the receiver.
             * @exception SWTException <ul>
             *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
             *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread 
             *    that created the receiver</li>
             * </ul>
             */
    	public void setMinSize(int width, int height) {
    		checkWidget();
    		minWidth = width == SWT.DEFAULT ? width : Math.max(width, 0);
    		minHeight = height == SWT.DEFAULT ? height : Math.max(height, 0);
    	}
     
    	/**
             * Sets the minimum size of the receiver to the given argument which can
             * be a positive value of <code>SWT.DEFAULT</code> to use the values given
             * by the <code>computeSize()</code> method. Every negatives values will be
             * set to zero. If there is no horizontal scroll bar the width value will
             * be ignored to keep the default behavior of a Composite and vice versa for
             * the vertical scroll bar.
             * @param width the minimum width of the receiver.
             * @param height the minimum height of the receiver.
             * @exception IllegalArgumentException <ul>
             *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
             * </ul>
             * @exception SWTException <ul>
             *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
             *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread 
             *    that created the receiver</li>
             * </ul>
             */
    	public void setMinSize(Point size) {
    		checkWidget();
    		if (size == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
    		setMinSize(size.x, size.y);
    	}
     
    	/**
             * Returns the minimum size of the receiver before its content will be scrolled.
             * @return the receiver's minimum size.
             * @exception SWTException <ul>
             *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
             *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread 
             *    that created the receiver</li>
             * </ul>
             */
    	public Point getMinSize() {
    		checkWidget();
    		return computeSize(minWidth, minHeight, false);
    	}
     
    	/**
             * Setting this flag at false will cause the receiver to hide its scroll bars
             * when they are not needed anymore. If no scroll bar has been created the
             * method does nothing.
             * @param show the receiver's scroll bars visible state.
             * @exception SWTException <ul>
             *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
             *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread 
             *    that created the receiver</li>
             * </ul>
             */
    	public void setAlwayShowScrollBars(boolean show) {
    		checkWidget();
    		alwaysShowScroll = show;
    		if (show) {
    			if (vScroll != null) vScroll.setVisible(true);
    			if (hScroll != null) hScroll.setVisible(true);
    		}
    		else {
    			Rectangle clientArea = super.getClientArea();
    			Point minSize = getMinSize();
    			if (clientArea.isEmpty() || alwaysShowScroll) return;
    			if (clientArea.height >= minSize.y && vScroll.getVisible()) {
    				vScroll.setVisible(false);
    			}
    			else if (!vScroll.getVisible()) {
    				vScroll.setVisible(true);
    			}
    			if (clientArea.width >= minSize.x && hScroll.getVisible()) {
    				hScroll.setVisible(false);
    			}
    			else if (!hScroll.getVisible()) {
    				hScroll.setVisible(true);
    			}
    		}
    	}
     
    	/**
             * Returns true if the receiver's scroll bars are always visible, false otherwise.
             * @return the receiver's scroll bars visible state.
             * @exception SWTException <ul>
             *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
             *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread 
             *    that created the receiver</li>
             * </ul>
             */
    	public boolean getAlwaysShowScrollBars() {
    		checkWidget();
    		return alwaysShowScroll;
    	}
     
    	@Override
    	public Rectangle getClientArea() {
    		Rectangle r = super.getClientArea();
    		Point p = null;
    		if ((minWidth == SWT.DEFAULT || minHeight == SWT.DEFAULT) &&
    				(vScroll != null || hScroll != null)) {
    			p = computeSize(minWidth, minHeight, false);
    		}
    		else if (minWidth != SWT.DEFAULT && minHeight != SWT.DEFAULT) {
    			p = new Point(minWidth, minHeight);
    		}
    		if (p != null) {
    			if (vScroll != null) {
    				r.y -= offsetY;
    				if (r.height < p.y) {
    					r.height = p.y;
    				}
    			}
    			if (hScroll != null) {
    				r.x -= offsetX;
    				if (r.width < p.x) {
    					r.width = p.x;
    				}
    			}
    		}
    		return r;
    	}
     
    	/**
             * Main method to test the control.
             * @param args nothing
             */
    	public static void main(String[] args) {
    		Display display = new Display();
    		Shell shell = new Shell(display, SWT.SHELL_TRIM);
     
    		//The scrollable Composite
    		ScrollableComposite container = new ScrollableComposite(shell,
    				SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    		GridLayout layout = new GridLayout(2, false);
    		container.setLayout(layout);
    		container.setAlwayShowScrollBar(false);
     
    		//The content of the scrollable Composite
    		for (int i=0; i<6; i++) {
    			Button b = new Button(container, SWT.PUSH);
    			b.setText("This is the test of\na ScrollableComposite");
    			b.setLayoutData(new GridData(GridData.FILL_BOTH));
    			b.setImage(display.getSystemImage(SWT.ICON_INFORMATION));
    		}
     
    		shell.setLayout(new FillLayout());
    		shell.pack();
    		Point p = shell.getSize();
    		Rectangle r = display.getBounds();
    		shell.setLocation(r.width / 2 - p.x / 2, r.height / 2 - p.y / 2);
    		shell.open();
    		while (!shell.isDisposed()) {
    			if (!display.readAndDispatch()) {
    				display.sleep();
    			}
    		}
    		display.dispose();
    	}
    }

  4. #4
    Membre chevronné

    Homme Profil pro
    Consultant en technologies
    Inscrit en
    Juin 2004
    Messages
    332
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Consultant en technologies

    Informations forums :
    Inscription : Juin 2004
    Messages : 332
    Par défaut
    Citation Envoyé par soft0613 Voir le message
    Voilà le code
    TU as vraiment écrit ça ......??? Tu m'as surtout l'air d'avoir méchamment copié-collé le code du ScrolledComposite...

  5. #5
    Membre chevronné
    Profil pro
    Étudiant
    Inscrit en
    Septembre 2007
    Messages
    340
    Détails du profil
    Informations personnelles :
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2007
    Messages : 340
    Par défaut
    non je l'ai écris si tu veux va voir le code source d'un ScrolledComposite tu veras que c'est pas le même et surtout tu ne peux pas ajouter plusieurs controles à un ScrolledComposite, tu ne peux pas non plus ajouter un gestionnaire de positionnement à un ScrolledComposite alors qu'avec ça tu peux, après si t'es pas content je peux plus rien pour toi si tu t'étais donné la peine d'executer le code et de le lire t'aurais pas dit ça je me suis cassé le cul pendant 2h30 pour faire ça un "merci" n'aurait pas été de trop...

  6. #6
    Membre chevronné

    Homme Profil pro
    Consultant en technologies
    Inscrit en
    Juin 2004
    Messages
    332
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Consultant en technologies

    Informations forums :
    Inscription : Juin 2004
    Messages : 332
    Par défaut
    Mouais ...
    Il n'empêche que la plupart du code est au mieux fortement dupliqué (voir le setAlwaysShowScrollBars)...

    Il faudra que tu m'expliques la différence avec l'utilisation du ScrolledComposite officiel, dont la javadoc nous indique que l'on peut par exemple l'utiliser comme ceci :

    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
     
            Display display = new Display();
            Color red = display.getSystemColor(SWT.COLOR_RED);
            Color blue = display.getSystemColor(SWT.COLOR_BLUE);
            Shell shell = new Shell(display);
            shell.setLayout(new FillLayout());
     
            // set the size of the scrolled content - method 1
            final ScrolledComposite sc1 = new ScrolledComposite(shell, SWT.H_SCROLL
                    | SWT.V_SCROLL | SWT.BORDER);
            final Composite c1 = new Composite(sc1, SWT.NONE);
            sc1.setContent(c1);
            c1.setBackground(red);
            GridLayout layout = new GridLayout();
            layout.numColumns = 4;
            c1.setLayout(layout);
            Button b1 = new Button(c1, SWT.PUSH);
            b1.setText("first button");
            c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
     
            // set the minimum width and height of the scrolled content - method 2
            final ScrolledComposite sc2 = new ScrolledComposite(shell, SWT.H_SCROLL
                    | SWT.V_SCROLL | SWT.BORDER);
            sc2.setExpandHorizontal(true);
            sc2.setExpandVertical(true);
            final Composite c2 = new Composite(sc2, SWT.NONE);
            sc2.setContent(c2);
            c2.setBackground(blue);
            layout = new GridLayout();
            layout.numColumns = 4;
            c2.setLayout(layout);
            Button b2 = new Button(c2, SWT.PUSH);
            b2.setText("first button");
            sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
     
            Button add = new Button(shell, SWT.PUSH);
            add.setText("add children");
            final int[] index = new int[] { 0 };
            add.addListener(SWT.Selection, new Listener() {
                public void handleEvent(Event e) {
                    index[0]++;
                    Button button = new Button(c1, SWT.PUSH);
                    button.setText("button " + index[0]);
                    // reset size of content so children can be seen - method 1
                    c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
                    c1.layout();
     
                    button = new Button(c2, SWT.PUSH);
                    button.setText("button " + index[0]);
                    // reset the minimum width and height so children can be seen -
                    // method 2
                    sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
                    c2.layout();
                }
            });
     
            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
            display.dispose();
    On obtient exactement le même comportement que ton ScrollableComposite, en 2h30 de moins. Avec en plus l'avantage d'utiliser un code qui, s'il venait à être buggé, sera potentiellement maintenu par des milliers d'utilisateurs de la communauté...

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

Discussions similaires

  1. Comment fonctionnent les "keywords"
    Par shikakus dans le forum Référencement
    Réponses: 3
    Dernier message: 29/01/2007, 00h13
  2. Comment fonctionne les versions d'un logiciel?
    Par Antigonos Ier Gonatas dans le forum Windows
    Réponses: 12
    Dernier message: 14/07/2006, 18h48
  3. [VB6] Comment fonctionne les Tableaux ?
    Par Lucas42 dans le forum VB 6 et antérieur
    Réponses: 7
    Dernier message: 27/04/2006, 14h59
  4. [FLASH 8] Comment fonctionne les clips
    Par steeves5 dans le forum Flash
    Réponses: 3
    Dernier message: 27/01/2006, 10h23
  5. Comment fonctionnent les index des options d'un select ?
    Par pekka77 dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 31/10/2005, 18h05

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