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.

QQ

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;

}

}

>

[7802 byte] By [break_the_chaina] at [2007-11-27 10:50:48]
# 1

First thing before going solution.

Any particular reason for using JApplet rather then JFrame? Applet has certain restriction on File and Directory usage of hard drive. I would use JFrame if you are developing stand alone application.

For quick JFrame example, visit: http://www.exampledepot.com/egs/javax.swing/pkg.html

Regards,

D

dhaval04a at 2007-7-29 11:27:30 > top of Java-index,Desktop,Core GUI APIs...
# 2

Can I embed a JFrame in a web page?

'Cause I'd like to. I understand this will cause further issues with file system security eventually, but I'll cross that bridge when i come to it... I guess.

Message was edited by:

break_the_chain

break_the_chaina at 2007-7-29 11:27:30 > top of Java-index,Desktop,Core GUI APIs...
# 3

> Can I embed a JFrame in a web page?

Proves you really are a beginner. JFrame is used when developing applications for the Desktop, such as your standard ms word, excel, etc applications. Java is not only restricted to running from a browser and a greater percentage of all current java software created using swing are desktop apps.

If you really are interested in swing, then you should be looking at creating a Desktop application rather than an Applet (unless you required to do so at the moment). Besides, your current code looks more like an attempt at making a desktop application than an applet, IMHO.

ICE

icewalker2ga at 2007-7-29 11:27:30 > top of Java-index,Desktop,Core GUI APIs...
# 4

Never suggested I wasn't a n00b, but I am picking some things up.

I have a pretty lacadaisical job, so I'm trying to learn something and be productive simultaneously. This does need to be on a web page, but i figured the swing library had some widgets I could use. I'm currently trying to get the jar signed so I can still access the client file system from the applet.

I = better at .net

break_the_chaina at 2007-7-29 11:27:30 > top of Java-index,Desktop,Core GUI APIs...
# 5

So you're saying that I should look somewhere besides swing if I want to create an applet?

break_the_chaina at 2007-7-29 11:27:30 > top of Java-index,Desktop,Core GUI APIs...
# 6

> So you're saying that I should look somewhere besides swing if I want to create an applet?

Never said that. Swing would probably be the best option for creatring applets, since it provides several components that enable you to develop a whole application within a browser. You should just ensute you do not mix AWT components with SWING components. This usually causes some problems with rendering of stuff.

If you look in your JDK demos you'll find there is an Applet version of the SwingSet2 demo. Very good example of a Swing Baased Applet

ICE

icewalker2ga at 2007-7-29 11:27:30 > top of Java-index,Desktop,Core GUI APIs...