Results 1 to 4 of 4
  1. #1
    the reporter is offline Member
    Join Date
    Jun 2010
    Rep Power

    Default Jcombobox filter list

    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 :

    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

    Java Code:
    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.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;
    	        String[] columnNames = {"Stock Name",
    	        		"Last Price",
    	        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
    	// Create a tabbed pane
    	tabbedPane = new JTabbedPane();
    	tabbedPane.addTab( "Stocks", panel1);
    	tabbedPane.addTab( "Search", panel4  );
    	topPanel.add( tabbedPane, BorderLayout.CENTER);
        String line = null;
      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]);
    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();
    	/*ipList.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
    		public void keyTyped(KeyEvent e) {
    		public void keyPressed(KeyEvent e) {
    		public void keyReleased(KeyEvent 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())) { 
            	 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(""+input1+"&f=sl1d1t1c1ohgv&e=.csv");
                             URLConnection connection = url.openConnection();
             	            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]);
         				} catch (IOException ex) {
    /*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 );

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

  3. #3
    anuskesh is offline Member
    Join Date
    Jul 2010
    Rep Power


    Does anyone have any experience woking with POI and MS WORD. I need to read bookmarks from a document then right to it. I went throw the docs, and I
    dont see any reference to bookmarks, only tables, ranges and paragraphs

    1. I wanted to work with the word document template in memory, I'm retrieving this doc with bookmark from a db and then I store the bytes in memory

    2. I wanted to retrieve the corresponding bookmark values from a db and place those dynamic values into document which is in memory. Finally I have to show the document to user which is updated everything.

    Highly appreciated if provide any example code.

  4. #4
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Rep Power

Similar Threads

  1. JComboBox search for match list
    By mine0926 in forum New To Java
    Replies: 3
    Last Post: 07-08-2010, 09:48 AM
  2. Replies: 4
    Last Post: 04-07-2010, 03:01 PM
  3. How at save list in Jcombobox...
    By pra.deep in forum AWT / Swing
    Replies: 1
    Last Post: 10-29-2009, 06:57 AM
  4. Filter in Fileconnector's list() method
    By arnab321 in forum CLDC and MIDP
    Replies: 0
    Last Post: 03-12-2009, 05:53 PM
  5. JComboBox and drop-down list.....HELP!!!!!
    By Anna in forum AWT / Swing
    Replies: 1
    Last Post: 06-18-2008, 06:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts