Not sure if I am adding it to the wrong layer. Probably more than a few mistakes on this. I added it to the applet because I will be removing the JFrame. The JFrame is for testing purposes only. I don't know if I am setting up the scroll pane wrong or what. The end goal is to add a picture to the jpanel with the button and be able to scroll if it is larger than the panel.
public class main {
public static JFrame f = new JFrame();
public static JApplet a = new JApplet();
public static JPanel p = new JPanel();
public static JButton b = new JButton();
public static JScrollPane pane = new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
public static JLabel gameimage;
public static void main(String[] args) {
f.setSize(1000,800);
a.setBounds(0,0,1000,800);
a.setVisible(true);
a.setBackground(Color.WHITE);
p.setBounds(0, 0, 1000, 800);
p.setVisible(true);
p.setBackground(Color.WHITE);
p.setLayout(null);
p.setOpaque(true);
pane.getVerticalScrollBar();
pane.getHorizontalScrollBar();
pane.setVisible(true);
b.setBounds(955, 0, 40, 30);
b.setText("+");
b.setFont(new Font("Times Roman", Font.BOLD, 30));
b.setVisible(true);
b.setBorder(javax.swing.BorderFactory.createLineBorder(Color.WHITE));
b.setBackground(Color.WHITE);
b.setForeground(Color.GREEN);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JFileChooser file = new JFileChooser();
file.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
file.setAcceptAllFileFilterUsed(true);
if(file.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
int width;
int height;
File f = file.getSelectedFile();
try {
BufferedImage bimg = ImageIO.read(new File(f.getAbsolutePath()));
width = bimg.getWidth();
height = bimg.getHeight();
String fname = f.getAbsolutePath();
gameimage = new JLabel("", new ImageIcon(fname), JLabel.CENTER);
gameimage.setSize(width,height);
gameimage.setOpaque(true);
a.revalidate();
a.repaint();
p.removeAll();
p.revalidate();
p.repaint();
pane.revalidate();
pane.repaint();
p.setSize(width,height);
p.add(gameimage);
p.add(b);
//p.add(pane);
a.getContentPane().add(pane);
}catch(IOException ioe) {
}
}else {
System.out.println("not working");
}
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setResizable(false);
f.add(a);
a.add(p);
p.add(b);
}
}