-2

I have a class that extends JPanel, where I have overridden the paintComponent(Graphics g) method. However, I can't see the rectangles or the scroll bars I have drawn.

In the main function I have the following code below:

    MyClass mainPanel = new MyClass();
    mainPanel.setPreferredSize(new Dimension(1000, 1000));
    mainPanel.setLayout(new BorderLayout());

    JPanel scrollPanel = new JPanel();
    scrollPanel.setSize(new Dimension(2000, 2000));       
    JScrollPane scrollPane = new JScrollPane(scrollPanel,  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);  //Let all scrollPanel has scroll bars
    scrollPane.setViewportView(scrollPanel);
    scrollPane.setOpaque(true);

    scrollPanel.revalidate();
    mainPanel.add(scrollPane);


    JFrame frame = new JFrame("Scrollable Panel");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(mainPanel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);  
Bob Gilmore
  • 9,708
  • 13
  • 46
  • 51
burcak
  • 763
  • 4
  • 17
  • Have your read the [official guide](https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html) – AxelH Jan 03 '17 at 12:02
  • 2
    Try `scrollPanel.setPreferredSize(new Dimension(2000, 2000));`. Currently your `scrollPanel` is shrinking to nothing, so the `scrollPane` doesn't need to show any scrollbars. – khelwood Jan 03 '17 at 12:06
  • 1
    @NimrodArgov, read his code, he has one. – AxelH Jan 03 '17 at 12:08
  • When I change scrollPanel.setSize(new Dimension(2000, 2000)); with scrollPanel.setPreferredSize(new Dimension(2000, 2000)); scroll bars show up but the rectangles I draw through graphics object does not show up. – burcak Jan 03 '17 at 12:27
  • Official guide shows examples for attaching scroll bars to textArea or images, it doesn't mention about making JPanels scrollable. – burcak Jan 03 '17 at 12:35
  • @burcak There are no rectangles and graphics objects in the code you posted. – khelwood Jan 03 '17 at 13:02
  • I have a paintComponent method in MyClass which extends JPanel, in this method I have calls such that ..... public void paintComponent(Graphics g) { g.drawChars(chrCharArray, 0, chrCharArray.length, top_left_x, top_left_y-10); g.fillRect(top_left_x, top_left_y + (low*enlargeFactor), width, height*enlargeFactor); } – burcak Jan 03 '17 at 13:43

1 Answers1

0

I have solved this issue. MyClass extends JPanel. And I have overwritten paintComponent(Graphics g) method in which I draw rectangles through Graphics.

MyClass mainPanel = new MyClass()
mainPanel.setPreferredSize(new Dimension(7000, 1000));
mainPanel.setLayout(new BorderLayout());

JScrollPane scrollPane = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);  //Let all scrollPanel has scroll bars
scrollPane.setPreferredSize(new Dimension(1000, 900));


JFrame frame = new JFrame("Scrollable JPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane);
frame.setSize(1000, 900);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
burcak
  • 763
  • 4
  • 17