i have to filter the user input in the comboxbox under my "search tab"
eg. if the user were to type "y", "YAHOO" or any words related to "y" will appear on the filter list and allow the user to select. anyway my combobox values are taken from text file that are stored in local drive.

example of text file :
YAHOO, YHOO
APPLE,AAPL

basically, what are in the text file are the stocks name and its symbol. so just add as and when you like.

ps : please don't recommend using autocomplete. cause it's not what i needed. thanks in advance

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;


import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;

import javax.swing.table.DefaultTableModel;



public class fyp8 extends JFrame 
{
JTabbedPane tabbedPane;
			JPanel		panel1;
			JPanel		panel2;
			JPanel		panel3;
			JPanel		panel4;
			JPanel		panel5;
			JLabel 		read1;
			JLabel 		read2;
			JLabel 		read3;
			JLabel 		read4;
			
			JButton		butt1;
			JButton		butt2;
			
			JComboBox ipList = new JComboBox();
			
	        ArrayList<Object> Stock = new ArrayList<Object>();
	        String name, symbol;
	        
	        public fyp8(String n1, String s1) {
	            name = n1;
	            symbol = s1;
	        }
	        public String toString() {
	           return name;
	        }
 
	        //TableModel
	        String[] columnNames = {"Stock Name",
	        		"Last Price",
	        		"Change",
	        		"Volume",};
	        Object[][] stock= null;

	        		DefaultTableModel model = new DefaultTableModel(stock, columnNames);
	        		// final JTable table = new JTable(data, columnNames);
	        		final JTable table = new JTable(model);
	        		JScrollPane jsp = new JScrollPane(table);
	        		
public fyp8()
{
	// NOTE: to reduce the amount of code in this example, it uses
	// panels with a NULL layout.  This is NOT suitable for
	// production code since it may not display correctly for
	// a look-and-feel.
	

	 table.setPreferredScrollableViewportSize(new Dimension(500, 100));
	setTitle( "Stock Ticker" );
	setSize( 500, 300 );
	setBackground( Color.gray );

	JPanel topPanel = new JPanel();
	topPanel.setLayout( new BorderLayout() );
	getContentPane().add( topPanel );

	// Create the tab pages
	createPage1();
	createPage2();
	

	// Create a tabbed pane
	tabbedPane = new JTabbedPane();
	
	tabbedPane.addTab( "Stocks", panel1);
	tabbedPane.addTab( "Search", panel4  );
	
	topPanel.add( tabbedPane, BorderLayout.CENTER);
	

    String line = null;
  try
{
  FileInputStream fis = new FileInputStream(
                  ("C:/Java Sem 2/FYP SEM 1/stock.txt"));
  InputStreamReader isr = new InputStreamReader(fis);
  BufferedReader input = new BufferedReader(isr);
  while ((line = input.readLine()) != null){
          String[] StockInfo = line.split(",");  
          fyp8 myitem = new fyp8(StockInfo[0],StockInfo[1]);
          ipList.addItem(myitem);
          ipList.setEditable(true);
         
  
  }       
}

catch(FileNotFoundException FNF){
}
catch(IOException E)
{
}
	
	
}

public void createPage1()
{
	panel1 = new JPanel();
	panel1.setLayout(new GridLayout(1,0) );
	

	panel1.add(jsp );
	

}

	
public void createPage2()
{
	panel4 = new JPanel();
	panel4.setLayout(new BorderLayout() );
	
	panel5 = new JPanel();
	panel5.add(ipList);
	panel4.add(panel5);
	
	/*ipList.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
		public void keyTyped(KeyEvent e) {
			ipList_keyTyped(e);
		}
		public void keyPressed(KeyEvent e) {
			ipList_keyPressed(e);
		}
		public void keyReleased(KeyEvent e) {
			ipList_keyReleased(e);
		
		if (ipList.isDisplayable()) ipList.setPopupVisible(true);
	}
		});*/
	
	/*ipList.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
        public void keyTyped(KeyEvent e) {
        	ArrayList input = new ArrayList();
        	input.add(new fyp8( name, symbol));
        	 for (fyp8 s : input) { 
        		 if (ipList.equals(s.getName())) { 
        			 ipList.setPopupVisible(true);
        		 }
        	 }
        	 
        	 if (ipList.isDisplayable()) ipList.setPopupVisible(true);
        }
    });*/
	ipList.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
        public void keyTyped(KeyEvent e) {
        	if (ipList.isDisplayable()) ipList.setPopupVisible(true);
        }
    });
	
	  ipList.getEditor().getEditorComponent().addFocusListener(new FocusListener()
      {   	public void focusGained(FocusEvent ef)
                      {	ipList.setPopupVisible(true);
                      }
                      public void focusLost(FocusEvent ef){}
              });
        

	 
 ipList.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
         	String line = null;
                 try {                                                  
                 	String input1 = ((fyp8) ipList.getSelectedItem()).symbol;
                 	
                         URL url = new URL("http://download.finance.yahoo.com/d/quotes.csv?s="+input1+"&f=sl1d1t1c1ohgv&e=.csv");
                         URLConnection connection = url.openConnection();
         	            connection.setDoInput(true);
         	            InputStream inStream = connection.getInputStream();
         	            BufferedReader input =
         	            new BufferedReader(new InputStreamReader(inStream));
         	            while ((line = input.readLine()) != null){
         	            	String[] yahooStockInfo = line.split(","); 
         	            	model.addRow(new Object[]{yahooStockInfo[0], yahooStockInfo[1], yahooStockInfo[4], yahooStockInfo[8]});
         	            //	data[1].add(yahooStockInfo[0]);
         					//data[][].setText(yahooStockInfo[1]);
         					//data.setText(yahooStockInfo[4]);
         					//data.setText(yahooStockInfo[8]);
                                 
                        
                 	

                         }
     				} catch (IOException ex) {
     					ex.printStackTrace();

     				}
     			}
     		});                 
                      
 
    		


}
/*protected void ipList_keyReleased(KeyEvent e) {
	// TODO Auto-generated method stub
	
}
protected void ipList_keyPressed(KeyEvent e) {
	// TODO Auto-generated method stub
	
}
protected void ipList_keyTyped(KeyEvent e) {
	// TODO Auto-generated method stub
	
}*/
// Main method to get things started
public static void main( String args[] )
{
	// Create an instance of the test application
	fyp8 mainFrame	= new fyp8();
	mainFrame.setVisible( true );
}
}

as u can compile and run my code it will form this

http://i32.tinypic.com/1pyzra.png

what i want is that when i type in A or a in the combo box

it will filter the stock that start with a or A and the list will appear like if i type that it will only show APPLE and not the rest of the stock.

Edited 6 Years Ago by the reporter: n/a

This article has been dead for over six months. Start a new discussion instead.