-2

I am trying to add scrollbar to panel2 in the following code which contains an image in it. The problem is even though i add JScrollPane, i am getting no scrollbar in the output and it doesn't scroll either and i only see half of the image. Please help .

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.image.*;



public class manga extends JPanel{


public static void main(String[] args) {
    JFrame frame = new JFrame("Swing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel=new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

    JPanel panel1=new JPanel();
    JPanel panel2=new JPanel();


    JButton button1=new JButton("PlAY");
    button1.setBounds(100,300,70,30);

    String path="01.jpg";
    ImageIcon imageIcon = new ImageIcon(path);
    JLabel label = new JLabel(imageIcon);


    panel.setPreferredSize(new Dimension(1000,1000));

    JScrollPane scroll = new JScrollPane(label);
   scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    panel2.add(scroll);
   panel2.add(label);

   panel1.setBackground(new Color(255,255,255));
   panel2.setBackground(new Color(0,0,0));
   panel1.add(button1);
   panel.add(panel1);
   panel.add(panel2);
   frame.add(panel);
   frame.setTitle("MANGA READER 0.1");
   frame.setSize(1366,768);


  frame.setVisible(true);
 }
 }

I think the error lies in the following code.

panel.setPreferredSize(new Dimension(1000,1000));

JScrollPane scroll = new JScrollPane(label);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panel2.add(scroll);

I am not sure.Please help

Imran Ali
  • 2,223
  • 2
  • 28
  • 41
Project-A
  • 133
  • 1
  • 2
  • 12

1 Answers1

4

In Java Swing a component can have only one parent.

That means that by executing

panel2.add(scroll);
panel2.add(label);

you remove the label from the scroll pane.

Your don't need to add the label to the panel itself, so just drop that line and write only

panel2.add(scroll);
Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
  • Your suggestion works. I can see something like scrollbar . But it doesn't scroll the image. Morever the scrollbar appearing seems to be at the right end of the image, not near the frame. – Project-A Feb 03 '17 at 11:24
  • 1
    @Project-A : maybe try to set a `BorderLayout` on `panel2` . – Arnaud Feb 03 '17 at 12:40
  • @Berger. I tried it panel2.setLayout(new BorderLayout()); and it seems to be working fine. But do you anyways to increase the scroll speed. – Project-A Feb 03 '17 at 14:00
  • @berger the image seems to vary in size. How to fix it. – Project-A Feb 03 '17 at 14:09
  • For the scroll speed, this may help : http://stackoverflow.com/questions/5583495/how-do-i-speed-up-the-scroll-speed-in-a-jscrollpane-when-using-the-mouse-wheel – Arnaud Feb 03 '17 at 14:16
  • @berger the panel2 background is not working. I also tried label.setBackground it doesn't work either too. – Project-A Feb 03 '17 at 14:17
  • 1
    Try `scroll.getViewport().setBackground(new Color(0, 0, 0));` – Arnaud Feb 03 '17 at 14:24