Hi everyone,

I'm stuck making a Scroll pane to see all the bar graphs created when they do not fit on screen, or when the frame is diminished. Can anyone tell me how to do this please?

Thanks

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.geom.*;

public class Graph extends JFrame{
        private JFrame frame = new JFrame();
        private String numberOfstudentsString="";
        private int     counter=0,numberOfstudentsInt=0,ConstructorCounter=0,QuestionAskerCounter=0,PanelModifierCounter=0;
        private int x,y;
        private JPanel panelOne = new JPanel();
        private String[] allStudents;
        private String [] allStudentsMarks;
        private int[] allStudentsInt;
        private JTextArea[] text1;
        private JTextArea valuesList= new JTextArea();
        private JTextArea size= new JTextArea();
        private JScrollPane testScroller = new JScrollPane(panelOne);

        private int[] coloumn={100,90,80,70,60,50,40,30,20,10,0};
    public Graph (){

        numberOfstudentsString=JOptionPane.showInputDialog(null,"Please Insert The Number of Students Needed","Details",JOptionPane.QUESTION_MESSAGE);
        numberOfstudentsInt=Integer.parseInt(numberOfstudentsString);
        panelOne = new JPanel();
        allStudents= new String[numberOfstudentsInt];
        allStudentsMarks= new String[numberOfstudentsInt];
        allStudentsInt= new int[numberOfstudentsInt];
        text1= new JTextArea[numberOfstudentsInt];
        textInitializer(text1);
        questionAsker();
        while(ConstructorCounter<numberOfstudentsInt)
        {
            //System.out.println(ConstructorCounter);
            displayValues(counter,allStudentsInt[ConstructorCounter],allStudents[ConstructorCounter],text1[ConstructorCounter]);
            ConstructorCounter++;
        }

         while(PanelModifierCounter<numberOfstudentsInt)
        {
            panelModifier(text1[PanelModifierCounter]);
            PanelModifierCounter++;
        }
        panelOne.setLayout(new GridLayout(1,numberOfstudentsInt));
        panelOne.setSize(300,300);
        testScroller.setPreferredSize(new Dimension(300, 300));
        testScroller.setHorizontalScrollBar(new JScrollBar());
        add(testScroller);
        panelOne.setBackground(Color.RED);
        valuesList.setEditable(false);




        super.setAlwaysOnTop(true);
        super.add(panelOne,BorderLayout.WEST);
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        super.setResizable(true);   
        super.setVisible(true);
        super.getContentPane().setBackground( Color.WHITE);
        super.setSize(300,300);
        super.setLocationRelativeTo(null);

    }
    /*public void paint(Graphics g)
    {
      super.paint(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setColor(Color.BLACK);
      Line2D line = new Line2D.Float(100, 55, 100, 225);
      g2.draw(line);
    }*/

    public  void displayValues(int counter,int numberVariable,String nameOfstudent,JTextArea text)
    {
        counter=0;
        while(counter <11){

               if(numberVariable>=coloumn[counter])
               {                   
                  text.append("\n"+coloumn[counter]+"\t*");
                }
               else
               {
                   text.append("\n"+coloumn[counter]);
                }
               counter++;
            }
        text.append("\n\t"+nameOfstudent+"("+numberVariable+")");
    }
    public void questionAsker()
    {

        while(QuestionAskerCounter<numberOfstudentsInt)
        {
            allStudents[QuestionAskerCounter]=JOptionPane.showInputDialog(null,"Please Insert Name of Student","Details",JOptionPane.QUESTION_MESSAGE);
            allStudentsMarks[QuestionAskerCounter]=JOptionPane.showInputDialog(null,"Please Insert Mark of Student","Details",JOptionPane.QUESTION_MESSAGE);

            QuestionAskerCounter++;
        }
    }
    public void panelModifier(JTextArea text)
    {


            text.setEditable(false);
            panelOne.add(text);


    }
    public void textInitializer(JTextArea[] text)
    {
        for( int i=0; i<numberOfstudentsInt; i++ )
        text[i] = new JTextArea();
    }
    public static void main(String []args){
        Graph gr=new Graph();
        gr.setVisible(true);
    }


}
  • remove panelOne.setSize(300,300);

  • remove super.setSize(300,300); use pack() instead

  • remove all super.Xxx, create an local variable for JFrame

  • JPanels dimension (inside JScrollPane) must be greater than rectangle from JScrollPane (basic property, reason why JScrollPane is there), otherwise JScrollBar isn't visible

This question has already been answered. Start a new discussion instead.