Results 1 to 6 of 6
  1. #1
    maddy14 is offline Member
    Join Date
    May 2013
    Posts
    14
    Rep Power
    0

    Default JTable not autorefreshing when user clicks on specific row

    Hiii...I got this weird problem. my Jtable is refreshing after every 5 seconds .Basicaly i have a function which gets called after every five seconds .Wht this function does is ..It takes values from database stores it in new DefaultTableModel and then through setModel function of JTable ,DefaultModel is added to JTable.Everything works fine but the problem is whenever the user clicks on a particular cell or row ,JTable doesnt get refreshed anymore.Any help would be highly appreciated.I tried fireTableDataChanged and wht not..Dont know how to approach


    Java Code:
    marketwatch.addActionListener(new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				// TODO Auto-generated method stub
    				plswork();
    				Timer timer = new Timer();
    				timer.schedule(new TimerTask() {
    
    					@Override
    					public void run() {
    						// TODO Auto-generated method stub
    						setTableModel();
    
    					}
    				}, 5000, 10000);
    			}[QUOTE][/QUOTE]
    				
    				
    		});
    	
                 void setTableModel()
    	{
    		final String obj[][] = new String[40][100];
    		final String columns[] = {"","SECURITY_ID","SECURTY_SYMBOL","SM_INSTRUMENT","DRV_TYPE","EXP_DATE" ," FUT_OPTION" ,"STRIKE_PRICE", "LOTSIZE","SCRIP_ID","LTP_PRICE","LTP_QTY","LTP_DATE" ,"BID_PRICE","BID_QTY","OFFER_PRICE"};
    		Statement sview;
    		
    		try {
    			
    			sview = c.createStatement();
    			ResultSet rsView = sview
    					.executeQuery("select SECURITY_ID, SECURTY_SYMBOL,SM_INSTRUMENT, DRV_TYPE, EXP_DATE ,FUT_OPTION ,STRIKE_PRICE, LOTSIZE, SCRIP_ID, LTP_PRICE, LTP_QTY, LTP_DATE,BID_PRICE, BID_QTY , from v_mkt_watch");
    			/*******************************1             2              3               4        5        6             7           8         9          10        11        12         13      14         15          16           17                18       19                     ************/
    			ResultSetMetaData rsmView = rsView.getMetaData();
    			int cols = rsmView.getColumnCount();
    			int i = 1;
    			count++;
    			while (rsView.next()) {
    				for (int j = 1; j <= cols; j++) {
    					
    					obj[i][j] = rsView.getString(j) + ""+count;
    					//table.getModel().setValueAt(rsView.getString(j), i, j);
    				    
    				}
    				i++;
    			}
    			defTableModel = new DefaultTableModel(obj,columns);
    			
    		}
    
    		catch (SQLException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    		 
    		if(null!=table)
    		{
    	     table.setModel(defTableModel);
    			
    		}
    	}
    	void plswork()
    	{
    		JFrame f = new JFrame();
    		f.setBackground(new Color(70, 200, 220));
    		f.revalidate();
    		
    		
    
    	     table = new JTable();
    		
    		final JTextField symbol = new JTextField();
    		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    		final TableColumnAdjuster tca = new TableColumnAdjuster(table);
    		tca.adjustColumns();
    		table.setAutoCreateRowSorter(true);
    		ListSelectionModel selectionModel = table.getSelectionModel();
    		selectionModel
    				.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		JMenuItem buyviewmkt = new JMenuItem();
    		JMenuItem sellmkt = new JMenuItem();
    		f.add(buyviewmkt);
    		f.add(sellmkt);
    		table.setFocusable(false);
    		buyviewmkt.setAccelerator(KeyStroke.getKeyStroke(
    				java.awt.event.KeyEvent.VK_F1, 0));
    		
    		selectionModel
    				.addListSelectionListener(new ListSelectionListener() {
    
    					@Override
    					public void valueChanged(ListSelectionEvent e) {
    						// TODO Auto-generated method stub
    						if (!e.getValueIsAdjusting()) {
    							ListSelectionModel model = table
    									.getSelectionModel();
    							int lead = model.getLeadSelectionIndex();
    							Object o = table.getValueAt(lead, 2);
    							
    							selectedScript = o + "";
    						 derivativeType = ""+table.getValueAt(lead, 4);
    						 System.out.println(derivativeType +derivativeType.equals("null"));
    						 
    						 
    							
    						}
    
    					}
    				});
    
    		buyviewmkt.addActionListener(new ActionListener() {
    
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				// TODO Auto-generated method stub
    				mktWatchBuySell(new Color(180, 230, 210),"f1");
    			
    			}
    		});
    		JScrollPane scrollPane = new JScrollPane(table);
    		f.setLayout(null);
    		f.getContentPane().setBackground(new Color(70, 200, 200));
    		f.setSize(1501, 600);
    		scrollPane.setBounds(10, 50, 1000, 400);
    		scrollPane.setBackground(new Color(70, 200, 200));
    		f.add(symbol);
    		f.add(scrollPane);
    		f.setVisible(true);
    	}
    Currently my database doesnt change automatically so i have inserted a count variable to check if the code is working properly .Have a look at line
    obj[i][j] = rsView.getString(j) + ""+count;
    Last edited by maddy14; 11-07-2013 at 06:17 AM.

  2. #2
    maddy14 is offline Member
    Join Date
    May 2013
    Posts
    14
    Rep Power
    0

    Default Re: JTable not autorefreshing when user clicks on specific row

    Is this forum alive?
    Last edited by maddy14; 11-07-2013 at 10:11 AM.

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,064
    Rep Power
    4

    Default Re: JTable not autorefreshing when user clicks on specific row

    Yes, but there are only a handful of people actually answering questions. You just need to be patient, people are on different timezones than you.

    I see that you are doing some database logic in a separate thread (well, the timer) and then changing the model of the table in that same thread; you may want to try to do the updating of the model on the Swing Event Dispatch Thread using a SwingWorker or the good old invokeLater().
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    maddy14 is offline Member
    Join Date
    May 2013
    Posts
    14
    Rep Power
    0

    Default Re: JTable not autorefreshing when user clicks on specific row

    Thanks just a guy for your reply.
    I may well post the error i m getting and also the image of the jtable which might be useful.As far as the SwingWorker is concerned i m very bad at it.

    JTable not autorefreshing when user clicks on specific row-table.jpg
    In this image instead of just the user selected row ending with '4'..Every row should have been ended with '4'

    The error that i get is

    Exception in thread "Timer-0" java.lang.IndexOutOfBoundsException: Invalid index
    at javax.swing.DefaultRowSorter.convertRowIndexToMode l(Unknown Source)
    at javax.swing.JTable.convertRowIndexToModel(Unknown Source)
    at javax.swing.JTable.getValueAt(Unknown Source)
    at com.iwealth.MDIForm$18.valueChanged(MDIForm.java:1 284)
    at javax.swing.DefaultListSelectionModel.fireValueCha nged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueCha nged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.setValueIsAd justing(Unknown Source)
    at javax.swing.JTable.clearSelectionAndLeadAnchor(Unk nown Source)
    at javax.swing.JTable.tableChanged(Unknown Source)
    at javax.swing.JTable.setModel(Unknown Source)
    at com.iwealth.MDIForm.setTableModel(MDIForm.java:125 9)
    at com.iwealth.MDIForm$14$1.run(MDIForm.java:1036)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)
    Last edited by maddy14; 11-07-2013 at 10:42 AM.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,445
    Rep Power
    18

    Default Re: JTable not autorefreshing when user clicks on specific row

    Put some debugging in the valueChanged listener.
    Find out what row it's working on...in essence print out everything.
    I'm not sure the ListSelectionModel is returning what you think it is at that point.
    Please do not ask for code as refusal often offends.

  6. #6
    maddy14 is offline Member
    Join Date
    May 2013
    Posts
    14
    Rep Power
    0

    Default Re: JTable not autorefreshing when user clicks on specific row

    Quote Originally Posted by Tolls View Post
    Put some debugging in the valueChanged listener.
    Find out what row it's working on...in essence print out everything.
    I'm not sure the ListSelectionModel is returning what you think it is at that point.
    Thanks Tolls..Debugging did help to solve other issue.

    Hey thanks for your reply.. I added this code

    Java Code:
     int   lead =0;
    							 lead = model.getLeadSelectionIndex();
    							 if(table.getSelectedRow()==-1)
    							 {
    								 lead =0;
    							 }
    Thanks to this thread http://www.javaprogrammingforums.com...tml#post129053
    and the jtable is now refreshing as per requirement.
    Last edited by maddy14; 11-08-2013 at 07:59 AM.

Similar Threads

  1. Using Java to detect user clicks on an application
    By msteele in forum New To Java
    Replies: 0
    Last Post: 10-18-2013, 02:50 AM
  2. Replies: 6
    Last Post: 12-16-2012, 03:26 AM
  3. Differentiating user clicks and doClick clicks?
    By jiffi in forum New To Java
    Replies: 19
    Last Post: 01-13-2012, 08:30 AM
  4. How do I check if user clicks on an image?
    By rajkobie in forum New To Java
    Replies: 10
    Last Post: 05-04-2011, 04:45 AM
  5. Replies: 0
    Last Post: 03-31-2008, 04:35 PM

Tags for this Thread

Posting Permissions

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