Results 1 to 3 of 3
  1. #1
    theChameleon is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default JScrollBar... how to set the position of the scrollbar?

    hi, i modified the JFreeChart code to scroll the x axis to the end by default (one of the examples has a JScrollBar incorporated).

    but i cant set the position of the horizontal JScrollBar itself. scrollBar.setPosition(300,0); does not work.

    how do i change the position of the scroll bar?
    Last edited by theChameleon; 04-12-2011 at 09:38 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    theChameleon is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default

    hello, below is the source code.

    it is setLocation(), not setPosition(). my mistake. ah, i found out that setLocation() doesnt move the scroll thing, but moves the whole thing instead.

    so, how do i make the scrollbar start by scrolling to the right instead of the default left?

    Java Code:
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.DateAxis;
    import org.jfree.chart.axis.DateTickMarkPosition;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.plot.XYPlot;
    import org.jfree.chart.renderer.xy.XYBarRenderer;
    import org.jfree.data.time.*;
    import org.jfree.data.xy.XYDataset;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.AdjustmentEvent;
    import java.awt.event.AdjustmentListener;
    
    public class BarChartScrollBar1Demo extends JFrame {
    
    	public BarChartScrollBar1Demo() {
    		super("BarChartScrollBarlDemo");
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		DateAxis domainAxis = new DateAxis("Date");
    		NumberAxis rangeAxis = new NumberAxis("Volume");
    		XYBarRenderer renderer = new XYBarRenderer(0.1);
    		XYDataset dataset = getDataset();
    
    		Day d1 = new Day();
    		Day d2 = (Day) d1.next();
    		domainAxis.setAutoRange(false);
    		domainAxis.setRange(new DateRange(d1.getFirstMillisecond(),
    				d2.getFirstMillisecond()));
    
    		XYPlot mainPlot = new XYPlot(dataset, domainAxis, rangeAxis, renderer);
    
    		mainPlot.setDomainGridlinesVisible(true);
    		domainAxis.setTickMarkPosition(DateTickMarkPosition.START);
    		JFreeChart chart = new JFreeChart(null, null, mainPlot, true);
    		ChartPanel chartPanel = new ChartPanel(chart);
    		chartPanel.setPreferredSize(new Dimension(600, 600));
    
    		this.add(chartPanel);
    		JScrollBar scrollBar = getScrollBar(domainAxis);
    
    		this.add(scrollBar, BorderLayout.SOUTH);
    		this.pack();
    
    		final double r1 = domainAxis.getLowerBound();
    		final double r2 = domainAxis.getUpperBound();
    		final double millisInHour = 60 * 60 * 1000;
    
    		final double lowerBound = (300 * millisInHour) + r1;
    		final double upperBound = (300 * millisInHour) + r2;
    
    		[U]domainAxis.setRange(lowerBound, upperBound); // set default position, need to start the scroll bar at the right instead of the left[/U]
    	}
    
    	private XYDataset getDataset() {
    		Hour h = new Hour();
    		TimeSeries s1 = new TimeSeries("Series 1", Hour.class);
    		for (int i = 0; i < 300; i++) {
    			h = (Hour) h.next();
    			s1.add(h, Math.random() * 200);
    		}
    		final TimeSeriesCollection dataset = new TimeSeriesCollection();
    		dataset.addSeries(s1);
    
    		return dataset;
    	}
    
    	private JScrollBar getScrollBar(final DateAxis domainAxis) {
    		final double r1 = domainAxis.getLowerBound();
    		final double r2 = domainAxis.getUpperBound();
    		JScrollBar scrollBar = new JScrollBar(JScrollBar.HORIZONTAL, 0, 100, 0, 400);
    
    		scrollBar.addAdjustmentListener(new AdjustmentListener() {
    
    			public void adjustmentValueChanged(AdjustmentEvent e) {
    				double x = e.getValue() * 60 * 60 * 1000;
    				domainAxis.setRange(r1 + x, r2 + x);
    			}
    		});
    		return scrollBar;
    	}
    
    	public static void main(String[] args) {
    		new BarChartScrollBar1Demo().setVisible(true);
    	}
    }
    Last edited by theChameleon; 04-12-2011 at 09:27 PM.

Similar Threads

  1. paint with JScrollBar
    By shakeel in forum Java 2D
    Replies: 5
    Last Post: 12-23-2010, 04:35 PM
  2. Problem with JScrollBar
    By Arthur in forum AWT / Swing
    Replies: 3
    Last Post: 02-19-2010, 02:59 AM
  3. JScrollBar
    By solomon_13000 in forum AWT / Swing
    Replies: 1
    Last Post: 07-01-2009, 07:46 AM
  4. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 01:36 AM
  5. How to maintain scrollbar position on postback?
    By marathaWarrior in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-02-2009, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •