Results 1 to 9 of 9
  1. #1
    maddy14 is offline Member
    Join Date
    May 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default how to stop the jframe from getting refreshed

    hiii..
    I got this strange problem.i m refreshing my jframe after every second.This jframe contains a button called addbutton.This addbutton opens another jframe.and that jframe contains a JComboBox .The problem is after every second the JComboBox is getting refreshed.I dont understand why.Before the user gets time to select any item in the combobox..That combobox again goes to the first element.
    Can somone pls explain me hw to prevent all this??
    Thanks for your time

    Java Code:
    public class MarketWatch {
    	static JButton addButton = new JButton("Add");
    	
    	
    	public static void main(String args[]) {
    		Timer timer = new Timer();
    		timer.schedule(new TimerTask() {
    
    			@Override
    			public void run() {
    				// TODO Auto-generated method stub
    				showTable();
    
    			}
    		}, 1000, 2000);
    	}
    
    	public static void showTable() {
    		SwingUtilities.invokeLater(new Runnable() {
    
    			@Override
    			public void run() {
    				// TODO Auto-generated method stub
    				
    				try {
    					// DriverManager.registerDriver(new
    					// oracle.jdbc.OracleDriver());
    					Class.forName("oracle.jdbc.driver.OracleDriver");
    					connMarketWatch = DriverManager.getConnection(
    							"jdbc:oracle:thin:@192.168.0.22:1521:XE", "sk_api",
    							"sk_api");
    
    addButton.addActionListener(new ActionListener() {
    							
    							@Override
    							public void actionPerformed(ActionEvent arg0) {
    								// TODO Auto-generated method stub
    								selectRow();
    												
    							
    
    							}
    						});
    				} catch (SQLException | ClassNotFoundException e1) {
    					// TODO Auto-generated catch block
    					System.out.println("Error in connecting");
    					e1.printStackTrace();
    				}
    	}
    	public static void selectRow()
    	{
    		try
    		{  
    			JComboBox jremoverows;
    			JFrame frame = new JFrame();
    			Dimension screenSize = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
    		        frame.setPreferredSize(new Dimension(200, 200));
    		        Dimension windowSize = new Dimension(frame.getPreferredSize());
    		        int wdwLeft = 300 + screenSize.width / 2 - windowSize.width / 2;
    		        int wdwTop = screenSize.height / 2 - windowSize.height / 2;
    			
    			Statement saddtoremoverow = connMarketWatch.createStatement();
    			ResultSet rsremoverow = saddtoremoverow.executeQuery("select securty_symbol from copy_view_mkt_watch where removerow='remove'");
    			int j=0;
    			
    			while(rsremoverow.next())
    			{
    				removedObjects[j]=rsremoverow.getString("SECURTY_SYMBOL");
    				j++;
    			}
    			jremoverows = new JComboBox(removedObjects);
    			AutoCompleteDecorator.decorate(jremoverows);
    			jremoverows.setEditable(true);
    			frame.setSize(200,200);
    			frame.requestFocus();
    			frame.setAlwaysOnTop(true);
    			frame.toFront();
    	
    			frame.setLocation(wdwLeft, wdwTop);
    			frame.add(jremoverows);
    			frame.setLayout(new FlowLayout());
    			frame.setVisible(true);
    			
    			
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    
    	}
    Last edited by Norm; 11-21-2013 at 02:59 PM. Reason: quote tag changed to code

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: how to stop the jframe from getting refreshed

    The code calls showTable() repeatedly in a Timer. Why not just call it once?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    maddy14 is offline Member
    Join Date
    May 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default Re: how to stop the jframe from getting refreshed

    Thanks for reply.i need to refresh the entire table every second .Thats why i m caling showTable() again n again

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: how to stop the jframe from getting refreshed

    Every time showTable() is called it adds another listener to the button. Every time the button is pressed ALL of its listeners will be called.
    Every time selectRow() is called a new frame is created.

    You need to make another design for how this code should work so it only creates one of the above instead of making so many of them.
    Last edited by Norm; 11-22-2013 at 02:18 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    maddy14 is offline Member
    Join Date
    May 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default Re: how to stop the jframe from getting refreshed

    Thanks for your suggestion... I will try to create a new design now..Because even if i place a textfield instead of combobox i find it difficult to type entire text in that textfield

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,917
    Rep Power
    5

    Default Re: how to stop the jframe from getting refreshed

    Quote Originally Posted by maddy14 View Post
    Thanks for your suggestion... I will try to create a new design now..Because even if i place a textfield instead of combobox i find it difficult to type entire text in that textfield
    Would a JTextArea suit your needs better?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    maddy14 is offline Member
    Join Date
    May 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default Re: how to stop the jframe from getting refreshed

    Actualy i solved that issue by using jdialog instead of jframe.Thanks a lot for your sugestions guys

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,265
    Rep Power
    6

    Default Re: how to stop the jframe from getting refreshed

    Quote Originally Posted by maddy14 View Post
    Actualy i solved that issue by using jdialog instead of jframe.Thanks a lot for your sugestions guys
    So the only change you made was to switch JFrame for JDialog and then the problems magically went away?

    If so: do you really see that as a solution? Because at this point you will likely be using JDialog for everything, since JFrame might just give you unexplainable problems. I'd not see this as a solution but rather a workaround, whatever works within a JDialog must also work within a JFrame.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    maddy14 is offline Member
    Join Date
    May 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default Re: how to stop the jframe from getting refreshed

    Thanks for replying... Whatever it was it worked now..The reason may be that jdialog doesnt allow the focus to shift to other frame and thats wht i wanted...Dont know exactly though

Similar Threads

  1. Replies: 7
    Last Post: 12-27-2011, 04:06 AM
  2. How do I stop the program but not exit the JFrame?
    By rajkobie in forum New To Java
    Replies: 1
    Last Post: 04-23-2011, 04:53 PM
  3. Use stop button to stop moving (stop timers) on JPanel
    By mneskovic in forum New To Java
    Replies: 3
    Last Post: 07-23-2010, 01:50 PM
  4. JLabel is not refreshed from withing ActionPerformed
    By mavaforumsm in forum AWT / Swing
    Replies: 2
    Last Post: 05-25-2010, 06:32 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
  •