public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (boundedRangeModel != null) { int value = boundedRangeModel.getValue(); if (source == forwardButton) { boundedRangeModel.setValue( value == boundedRangeModel.getMaximum() ? boundedRangeModel.getMinimum() : value + 1); } else if (source == rewindButton) { boundedRangeModel.setValue( value == boundedRangeModel.getMinimum() ? boundedRangeModel.getMaximum() : value - 1); } else if (source == startButton) { if (startButton.isSelected() != player.isActive()) { if (startButton.isSelected()) { player.start(); } else { player.stop(); } } } else if (source == audioButton) { player.setAudioEnabled(audioButton.isSelected()); } else if (source == colorCyclingButton) { if (player instanceof ColorCyclePlayer) { ((ColorCyclePlayer) player).setColorCyclingStarted(colorCyclingButton.isSelected()); } } } }
public synchronized void setPlayer(Player player) { if (this.player != null) { this.player.removeChangeListener(this); player.removePropertyChangeListener(this); } this.player = player; // boundedRangeModel = player == null ? null : player.getBoundedRangeModel(); boundedRangeModel = player == null ? null : player.getTimeModel(); slider.setModel(boundedRangeModel); if (player != null) { if (player.getState() >= Player.REALIZED && boundedRangeModel != null && boundedRangeModel.getMaximum() == 0) { setPlayerControlsVisible(false); } slider.setProgressModel(player.getCachingModel()); startButton.setSelected(player.isActive()); player.addChangeListener(this); player.addPropertyChangeListener(this); audioButton.setVisible(player.isAudioAvailable()); audioButton.setSelected(player.isAudioEnabled()); colorCyclingButton.setVisible( (player instanceof ColorCyclePlayer) ? ((ColorCyclePlayer) player).isColorCyclingAvailable() : false); colorCyclingButton.setSelected( (player instanceof ColorCyclePlayer) ? ((ColorCyclePlayer) player).isColorCyclingStarted() : false); validate(); repaint(); BoundedRangeModel cachingControlModel = slider.getProgressModel(); } }
/** * This determines the amount of the progress bar that should be filled based on the percent done * gathered from the model. This is a common operation so it was abstracted out. It assumes that * your progress bar is linear. That is, if you are making a circular progress indicator, you will * want to override this method. */ protected int getAmountFull(Insets b, int width, int height) { int amountFull = 0; BoundedRangeModel model = progressBar.getModel(); if ((model.getMaximum() - model.getMinimum()) != 0) { if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) { amountFull = (int) Math.round(width * progressBar.getPercentComplete()); } else { amountFull = (int) Math.round(height * progressBar.getPercentComplete()); } } return amountFull; }
// ChangeListener public void stateChanged(ChangeEvent e) { BoundedRangeModel model = progressBar.getModel(); int newRange = model.getMaximum() - model.getMinimum(); int newPercent; int oldPercent = getCachedPercent(); if (newRange > 0) { newPercent = (int) ((100 * (long) model.getValue()) / newRange); } else { newPercent = 0; } if (newPercent != oldPercent) { setCachedPercent(newPercent); progressBar.repaint(); } }
/** * Get the maximum accessible value of this object. * * @return The maximum value of this object. */ public Number getMaximumAccessibleValue() { // TIGER - 4422362 BoundedRangeModel model = JSlider.this.getModel(); return Integer.valueOf(model.getMaximum() - model.getExtent()); }
/** * Returns the maximum value for the <code>JProgressBar</code>. This defines the upper bound for * the current value, and is stored in the component's <code>model</code>. * * @return The maximum value. * @see #setMaximum(int) */ public int getMaximum() { return model.getMaximum(); }
/** * Returns the current value expressed as a percentage. This is calculated as <code> * (value - min) / (max - min)</code>. * * @return The percentage (a value in the range 0.0 to 1.0). */ public double getPercentComplete() { if (getMaximum() == getMinimum()) return 1.0; else return (double) (model.getValue() - model.getMinimum()) / (model.getMaximum() - model.getMinimum()); }