javaswingjfreechart

Add JScrollPane to JFreeChart_Facing problem


I am having problem adding JScrollPane to my LineChart, I am unable to the full project since it is a big Code. This is a code snippet from my project. I am able to generate the chart but the bottom scrollbar and vertical scroll bars do not appear. I have scratched head for few hours on this.

private void generateChart(String chartType, String xAxis, String yAxis) {
            JFreeChart chart = null;
    
            if ("Bar Chart".equals(chartType)) {
                chart = createBarChart(xAxis, yAxis);
            } else if ("Pie Chart".equals(chartType)) {
                chart = createPieChart(xAxis, yAxis);
            } else if ("Line Graph".equals(chartType)) {
                chart = createLineChart(xAxis, yAxis);
            }
    
            if (chart != null) {
                JFrame chartFrame = new JFrame("Chart");
    
                chartFrame.setSize(800, 600);
                chartFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // Create chart panel with scroll
                
              
    
                ChartPanel chartPanel = createChartPanelWithScroll(chart);
    
               
                chartFrame.add(chartPanel);
                chartFrame.setVisible(true);
            }
        }
        
     // Method to create scrollable and zoomable ChartPanel
        public ChartPanel createChartPanelWithScroll(JFreeChart chart) {
            // Create the ChartPanel with the chart
            ChartPanel chartPanel = new ChartPanel(chart);
            
            // Set preferred size larger than the visible area to ensure scrollbars appear
           // chartPanel.setPreferredSize(new java.awt.Dimension(2000, 1200));  // Force a larger size
    
            // Enable mouse zooming and panning
            chartPanel.setMouseWheelEnabled(true);  // Enable zooming with mouse wheel
            chartPanel.setDomainZoomable(true);     // Allow zooming on X-axis
            chartPanel.setRangeZoomable(true);   
            // Allow zooming on Y-axis
    
        
    
            // Wrap the ChartPanel with a JScrollPane for scrollable bars
            JScrollPane scrollPane = new JScrollPane(chartPanel, 
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    
            // Ensure that the JScrollPane size is set to occupy the window space
            scrollPane.setPreferredSize(new java.awt.Dimension(800, 600));
    
            // Create a panel that contains the scrollPane (with the chartPanel inside)
            JPanel chartWithScrollPanel = new JPanel(new BorderLayout());
            chartWithScrollPanel.add(scrollPane, BorderLayout.CENTER);
    
            return chartPanel;
        }

//Development by Habeeb E Sadeed


Solution

  • Corrected approach:

    private void generateChart(String chartType, String xAxis, String yAxis) {
        JFreeChart chart = null;
    
        // Generate the chart based on the selected type
        if ("Bar Chart".equals(chartType)) {
            chart = createBarChart(xAxis, yAxis);
        } else if ("Pie Chart".equals(chartType)) {
            chart = createPieChart(xAxis, yAxis);
        } else if ("Line Graph".equals(chartType)) {
            chart = createLineChart(xAxis, yAxis);
        }
    
        if (chart != null) {
            JFrame chartFrame = new JFrame("Chart");
    
            // Set the size of the frame
            chartFrame.setSize(1200, 800);
            chartFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            // Create the scrollable chart panel
            JScrollPane scrollPane = createChartPanelWithScroll(chart);
    
            // Add the scrollPane to the frame
            chartFrame.add(scrollPane, BorderLayout.CENTER);
    
            // Set the frame visibility
            chartFrame.setVisible(true);
        }
    }
    
    // Method to create scrollable and zoomable ChartPanel
    public JScrollPane createChartPanelWithScroll(JFreeChart chart) {
        // Create the ChartPanel with the chart
        ChartPanel chartPanel = new ChartPanel(chart);
    
        // Set the preferred size of the ChartPanel larger to trigger scrollbars
        chartPanel.setPreferredSize(new Dimension(1600, 1200));  // Set a larger preferred size
    
        // Enable mouse zooming and panning
        chartPanel.setMouseWheelEnabled(true);  // Enable zooming with mouse wheel
        chartPanel.setDomainZoomable(true);     // Allow zooming on X-axis
        chartPanel.setRangeZoomable(true);      // Allow zooming on Y-axis
    
        // Wrap the ChartPanel with a JScrollPane for scrollable bars
        JScrollPane scrollPane = new JScrollPane(chartPanel,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    
        return scrollPane;  // Return the scrollable pane instead of ChartPanel directly
    }