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

[905 byte] By [break_the_chaina] at [2007-11-27 10:58:48]
# 1

> Searching through the archives, this appears to be a

> pretty frequent question, but so far, none of the

> answers have worked for me.

I suspect this is mainly due to Layout problems.

So what you posted may well fix the symptom but you might want to give more thought to addressing the disease. Which is again some sort of Layout problems.

cotton.ma at 2007-7-29 12:18:34 > top of Java-index,Desktop,Core GUI APIs...
# 2

I suspect you're right, but I haven't stated the problem yet :-P. The list populates correctly, and the bar scroller resizes correctly to the amount of overage in the list, but the bar is locked. It won't move. BTW, this is a call for help thread, not a LOOK AT MAI UBERNESS.

Message was edited by:

break_the_chain

break_the_chaina at 2007-7-29 12:18:34 > top of Java-index,Desktop,Core GUI APIs...
# 3

I think you'll need to post a simple, compiliable, exceutable example so that camickr can reproduce the issue you are seeing and give you advice.

Sorry, I don't have a clue really, I have never seen that before (a scrollbar that should scroll but won't). I'm just here to ask my stupid applet question.

cotton.ma at 2007-7-29 12:18:34 > top of Java-index,Desktop,Core GUI APIs...
# 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

break_the_chaina at 2007-7-29 12:18:34 > top of Java-index,Desktop,Core GUI APIs...
# 5

I would be glad to help but your code does not compile correctly and when it finally does, there is no permission to access the local file system from within the Applet.

The code you provided is not the relevant code only since whatever security manager implementation you have is not provided.

And why do you type "><"? It is not even a standard java operator. I wonder how you get your code to compile.

ICE

icewalker2ga at 2007-7-29 12:18:34 > top of Java-index,Desktop,Core GUI APIs...
# 6

Ok, well this was copied and pasted right out of an eclipse window, so I'm not sure what kind of encapsulation it did before it showed me my applet, but it did compile and show it to me. The security manager must have been embedded in the container somewhere. The problem with the left angle bracket is that, I'm guessing, it gets messed with before it gets posted so that people can't inject html.

break_the_chaina at 2007-7-29 12:18:34 > top of Java-index,Desktop,Core GUI APIs...
# 7

1) Why are you building an applet that looks at the local file system? Applets can't read files from the local file system. You should be building an application (as was explained to you in your previous thread).

Code doesn't run on my machine because of a security error so I can't test your code.

2) Learn proper Java coding conventions for variable names. You don't upper case the first word or first character of the first word.

3) Read the JList API and following the "How to Use Lists" link for a working example.

camickra at 2007-7-29 12:18:34 > top of Java-index,Desktop,Core GUI APIs...
# 8

1.)http://www.developer.com/java/data/article.php/3303561

2.)This is somewhat of a popular but subjective thing, and should definitely not come into play with compilation.

3.)Thanks for the info, doing so right now!

PS explanation for why I'm building it to be an applet looking at the local filesystem:

I work in a testing division of an IT department. We also manage deployment from development servers to testing servers and Production servers.

Our wish is to deploy the final form of this application to our production server, and this application must be able to view the files on our development servers. However, we do not wish to mount our development filesystem on our production server for obvious security reasons. Our solution is to allow the applet to access the development server via our mapped network shares. That way, only those with access to the development server files can use this application effectively. This is more or less a proof of concept.

Message was edited by:

break_the_chain

break_the_chaina at 2007-7-29 12:18:34 > top of Java-index,Desktop,Core GUI APIs...
# 9

> 2.)This is somewhat of a popular but subjective

> thing, and should definitely not come into play with

> compilation.

True, but I think you'll find that as soon as you ask other people to help you with your code, your chances of getting help will increase if you follow common coding conventions, simply because it makes it easier for other people to read your code and understand what it does.

Torgila at 2007-7-29 12:18:34 > top of Java-index,Desktop,Core GUI APIs...
# 10

> 1.)http://www.developer.com/java/data/article.php/3303561

Well I'm not going to go to all that trouble just to test some code. Its up to you to make it easy for all of us to test. Read my comments in replies 27 and 30 of this posting for a better design which would allow you to post code we could execute:

http://forum.java.sun.com/thread.jspa?threadID=5145907&start=27

If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",

see http://homepage1.nifty.com/algafield/sscce.html,

that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

camickra at 2007-7-29 12:18:34 > top of Java-index,Desktop,Core GUI APIs...
# 11

So the whole problem was fixed by removing the size directives for the JScrollPane in the init method.

FilePane.setLocation(150,0);

FilePane.setSize(650,150);

All I had to do was comment those out and everything worked better than perfect.

10 stars to me HA!

Actually I'll give 'em to camickr for the effort

Message was edited by:

break_the_chain

break_the_chaina at 2007-7-29 12:18:34 > top of Java-index,Desktop,Core GUI APIs...