1

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);
    }
}
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
  • 1
    Please create a [MCVE]. Currently there is a lot of stuff that is likely not causing the problem. The smaller the program is the more likely people will try to work on it to solve the problem. Another suggestion: take the [tour] to get to know the site a little. And welcome to SO – geisterfurz007 Oct 11 '17 at 08:36
  • 2
    Don't use a `null` layout. – trashgod Oct 11 '17 at 09:11
  • Call `f.setVisible(true);` last - also, only call `revalidate`/`repaint` on the containers you've actually changed, after you've changed them – MadProgrammer Oct 11 '17 at 09:17
  • *"I added it to the applet because I will be removing the JFrame"* - That seems pointless as applets are no longer support and are deprecated – MadProgrammer Oct 11 '17 at 09:19
  • `JScrollPane`s relay on the view port component providing sizing hints via the `preferred/minimum/maximum` properties, which is normally provided by `LayoutManager`s, by using `null` layouts you've effective removed that support – MadProgrammer Oct 11 '17 at 09:21
  • Thank you trashgod I deleted the layout and it started working. – Chargers0202 Oct 11 '17 at 09:27

1 Answers1

2

I'm not going to go into a lot of detail, I've comment the code instead

The key points are:

  • Avoid null layouts, they never work and the Swing API is designed to operate the layout manager API
  • Applets are deprecated, you won't get any functionality out of them that you can't get from JFrame or other window based components

While I've not taken to direct task, static is also a bad idea (in the context that you are using it), try and avoid it if you can

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class Main {

    public static JFrame f = new JFrame();
    // Pointless, applets are deprecated
    // Besides, this isn't how you use them
    //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) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                f.setSize(1000, 800);

                //a.setBounds(0, 0, 1000, 800);
                //a.setVisible(true);
                //a.setBackground(Color.WHITE);
                // Pointless, the layout manager of the JScrollPane
                // will make these choices
                //p.setBounds(0, 0, 1000, 800);
                p.setVisible(true);
                p.setBackground(Color.WHITE);
                // !! This is root of your problem !!
                //p.setLayout(null);
                p.setLayout(new BorderLayout());
                // Pointless, it already is
                //p.setOpaque(true);

                // Pointless, you don't do anything with the returned value
                //pane.getVerticalScrollBar();
                //pane.getHorizontalScrollBar();
                // Pointless, it already is
                //pane.setVisible(true);
                // Pointless
                //b.setBounds(955, 0, 40, 30);
                b.setText("+");
                b.setFont(new Font("Times Roman", Font.BOLD, 30));
                // Pointless
                //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();
                                if (gameimage != null) {
                                    p.remove(gameimage);
                                }
                                gameimage = new JLabel("", new ImageIcon(fname), JLabel.CENTER);
                                gameimage.setSize(width, height);
                                gameimage.setOpaque(true);

                                //a.revalidate();
                                //a.repaint();
                                //p.removeAll();
                                // Not really what you want to do right now
                                // Besides, if you use a null layout, it won't do anything
                                //p.revalidate();
                                //p.repaint();
                                //pane.revalidate();
                                //pane.repaint();
                                //p.setSize(width, height);
                                p.add(gameimage);
                                //p.add(b);
                                //p.add(pane);
                                // Presumaly, pane should alredy be added to a container
                                //a.getContentPane().add(pane);
                                p.revalidate();
                                p.repaint();
                            } catch (IOException ioe) {

                            }

                        } else {
                            System.out.println("not working");
                        }
                    }
                });

                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                // Not what you want to call here
                //f.setVisible(true);
                // Probably not the best idea right now
                f.setResizable(false);
                //f.add(a);
                //a.add(p);
                // Good idea to add the scollpane to the container
                f.add(pane);
                p.add(b, BorderLayout.SOUTH);
                f.setVisible(true);
            }
        });
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366