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