Adding a JList to a JScrollPane
Searching through the archives, this appears to be a pretty frequent question, but so far, none of the answers have worked for me. Behold, what I have:
Filelist =new JList(Filelistmodel);
FilePane =new JScrollPane();
FilePane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
FilePane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
FilePane.setMaximumSize(new Dimension(650,150));
FilePane.setMinimumSize(new Dimension(650,150));
FilePane.setPreferredSize(new Dimension(650,150));
FilePane.setBounds(150, 0, 650, 150);
FilePane.setViewportView(Filelist);
panel.add(FilePane);
Message was edited by:
break_the_chain
# 4
Ok, here's the context. It's not exactly simple, but it should compile and the rest of the code is more or less outside of the problem.
package formshow;
//import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.io.*;
import java.util.*;
public class Sync extends JApplet
{
JButton bttn1;
JComboBox Cbox_drives, Cbox_folders;
JPanel panel;
JFrame aframe;
JScrollPane FilePane;
DefaultListModel Filelistmodel;
JList Filelist;
class OnlyPRE implements FilenameFilter
{
Vector PRE;
public OnlyPRE(Vector PRE)
{this.PRE = PRE;}
public boolean 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;
}
}
public void init()
{
//applet listener
this.addFocusListener( new FocusListener()
{
public void focusGained(FocusEvent evt)
{
getContentPane().invalidate();
getContentPane().validate();
}
public void focusLost(FocusEvent evt)
{
getContentPane().invalidate();
getContentPane().validate();
}
}
);
//this applet
this.setSize(800,150);
//jpanel
panel = new JPanel(null);
//Button init stuff
bttn1 = new JButton("Get Files");
bttn1.setSize(150,50);
bttn1.setLocation(0,100);
bttn1.addActionListener( new ActionListener()
{
public void 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()
{
public void actionPerformed(ActionEvent evt)
{
try{
getPatFolders();
}
catch (Exception e)Cbox_drives.setBackground(Color.RED);}
} }
);
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);
}
private int getPatFolders () throws Exception
{
String drive = (String) Cbox_drives.getSelectedItem();
String files[];
Vector prelist = new Vector();
prelist.addElement("PAT");
prelist.addElement("REL");
prelist.addElement("EME");
FilenameFilter onlyget = new OnlyPRE(prelist);
Cbox_folders.removeAllItems();
try{
File rootFolder = new File(drive);
files = rootFolder.list();
}
catch (Exception E)
{
files = new String[]{"Error Here"};
}
for (int i=0; i<files.length; i++)
Cbox_folders.addItem((String)files[i]);
return (1);
}
private int PopList()
{
File StartFolder = new File(((String)Cbox_drives.getSelectedItem())+
(String)Cbox_folders.getSelectedItem()));
PopList(getFiles(StartFolder));
Filelist.removeAll();
Filelist = new JList(Filelistmodel);
FilePane = new JScrollPane();
FilePane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
FilePane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
FilePane.setMaximumSize(new Dimension(650,150));
FilePane.setMinimumSize(new Dimension(650,150));
FilePane.setPreferredSize(new Dimension(650,150));
FilePane.setBounds(150, 0, 650, 150);
FilePane.setViewportView(Filelist);
panel.add(FilePane);
getContentPane().invalidate();
getContentPane().validate();
return(1);
}
private int PopList(Vector transmute)
{
Filelistmodel = new DefaultListModel();
for (int i=0; i><transmute.size(); i++)
{
Filelistmodel.addElement(transmute.elementAt(i));
}
return(1);
}
private Vector getFiles(File ThisPath)
{
Vector ThisLevel = new Vector();
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 Vector append(Vector Left, Vector Right)
{
for (int i=0; i><Right.size();i++)
Left.addElement(Right.get(i));
return Left;
}
}
Ok, basically this is for windows users. Pick a drive letter, then pick a folder from the next combo box.
Hit the button and a list of all of the files in the folders and subfolders shows up in the JList on the right.
DO NOT try this when the second list box is pointing to something like windows or program files.
I'd recommend creating your own folder that goes maybe one or two levels deep with just a bunch of empty text files.
I will ><3 anyone that can help me with this forever and bestow upon them rediculous numbers of dukestars.
Message was edited by:
break_the_chain