0

The code is as such:

public static class Fileslist extends JFrame
    {
        List<Container> folderbuttons= new ArrayList<Container>(); 
        List<JButton> folderbuttons2= new ArrayList<JButton>(); 
        List<DbxEntry> filesandfolders=new ArrayList<DbxEntry>();
        ArrayList<Item> kal = new ArrayList<Item>();
        public Fileslist(String path) throws NetworkIOException, DbxException, IOException
        {
            addFolders(path);
            int count=0;
            JScrollPane panel= new JScrollPane();
            JScrollPane mainpanel= new JScrollPane();
            DefaultListModel<String> model= new DefaultListModel<String>();
            JPanel jaypanel = new JPanel();
            for(Container c : folderbuttons)
            {
                jaypanel.add(c);
            }
            for(DbxEntry c: filesandfolders)
            {
                if(c.isFile())
                {
                    if(model.contains(c.name+" "+" File"+" "+c.asFile().lastModified))
                    {
                        model.addElement(c.name+" "+" File"+" "+c.asFile().lastModified);
                    }
                }
            }
            JList<String> filesfolders= new JList<String>(model);
            filesfolders.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
            Container c = new Container();
            c.add(filesfolders);
            panel.add(c);
            panel.setSize(200,200);
            JTextField text = new JTextField("",2);
            text.addKeyListener(new KeyAdapter() 
            {
                public void keyTyped(KeyEvent e) 
                {
                    if (e.getKeyChar() == KeyEvent.VK_ENTER) 
                    {
                        try {
                            ArrayList<Item> black=getFilesStartingWith(path, text.getText());
                            DbxClientV1 client = new DbxClientV1(config, ACCESS_TOKEN);
                            client.createFolder(path+"/"+text.getText());
                             System.out.println("Entered:  "+ text.getText());
                            for(int i=0; i<black.size();i++)
                            {
                                client.move("/"+black.get(i).mainpath, "/"+black.get(i).mainpath+"/"+text.getText()+"/"+black.get(i).name);
                            }
                            System.out.println("Entered: "+ text.getText());
                        } 
                        catch (DbxException e1) 
                        {
                            e1.printStackTrace();
                        }
                        System.out.println("Entered:"+ text.getText());
                    }
                }
            });
            jaypanel.setSize(400, 400);
            mainpanel.add(jaypanel);
            mainpanel.setSize(400,400);
            mainpanel.setLayout(new ScrollPaneLayout());
            getContentPane().add(mainpanel);
            getContentPane().add(panel);
            getContentPane().add(text);
            if(filesandfolders.size()==0)
            {
                System.out.println("No files");
            }
            setSize(600, 600);
            repaint();
            setLayout(new GridLayout(3,3));
            setVisible(true);
        }
        public void addFolders(String path) throws NetworkIOException, DbxException, IOException
        {
            ArrayList<String> folders= new ArrayList<String>();
            folders.add("/");
            int count=0;
            DbxClientV1 client = new DbxClientV1(config, ACCESS_TOKEN);
            List<Metadata> newmeta= new ArrayList<Metadata>(2);
            folders=getAllFolders2(newmeta, client);
            WithChildren result = client.getMetadataWithChildren(path, true);
            filesandfolders = result.children;
            for(String kal:folders)
            {
                JButton addtoMain= new JButton(kal);
                addtoMain.setSize(150,150);
                addtoMain.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e)
                {
                    //Execute when button is pressed
                    try {
                        Fileslist filesframe=new Fileslist(kal);
                    } catch (NetworkIOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (DbxException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            });
            Container contain= new Container();
            contain.setSize(150, 150);
            contain.setLayout(new FlowLayout());
            contain.add(addtoMain);
            folderbuttons.add(contain);
            folderbuttons2.add(addtoMain);
        }

    }
    public ArrayList<Item> getFilesStartingWith(String path, String key) throws DbxException
    {
        ArrayList<Item> files = new ArrayList();
        DbxClientV1 client = new DbxClientV1(config, ACCESS_TOKEN);
        WithChildren result = client.getMetadataWithChildren(path, true);
        List<DbxEntry> cursor3 = result.children;
        for (DbxEntry entry : cursor3) 
        {
            if(entry.isFile())
            {
                Item newItem = new  Item(entry.path, path, entry.name);
                kal.add(newItem);
            }
        }
        for(int i=0; i<kal.size();i++)
        {
                if(kal.get(i).name.startsWith(key))
                {
                    System.out.println(path+" has under:*** "+kal.get(i).path+" also called "+kal.get(i).name);
                    files.add(kal.get(i));
                }
        }
        return files;
    }
}

This gives the following result: http://i558.photobucket.com/albums/ss30/magpiejay/Balksz_zpsuxdv6fvl.jpg~original

Where the end of the scrolling button is lost, and TextArea is unnecessarily large. So, what's going on?

Edit: Okay, I seemingly solved the problem. It was because I was adding elements directly to JScrollPane, instead of using .setViewportView().

((There was also an unrelated issue with the if clause, it was originally supposed to be the opposite of the current one)).

  • `DbxEntry` This seems to be a layout problem, so should not require any custom classes. For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 29 '16 at 22:06
  • `panel.setSize(200,200);` See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson May 29 '16 at 22:06

1 Answers1

1
    mainpanel.add(jaypanel);
    mainpanel.setSize(400,400);
    mainpanel.setLayout(new ScrollPaneLayout());
  1. Whenever you use a layout manager you should set the layout BEFORE adding components to the panel.

  2. However, there is no need to set the layout manager of the scroll pane.The scrollpane will set the layout automatically.

  3. Don't use setSize(..). The layout manger will determine the size and location of every component. Get ride of all the setSize(...) statements.

Read the section from the Swing tutorial on Layout Managers for working examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • When I get rid of setSize statements everything disappears. Besides, I need window to be in a set size. – Yusuf Kaya Kuzu May 30 '16 at 08:21
  • @YusufKayaKuzu, If you use layout managers properly and pack() the frame all the components will display properly. There is no need for a null layout or to use the setSize() method. I forgot to include the link to the `Layout Manager` tutorial. My answer has been updated. Read the tutorial. Download the demo code and learn to use Swing the way it was designed to be used. – camickr May 30 '16 at 20:31