Praburaj
Praburaj

Reputation: 613

Adding JScrollPane to a class that extend from JPanel

I am creating an application which displays the list of files and folder from the network.How do I add JScrollPane to a class the extends from JPanel. The JPanel contains JButtons which indicate the files and folders.

package fileviewer;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/*
 * @author      : GUNNER.
 * Title        : Explorer. 
 * Description  : You can browse the folders and download the files using this.
 */

@SuppressWarnings("serial")
public class FoldersAndFiles extends JPanel {
    private final static int PADDING = 20;
    private ArrayList<FolderAndFilePaths> totalFoldersAndFiles;
    @SuppressWarnings("unused")
    private ArrayList<JButton> buttons;
    private BufferedImage myImage;

    public FoldersAndFiles() {
        // super("Explorer");
        setLayout(new FlowLayout(FlowLayout.LEFT, PADDING, PADDING));
        buttons = new ArrayList<JButton>();
        totalFoldersAndFiles = new ArrayList<FolderAndFilePaths>();

    }

    public void addToTotalFolderAndFiles(FolderAndFilePaths folderAndFile) {
        if (this.totalFoldersAndFiles != null)
            this.totalFoldersAndFiles.add(folderAndFile);
        createFoldersAndFiles(folderAndFile);
    }

    private void setGBC(GridBagConstraints g, int width, int height, int gridX,
            int gridY) {
        g.gridheight = height;
        g.gridwidth = width;
        g.gridx = gridX;
        g.gridy = gridY;
    }

    private void createFoldersAndFiles(FolderAndFilePaths folderAndFile) {

        FolderClickHandler folderClickHandler = new FolderClickHandler();
        FileClickHandler fileClickHandler = new FileClickHandler();

        // Here is the button, Panel and layouts which we set for the explore.
        JButton button = new JButton();
        JPanel panel = new JPanel();
        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();

        // This loop adds the available folders in the given path
        for (String folder : folderAndFile.getFolderNames()) {
            button = new JButton();
            panel = new JPanel();
            panel.setLayout(layout);

            // Here we set the image for the folder.
            try {
                myImage = ImageIO.read(new File("res/folder.png"));
                button.setIcon(new ImageIcon(myImage));
                new JLabel("file").setLabelFor(button);
            } catch (IOException e) {
                e.printStackTrace();
            }

            button.setName("f" + folder);

            // This is to set the backgrouond of the button as transparent.
            button.setOpaque(false);
            button.setContentAreaFilled(false);
            button.setBorderPainted(false);

            // Here we set the Constraints for the buttons.
            setGBC(constraints, 1, 1, 0, 0);
            layout.setConstraints(button, constraints);
            panel.add(button);

            // This registers the Event Handler for the folder.
            button.addMouseListener(folderClickHandler);

            // This sets the label and Constraints for the corresponding folder.
            JLabel label = new JLabel(folder);
            setGBC(constraints, 1, 1, 0, 1);
            layout.setConstraints(label, constraints);
            panel.add(label);
            add(panel);
        }

        // This loop adds the available files in the given path
        for (String file : folderAndFile.getFileNames()) {
            button = new JButton();
            panel = new JPanel();
            panel.setLayout(layout);

            // Here we set the image for the files.
            try {
                myImage = ImageIO.read(new File("res/file.png"));
                button.setIcon(new ImageIcon(myImage));
            } catch (IOException e) {
                e.printStackTrace();
            }

            // This is to set the backgrouond of the button as transparent.
            button.setOpaque(false);
            button.setContentAreaFilled(false);
            button.setBorderPainted(false);

            button.setName("F" + file);

            setGBC(constraints, 1, 1, 0, 0);
            layout.setConstraints(button, constraints);
            panel.add(button);

            // This register the Event Handler for the files.
            button.addMouseListener(fileClickHandler);

            // This sets the label and Constraints for the corresponding file.
            JLabel label = new JLabel(file);
            setGBC(constraints, 1, 1, 0, 1);
            layout.setConstraints(label, constraints);
            panel.add(label);
            add(panel);
        }

        setVisible(true);
    }

    private class FolderClickHandler extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent event) {
            // This is used to set the background color for the selected file or
            // folder.
            if (event.getClickCount() == 1) {
                // Here we get the button and set color.

            }

            // This is used to check for double clicks.
            if (event.getClickCount() == 2) {
                // We print the folder / file name to start with
                System.out.println(((JButton) event.getSource()).getName());
            }

        }

    }

    private class FileClickHandler extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent event) {
            // This is used to set the background color for the selected file or
            // folder.
            if (event.getClickCount() == 1) {
                // Here we get the button and set color.

            }

            // This is used to check for double clicks.
            if (event.getClickCount() == 2) {
                // We print the folder / file name to start with
                System.out.println(((JButton) event.getSource()).getName());
            }

        }

    }

}

Upvotes: 0

Views: 1286

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347194

Start by reading through How to Use Scroll Panes

There are at least two was to do this...

First, by specifying the view component via the constructor...

JScrollPane scrollPane = new JScrollPane(panel);

Second, vie the setViewportView method...

scrollPane.setViewportView(panel);

Alternatively, you can set the JViewports view directly...

scrollPane.getViewport().setView(panel);

Make sure you are adding the scroll pane to the display...for example...

JFrame frame = ...
JPanel panel = ...
frame.add(new JScollPsne(panel));

Upvotes: 2

Braj
Braj

Reputation: 46841

Try this one

    JPanel panel=new MyPanel();
    JScrollPane pane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    pane.getViewport().add(panel);

Here MyPanel is a class that extend from JPanel.

You can set the horizontal and vertical scroll bars visibility also.


-- EDIT --

Please have a look at main method.

public static void main(String[] a) {
    JFrame fram = new JFrame();
    FoldersAndFiles b = new FoldersAndFiles();

    JScrollPane pane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    pane.getViewport().add(b);
    fram.getContentPane().add(pane);

    b.addToTotalFolderAndFiles(new FolderAndFilePaths());
    fram.pack();
    fram.setVisible(true);
    fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

Upvotes: 2

Related Questions