Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-09-2008, 11:03 PM
Member
 
Join Date: Jan 2008
Posts: 16
newtojava7 is on a distinguished road
searching within a JList
hello, I have a bunch of empty textfiles i am using to represent as songs to test a music player im working on for an assignment. basically i use my open button to allow the user to search directories, and i select the folder with all these textfiles. all of the artists are shown in a JList. I have a textfield with the button search beside it, I want to allow the user to type in an artist, and have my program search for the artist within the JList and find it. how can I do this??
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-10-2008, 01:12 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SearchTest { JList list; JLabel label; private void search(String text) { DefaultListModel model = (DefaultListModel)list.getModel(); // Case–sensitive. if(model.contains(text)) { int index = model.indexOf(text); list.setSelectedIndex(index); label.setText(text + " found at index " + index); } else { list.clearSelection(); label.setText(text + " not found"); } } private JScrollPane getListComponent() { String[] items = { "Bob", "Ted", "Carol", "Alice" }; DefaultListModel model = new DefaultListModel(); for(int j = 0; j < items.length; j++) model.add(j, items[j]); list = new JList(model); return new JScrollPane(list); } private JPanel getFirst() { final JTextField textField = new JTextField(12); JButton button = new JButton("Search"); ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent e) { String text = textField.getText(); search(text); } }; textField.addActionListener(al); button.addActionListener(al); JPanel panel = new JPanel(); panel.add(textField); panel.add(button); return panel; } private JLabel getLabel() { label = new JLabel(); label.setHorizontalAlignment(JLabel.CENTER); Dimension d = label.getPreferredSize(); d.height = 25; label.setPreferredSize(d); return label; } public static void main(String[] args) { SearchTest test = new SearchTest(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test.getListComponent()); f.add(test.getFirst(), "First"); f.add(test.getLabel(), "Last"); f.setSize(360,240); f.setLocation(200,200); f.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JList problem zizou147 Advanced Java 1 04-17-2008 09:50 AM
searching nalinda New To Java 3 12-06-2007 03:56 AM
Dialog and JList Doubts hemanthjava AWT / Swing 0 12-05-2007 09:42 PM
Help with JList Albert NetBeans 1 07-13-2007 04:42 PM
add a jlist column Alan JCreator 1 05-28-2007 05:51 AM


All times are GMT +3. The time now is 12:47 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org