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 :

[AWT] ProgressBar qui ne va pas jusqu'au bout graphiquement


Sujet :

AWT/Swing Java

  1. #1
    Candidat au Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2015
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2015
    Messages : 3
    Par défaut [AWT] ProgressBar qui ne va pas jusqu'au bout graphiquement
    Bonjour,

    J'aimerais savoir pourquoi ma progressBar dont j'augmente la valeur à partir d'un Thread ne va pas jusqu'au bout graphiquement alors qu'au niveau du code la valeur maximale est atteinte.
    Voilà ce que ca donne :
    Nom : progressbar_dvp.png
Affichages : 170
Taille : 22,9 Ko

    Du coup elle se ferme toujours avant que la jauge ait été jusqu'au bout...

    Voici le code de la boite de dialogue contenant la progressBar :
    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
    public class LoadingDialog extends Dialog implements Observer, SelectionListener {
     
    	private static final Logger log = LogManager.getLogger(LoadingDialog.class.getName());
     
    	private Composite container;
     
    	private Label labelInfo;
     
    	private final HelloSWTJFace mainApplication;
     
    	private ProgressBar progressBar;
     
    	private ProjectsLoader projectsLoader;
     
    	public LoadingDialog(HelloSWTJFace mainApplication) {
     
    		super(mainApplication);
     
    		this.mainApplication = mainApplication;
     
    	}
     
    	@Override
    	protected void configureShell(Shell shell) {
     
    		super.configureShell(shell);
     
    		shell.setText(HelloSWTJFace.props.getProperty("messageDialogTitle.Loading"));
    	}
     
    	@Override
    	protected void createButtonsForButtonBar(Composite parent) {
     
    		final Button button = this.createButton(parent, IDialogConstants.CANCEL_ID,
    				HelloSWTJFace.props.getProperty("button.Cancel"), true);
    		button.addSelectionListener(this);
    	}
     
    	@Override
    	protected Control createDialogArea(Composite parent) {
     
    		this.container = (Composite) super.createDialogArea(parent);
    		final GridLayout gridLayout = new GridLayout(1, true);
    		gridLayout.marginRight = 7;
    		gridLayout.marginLeft = 10;
    		gridLayout.marginTop = 10;
    		gridLayout.marginBottom = 10;
    		this.getContainer().setLayout(gridLayout);
    		this.getContainer().setLayoutData(new GridData(325, 100));
    		this.progressBar = new ProgressBar(this.getContainer(), SWT.HORIZONTAL | SWT.SMOOTH);
    		final GridData gridData = new GridData();
    		gridData.horizontalAlignment = GridData.FILL;
    		gridData.grabExcessHorizontalSpace = true;
    		gridData.horizontalSpan = 1;
    		gridData.heightHint = 50;
    		this.getProgressBar().setLayoutData(gridData);
    		this.getProgressBar().setMinimum(0);
    		this.getProgressBar().setMaximum(100);
    		this.labelInfo = new Label(this.getContainer(), SWT.NONE);
     
    		gridData.horizontalAlignment = GridData.FILL;
    		gridData.grabExcessHorizontalSpace = true;
    		gridData.horizontalSpan = 1;
    		gridData.heightHint = 50;
    		this.getLabelInfo().setLayoutData(gridData);
     
    		return this.container;
    	}
     
    	public Composite getContainer() {
     
    		return this.container;
    	}
     
    	@Override
    	protected Point getInitialSize() {
     
    		return super.getInitialSize();
    	}
     
    	public Label getLabelInfo() {
     
    		return this.labelInfo;
    	}
     
    	public HelloSWTJFace getMainApplication() {
     
    		return this.mainApplication;
    	}
     
    	public ProgressBar getProgressBar() {
     
    		return this.progressBar;
    	}
     
    	public ProjectsLoader getProjectsLoader() {
     
    		return this.projectsLoader;
    	}
     
    	public void setProjectsLoader(ProjectsLoader projectsLoader) {
     
    		this.projectsLoader = projectsLoader;
    	}
     
    	@Override
    	public void update(Observable arg0, Object arg1) {
     
    		// log.warn("updateObservable : " + arg0);
     
    		if (arg0 instanceof ProjectsLoader) {
     
    			this.setProjectsLoader((ProjectsLoader) arg0);
     
    			// log.warn("projectsLoader : " + this.getProjectsLoader());
     
    			try {
     
    				this.getProgressBar().getDisplay().asyncExec(new Runnable() {
     
    					@Override
    					public void run() {
     
    						LoadingDialog.this.progressBar.setSelection(LoadingDialog.this.progressBar.getSelection() + 1);
     
    						if (LoadingDialog.this.progressBar
    								.getSelection() < (LoadingDialog.this.progressBar.getMaximum())) {
     
    							LoadingDialog.this.labelInfo.setText(
    									String.valueOf(LoadingDialog.this.progressBar.getSelection() + 1) + " / 100");
    						}
    						else if (LoadingDialog.this.progressBar
    								.getSelection() == (LoadingDialog.this.progressBar.getMaximum())) {
     
    							try {
     
    								Thread.sleep(2000);
     
    							}
    							catch (final Exception e) {
     
    								log.warn(e.fillInStackTrace());
    							}
     
    							LoadingDialog.this.close();
    						}
    					}
    				});
    			}
    			catch (final Exception e) {
     
    				log.warn("Interrupted : " + e.fillInStackTrace());
    			}
    		}
    	}
     
    	@Override
    	public void widgetDefaultSelected(SelectionEvent arg0) {
     
    	}
     
    	@Override
    	public void widgetSelected(SelectionEvent arg0) {
     
    		log.warn("widgetSelected : " + arg0.getSource());
     
    		try {
     
    			this.getProjectsLoader().stop();
     
    		}
    		catch (final NullPointerException e) {
     
    			log.warn(e.fillInStackTrace());
    		}
     
    	}
    }
    Une entité bidon qui me sert juste de base :
    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
    package org.helloswtjface.entities;
     
    import java.util.Observable;
    import java.util.Observer;
     
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.helloswtjface.dialogs.LoadingDialog;
    import org.helloswtjface.threads.LoadingThread;
     
    public class ProjectsLoader extends Observable {
     
    	private static final Logger log = LogManager.getLogger(ProjectsLoader.class.getName());
     
    	private boolean loading;
     
    	private final LoadingDialog loadingDialog;
     
    	public ProjectsLoader(LoadingDialog loadingDialog) {
     
    		this.loading = false;
    		this.loadingDialog = loadingDialog;
    		this.addObserver(loadingDialog);
    		this.start();
     
    	}
     
    	@Override
    	public void addObserver(final Observer observer) {
     
    		log.warn("addObserver : " + observer);
     
    		super.addObserver(observer);
    	}
     
    	public LoadingDialog getLoadingDialog() {
     
    		return this.loadingDialog;
    	}
     
    	public boolean isLoading() {
     
    		return this.loading;
    	}
     
    	public void setLoading(boolean loading) {
     
    		this.loading = loading;
    	}
     
    	public void start() {
     
    		this.setLoading(true);
     
    		if (this.isLoading()) {
     
    			final LoadingThread loadingThread = new LoadingThread(this);
     
    			loadingThread.start();
    		}
     
    	}
     
    	public void stop() {
     
    		this.setLoading(false);
    	}
     
    	public void update() {
     
    		this.setChanged();
    		this.notifyObservers();
    	}
    }
    et enfin le Thread :
    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
    package org.helloswtjface.threads;
     
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.helloswtjface.entities.ProjectsLoader;
     
    public class LoadingThread extends Thread {
     
    	private static final Logger log = LogManager.getLogger(LoadingThread.class.getName());
     
    	private final ProjectsLoader projectsLoader;
     
    	public LoadingThread(ProjectsLoader projectsLoader) {
     
    		super();
     
    		this.projectsLoader = projectsLoader;
     
    	}
     
    	public ProjectsLoader getProjectsLoader() {
     
    		return this.projectsLoader;
    	}
     
    	@Override
    	public void run() {
     
    		int i = 0;
     
    		while ((i < 100) && this.getProjectsLoader().isLoading()) {
     
    			try {
     
    				Thread.sleep(100);
     
    				i++;
     
    				log.warn("ProgressBar : " + i + "%");
     
    				this.getProjectsLoader().update();
    			}
    			catch (final InterruptedException e) {
     
    				i = 100;
     
    				this.getProjectsLoader().stop();
     
    				log.warn("LoadingThread : " + e.fillInStackTrace());
    			}
    		}
     
    		this.getProjectsLoader().stop();
    	}
    }
    Je précise que je débute...
    Merci d'avance.

  2. #2
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Parce que dans ton code tu ferme la boîte de dialogue quand elle arrive à 100% et le sleep n'y change rien puisque tu suspend le thread d'affichage et donc tu empêche la barre de se redessiner. Laisse la fenêtre ouverte et tu verra que tu arrive a 100%

  3. #3
    Candidat au Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2015
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2015
    Messages : 3
    Par défaut
    Bonjour,

    merci pour votre réponse, je sais que mon Thread.sleep(2000) ne sert à rien mais c'était pour avoir le temps de voir que l'affichage ne va pas jusqu'au bout...
    Et vous le dites bien; j'arrive à 100% mais l'affichage ne suit pas, il y a un décalage dans le temps entre l'affichage et la valeur de la fonction progressBar.getSelection(), j'aimerais savoir pourquoi.

  4. #4
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Je viens de te le dire, le Thread.sleep bloque l'affichage donc la barre ne peut pas afficher les 100%

  5. #5
    Candidat au Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2015
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2015
    Messages : 3
    Par défaut
    Ok désolé on s'est mal compris .
    Merci en tout cas!

Discussions similaires

  1. Commande batch qui ne s'exécute pas jusqu'au bout
    Par jejeman dans le forum Shell et commandes GNU
    Réponses: 6
    Dernier message: 03/10/2012, 13h04
  2. [1.x] fichier test qui ne s'execute pas jusqu'au bout
    Par erictomcat dans le forum Symfony
    Réponses: 2
    Dernier message: 20/08/2012, 23h04
  3. Réponses: 14
    Dernier message: 01/09/2010, 16h22
  4. Programme qui ne va pas jusqu'au bout ?
    Par chicabonux dans le forum Débuter
    Réponses: 59
    Dernier message: 12/04/2009, 20h06
  5. div qui ne descend pas jusqu'en bas
    Par grinder59 dans le forum Mise en page CSS
    Réponses: 1
    Dernier message: 15/02/2008, 10h35

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