Weirdness and ignorance
This is a semi-continuation of another thread in the 'new to java' forum. I'm learning swing and I'm looking for help as I go. Right now I've got a 'lil mini-gui that will let you point to any folder and it will list all of the files subfolders, sufiles, sub-sub files, etc. in that folder. The problem now is that if I resize the window or it loses focus, the list gets wiped. Also the scrollpane isn't scrolling.
Any suggestions?
package formshow;
//import java.applet.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
publicclass Syncextends JApplet
{
JButton bttn1;
JComboBox Cbox_drives, Cbox_folders;
JPanel panel;
JFrame aframe;
JScrollPane FilePane;
DefaultListModel Filelistmodel;
JList Filelist;
class OnlyPREimplements FilenameFilter
{
Vector PRE;
public OnlyPRE(Vector PRE)
{this.PRE = PRE;}
publicboolean accept(File dir, String name)
{
boolean accept =false;
for (int i=0; i<PRE.size(); i++)
{
if (name.startsWith((String)PRE.get(i)))
accept =true;
}
return accept;
}
}
publicvoid init()
{
//this applet
this.setSize(800,150);
//jpanel
panel =new JPanel(null);
//Button init stuff
bttn1 =new JButton("HAI ROLDR!!!");
bttn1.setSize(150,50);
bttn1.setLocation(0,100);
bttn1.addActionListener(new ActionListener()
{
publicvoid actionPerformed(ActionEvent evt)
{PopList();}
}
);
panel.add(bttn1);
//Cbox_drives init stuff
int offset ='a';
Cbox_drives =new JComboBox();
for (int i=0; i><26; i++)
Cbox_drives.addItem((char)(offset+i)+":\\");
Cbox_drives.setSize(150,50);
Cbox_drives.setLocation(0,0);
Cbox_drives.addActionListener(new ActionListener()
{
publicvoid actionPerformed(ActionEvent evt)
{getPatFolders();}
}
);
panel.add(Cbox_drives);
//Cbox_folders init stuff
Cbox_folders =new JComboBox();
Cbox_folders.setLocation(0,50);
Cbox_folders.setSize(150,50);
Cbox_folders.addItem("Pick the drive letter you have mapped to 1416_a");
panel.add(Cbox_folders);
//Filelist init stuff
Filelist =new JList();
Filelist.setVisibleRowCount(5);
//FilePane init stuff
FilePane =new JScrollPane(Filelist);
FilePane.setLocation(150,0);
FilePane.setSize(650,150);
panel.add(FilePane);
getContentPane().add(panel);
}
privateint getPatFolders()
{
String drive = (String)Cbox_drives.getSelectedItem();
Vector prelist =new Vector();
prelist.addElement("PAT");
prelist.addElement("REL");
prelist.addElement("EME");
FilenameFilter onlyget =new OnlyPRE(prelist);
Cbox_folders.removeAllItems();
File rootFolder =new File(drive);
String files[] = rootFolder.list(onlyget);
for (int i=0; i<files.length; i++)
Cbox_folders.addItem((String)files[i]);
return (1);
}
privateint PopList()
{
File StartFolder =new File(((String)Cbox_drives.getSelectedItem())+
((String)Cbox_folders.getSelectedItem()));
Filelistmodel = getFiles(StartFolder);
Filelist.removeAll();
Filelist =new JList(Filelistmodel);
FilePane =new JScrollPane(Filelist);
FilePane.setLocation(150,0);
FilePane.setSize(650,150);
panel.add(FilePane);
getContentPane().invalidate();
getContentPane().validate();
return(1);
}
private DefaultListModel getFiles(File ThisPath)
{
DefaultListModel ThisLevel =new DefaultListModel();
String FileList[] = ThisPath.list();
for (int i=0; i><FileList.length; i++)
{
File afile =new File(ThisPath.getAbsolutePath()+"\\"+FileList[i]);
if (afile.isFile())
ThisLevel.addElement(afile.getAbsolutePath());
else
append(ThisLevel,getFiles(afile));
}
return ThisLevel;
}
private DefaultListModel append(DefaultListModel Left, DefaultListModel Right)
{
for (int i=0; i><Right.getSize();i++)
Left.addElement(Right.get(i));
return Left;
}
}
>

