Tazmanian Tad
Tazmanian Tad

Reputation: 313

Extending a JPanel will not display

I've been searching for an answer, but everything else comes very close, but isn't the problem I'm having.

So my main class creates a new JFrame, adds a panel as the content panel, and I add a scrollpanel to that content panel.

Now I create some of my extending JPanel classes, add them to the scroll pane and see nothing but an empty frame.

And I have checked to make sure there is indeed a list of FTPFile's

Here is the main code:

public browser(ftpHandler _FTP) {

    FTP = _FTP;
    Panels = new ArrayList<JPanel>();

    frame = new JFrame("File Browser");
    frame.setContentPane(mainPanel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(750, 500));
    frame.setSize(frame.getPreferredSize());
    frame.setMinimumSize(frame.getSize());

    mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

    listFiles();

    frame.pack();
    frame.setVisible(true);
}

public void listFiles(){
    Panels.clear();
    FTPFile[] list = null;
    try {
        list = FTP.listFiles();
    } catch (Exception e) {
        e.printStackTrace();
    }

    for(FTPFile file : list){
        fileObject FO = new fileObject(file);
        Panels.add(FO);
        scrollPane.add(FO);
    }

    scrollPane.updateUI();
}

And my extended JPanel, fileObject

public class fileObject extends JPanel {
private FTPFile file;
private JLabel Label;
private ImageIcon Icon;
private int FileType;
private final int IconSize = 25;
private final Dimension panelSize = new Dimension(150, 40);

public fileObject(FTPFile FILE){
    file = FILE;
    FileType = file.getType();
    this.setSize(panelSize);
    this.setPreferredSize(panelSize);
    this.setMinimumSize(panelSize);
    this.setLayout(new WrapLayout());

    switch (FileType){
        case FTPFile.TYPE_DIRECTORY:
            Icon = resizeImage(new ImageIcon(getClass().getResource("/com/taylor/48px/folder.png")),IconSize);
            break;
        case FTPFile.TYPE_FILE:
            try {
                String FileExtension = file.getName().substring(file.getName().lastIndexOf(".")+1);
                Icon = resizeImage(new ImageIcon(getClass().getResource("/com/taylor/48px/"+FileExtension+".png")),IconSize);
            } catch(Exception e) {
                Icon = resizeImage(new ImageIcon(getClass().getResource("/com/taylor/48px/_blank.png")),IconSize);
            }
            break;
        case FTPFile.TYPE_LINK:
            Icon = resizeImage(new ImageIcon(getClass().getResource("/com/taylor/48px/_page.png")),IconSize);
            break;
    }
    Label = new JLabel(file.getName(), Icon, JLabel.LEFT);
    this.add(Label);

}

private ImageIcon resizeImage(ImageIcon II, int Size){
    Image img = II.getImage();
    BufferedImage resizedImage = new BufferedImage(Size, Size, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = resizedImage.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(img, 0, 0, Size, Size, null);
    g2.dispose();
    return new ImageIcon(resizedImage);
}
}

Upvotes: 0

Views: 68

Answers (1)

Tazmanian Tad
Tazmanian Tad

Reputation: 313

I got this working, it turns out I was had my panels set up wrong, they were setup like this:

JFrame

--mainPanel

----scrollPane

And I was adding my extended panel to the scrollPane

And this seemed to not work, so I added another panel inside the scrollPane and started adding my extended panels to this new panel and it started working!

Upvotes: 1

Related Questions