wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes regarding jFileChooser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "regarding jFileChooser" Watch "regarding jFileChooser" New topic
Author

regarding jFileChooser

kannan D V
Greenhorn

Joined: May 07, 2008
Posts: 4
i need the jList component from the Jfilechooser component.
using the Jlist i need to add keyListener
here is the code to get the Jlist from jfileChooser

private Component findJList(Component component_)
{
if(component_.getClass()==JList.class)
return component_;
else if(component_ instanceof Container)
{
Component[] components_=((Container)component_).getComponents();
for(Component comp_:components_)
{
Component childComponent_=findJList(comp_);
if(childComponent_!=null)
return childComponent_;
}
}
return null;
}


there parameter 'component_' is a jfileChooser object.
what my problem is i get null pointer exception
can we access the jList from the jfileChooser?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"kannan DV",
Please check your private messages regarding an important administrative matter.
-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by kannan DV:
i need the jList component from the Jfilechooser component.
using the Jlist i need to add keyListener
here is the code to get the Jlist from jfileChooser

...
if(component_.getClass()==JList.class)


Here your test succeeds if component_ is a JList, but not if it is
a subclass of JList. I would imagine that some LnFs would use
subclasses, so perhaps you should change this to:
if (component_ instanceof JList)

Keep in mind that an LnF could do anything. In fact, I believe that
there will be two JLists under MotifLookAndFeel, one for folders
and one for files. It's possible for an LnF to have no JLists at all.



Why do you need to add a KeyListener?



[note: The image was hotlinked from here.]
[ May 07, 2008: Message edited by: Brian Cole ]

bitguru blog
kannan D V
Greenhorn

Joined: May 07, 2008
Posts: 4
what i am trying is 'Speed up file searching in JFileChooser'.
it is a sample program.
there they get the JList from the Jfilechooser then add keyListener to the jList.
when user type some 'string' i get the string and search in the file list and set the fileChooser to that file.like below,

fileChooser_.setSelectedFile(file_);
here file_ is where i get form the list.

the code for this logic is given below

public class SearchFastLogic extends KeyAdapter implements PropertyChangeListener{

JFileChooser fileChooser_=null;
StringBuffer keyTypedTemp_=new StringBuffer();
Vector files;
private boolean resetPartialName = true;

public SearchFastLogic(JFileChooser fileChooser_) {
// TODO Auto-generated constructor stub
this.fileChooser_=fileChooser_;
Component component_=findJList(fileChooser_);
component_.addKeyListener(this);
setListDataListener();
fileChooser_.addPropertyChangeListener(this);
}

private void setListDataListener() {
final BasicDirectoryModel model = ((BasicFileChooserUI)fileChooser_.getUI()).getModel();
model.addListDataListener(new ListDataListener() {
public void contentsChanged(ListDataEvent lde) {
Vector buffer = model.getFiles();
if (buffer.size() > 0) {
files = buffer;
}
}
public void intervalAdded(ListDataEvent lde) {}
public void intervalRemoved(ListDataEvent lde) {}
});
}

private Component findJList(Component component_)
{
if(component_.getClass()==JList.class)
return component_;
else if(component_ instanceof Container)
{
Component[] components_=((Container)component_).getComponents();
for(Component comp_:components_)
{
Component childComponent_=findJList(comp_);
if(childComponent_!=null)
return childComponent_;
}
}
return null;
}

@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getKeyChar()==KeyEvent.VK_ENTER)
if(fileChooser_.getSelectedFile().isFile())
fileChooser_.approveSelection();

keyTypedTemp_.append(arg0.getKeyChar());
String upperCaseKeyTypedTemp_=keyTypedTemp_.toString().toUpperCase();
for(int i=0;i<files.size();i++)
{
File file_=(File)files.get(i);
String fileName_=file_.getName().toUpperCase();
if(fileName_.startsWith(upperCaseKeyTypedTemp_))
{
resetPartialName=false;
fileChooser_.setSelectedFile(file_);
return;
}
}
}

@Override
public void propertyChange(PropertyChangeEvent arg0) {
// TODO Auto-generated method stub
String propertyName = arg0.getPropertyName();
if (propertyName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
if (resetPartialName) keyTypedTemp_.setLength(0);
resetPartialName = true;
}
}

}
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"kannan D V",
Please check your private messages.
-Ben
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regarding jFileChooser
 
Similar Threads
Count items in JList
JFileChooser opendialog question
JFileChooser override
Problem with getSelectedValues() ...!!!
problem selecting JList item