File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Jlist listener problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Jlist listener problem" Watch "Jlist listener problem" New topic
Author

Jlist listener problem

Allen Bandela
Ranch Hand

Joined: Feb 16, 2006
Posts: 128

Hi all,
I have a strange problem I am unable to solve in my Jlist. I am posting the code below. Please , if anyone can help, try to catch the bug.

The program takes a phrase of keywords and searches them in a prestored list. It displays the results in a JList. The user can double click an item in the JList and a window would pop-up showing the respective document. There are two scenarios when I do not want the item in the list to pop-up. Those two scenarios are :
a) When the user has'nt entered any keyword, the JList displays
"Please enter a search phrase with each word seperated by space".
b) When no match for the entered keyword is found, the JList display
"No results found. Please try a more related keyword"

So, I checked for those two scenarios and disabled the listener during their appearance in the JList. It works the very first time I start the JApplet. Thereafter, it just does'nt. You can execute the following code by itself and run it in a browser. Its simple and its working. Please find the bug.I am new to programming, so any new suggestions are more than welcome. Thanks much in advance.

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.net.*;

public class IndexSearch1 extends JApplet implements ActionListener, MouseListener{

JButton button;
JTextField textfield;
JList list;
DefaultListModel model;
JScrollPane listScroller;
String text;
String listelement;

int pos,flag;
String[] URL;
String[] title;
String[] keyword;
String[] joined;
String[] searchString;
URL website;
ListSelectionModel listSelectionModel;

private void createAndShowGUI() {
//Force SwingApplet to come up in the system Look & Feel
//JApplet.setDefaultLookAndFeelDecorated(true);// not working
String laf=UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(laf);
} catch (UnsupportedLookAndFeelException exc) {
System.err.println("Warning: UnsupportedLookAndFeel: " + laf);
} catch (Exception exc) {
System.err.println("Error loading " + laf + ": " + exc);
}

getContentPane().setLayout(new FlowLayout());
button=new JButton("Go");
getContentPane().add(button);
button.addActionListener(this);

textfield=new JTextField(10);
getContentPane().add(textfield);


model=new DefaultListModel();
list=new JList(model);

list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//SPECIFIES HOW MANY ITEMS CAN BE SELECTED
list.setLayoutOrientation(JList.VERTICAL_WRAP);
list.setVisibleRowCount(-1);
getContentPane().add(list);
//listSelectionModel=list.getSelectionModel();
//listSelectionModel.addListSelectionListener(this);

listScroller=new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(250,80));
getContentPane().add(listScroller);

}




public void init()
{ //Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

public void actionPerformed(ActionEvent ae)
{
searchString=null;

list.addMouseListener(this);// Registering here so that everytime , user hits "go", the list starts listening.

while((pos=list.getModel().getSize())>0)
model.remove(pos-1);
text=textfield.getText();
if(text.length()>1){
searchString=text.split("\\s");// split the search phrase by spaces
searchIndex();
}
else
{
list.removeMouseListener(this);
model.add(pos,"Please enter a search phrase with each word seperated by space");

}
}

public void searchIndex()
{
URL=new String[50];
title= new String[50];
keyword=new String[50];

URL[0]="BOY.pdf";
title[0]="A test pdf file";
keyword[0]="test,file,boy";// any keyword

URL[1]="hello.txt";
title[1]=" A girl ";
keyword[1]="girl,file";


joined=new String[50];
int j=0;
for(int i=0;i<URL.length;i++)
{
joined[i]=title[i]+keyword[i]; // joining the title and the keyword
j++;
}

// Searching if the entered searchString contains any of the keywords

for(int k=0;k<j;k++){
flag=0;
for(int l=0;l<searchString.length;l++){

if(joined[k].indexOf(searchString[l])>-1)
{if(flag!=1){
int pos=list.getModel().getSize();

model.add(pos,URL[k]);}
flag=1;
}
}
}
if (list.getModel().getSize()==0)// If no results are found
{
list.removeMouseListener(this);
model.add(pos,"No results found. Please try a more related keyword");
System.out.println("removing mouse listener...");
}
}


public void mouseClicked(MouseEvent evt) {

list=(JList)evt.getSource();
if(evt.getClickCount()==2) { //Double Click
System.out.println("MOuse clicked double");
// Get item index
int index2=list.locationToIndex(evt.getPoint());
listelement=(String)model.elementAt(index2);
//System.out.println(listelement);

try{
website=new URL(getDocumentBase(),listelement);
this.getAppletContext().showDocument( website, "_blank" );// Requests that the browser show the Web page indicated by the URL

document
}catch(Exception e)
{
System.out.println("Some exception has occured boy");
}
}
}

public void mouseExited(MouseEvent evt1){
}
public void mouseEntered(MouseEvent evt2){
}
public void mouseReleased(MouseEvent evt3){
}
public void mousePressed(MouseEvent evt4){
}

}
 
GeeCON Prague 2014
 
subject: Jlist listener problem