CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2009
    Posts
    8

    Gui becomes unresponsive!

    note: I have two classes that extend the JFrame class.

    my Main calls DisplayQueryResults, which offers a GUI interface for the user to interact with my Database.
    Display Query results give the user the option to insert, delete and update rows. So far I have only implemented Insertion.

    When the user chooses to insert a row, a RowEntryFormat object is created and its update function is called.

    This in turn creates an InputWindow object (which calls its own createGUI method from its constructor)

    Using debug mode and stepping thru everything i found that when we step into the RowEntryFormat.insertion() method, the InputWindow GUI becomes completely unresponsive, and RowEntryFormat keeps sleeping, waiting on the user to enter data in the InputWindow and click ok or cancel, but this won't happen because the GUI doesnt respond. The only thread that seems to be actively running is RowEntryFormat.insertion()

    Heres my output to the console:
    *******
    AWT-EventQueue-0 is about to sleep!!
    AWT-EventQueue-0 is about to sleep!!
    AWT-EventQueue-0 is about to sleep!!
    AWT-EventQueue-0 is about to sleep!!
    AWT-EventQueue-0 is about to sleep!!
    AWT-EventQueue-0 is about to sleep!!
    AWT-EventQueue-0 is about to sleep!!
    AWT-EventQueue-0 is about to sleep!!

    AND SO ON.............
    *******
    These printed lines are all from RowEntryFormat.insertion()

    why does the GUI become unresponsive? why is RowEntryFormat taking up all the CPU time?

    Heres my code... Ive left out a LOT of the details, but included all the important stuff (i hope)

    Code:
    public class DisplayQueryResults extends JFrame {
    	//default constructor initializes some variables and sets up the GUI
    	DisplayQueryResults() { 
    		.......... blah blah 
    		RowEntryFormat rowEntry = new RowEntryFormat();
    		..... blah blah
    		//if the user selects the Insert Row options the ActionListener is activated and:
    		{
    			insertRow();
    		}
    	}
    	private void insertRow() {
    		rowEntry.insertion();
    		...... blah blah
    	}
    }
    Code:
    public class RowEntryFormat {
    	//default constructor initializes variables
    	RowEntryFormat () {
    		InputWindow myInputWindow = new InputWindow();
    		...... blah blah
    	}
    	protected void insertion () {
    		while (!myInputWindow.quit()){
    			try {
    				System.out.println(Thread.currentThread().getName() + " is about to sleep!!");
    				Thread.sleep(2000);				
    				// myInputWindow becomes unresponsive!! this thread seems to take all priority!
    				// it just keeps looping and looping, waiting for the user to enter the info and quit
    			} 
    			catch (InterruptedException e) {
    				DialogBox.showDialog("Row Entry Format thread could not be put to sleep");
    				e.printStackTrace();
    			}
    			
    			//when it gets down here it will collect 
    			//gathered information from myInputWindow 
    			//and insert a new row in the database
    		}
    	}
    }
    Code:
    public class InputWindow extends JFrame implements WindowListener{
    	public InputWindow() {
    		super("Data Input Window");
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
    	}
    	
    	private void createAndShowGUI() {
    		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            
            //set up grid bad layout
    		//Create the fields and set them up
            firstName = new JTextField(20);
            JLabel firstNameLabel = new JLabel("First Name:");
            firstNameLabel.setDisplayedMnemonic('N');
            firstNameLabel.setLabelFor(firstNameLabel);
            lastName = new JTextField(20);
            JLabel lastNameLabel = new JLabel("Last Name:");
            lastNameLabel.setDisplayedMnemonic('N');
            lastNameLabel.setLabelFor(lastNameLabel);
                  
            display = new JTextArea(5,20);
            display.setEditable(false);
            JScrollPane scrollPane = new JScrollPane(display);
            scrollPane.setPreferredSize(new Dimension(400, 200));
    
            JButton okButton = new JButton("OK");
            okButton.setPreferredSize(new Dimension(100,
                    (int) okButton.getPreferredSize().getHeight()));
            okButton.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				//SUBMIT DATA
    				FirstName = firstName.getText();
    				LastName = lastName.getText();
    				//System.out.println("Name entered was:\t" + FirstName + "\t" + LastName );
    				quit = true;
    				frame.dispose();
    				//this.notifyAll();
    			}
            });
            JButton cancelButton = new JButton("Cancel");
            cancelButton.addActionListener(new ActionListener(){
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				quit = true;
    				frame.dispose();
    			}
            });
            cancelButton.setPreferredSize(new Dimension(100,
                    (int) cancelButton.getPreferredSize().getHeight()));
    
            JPanel panel = new JPanel(new GridBagLayout());
            panel.setBorder(BorderFactory.createEmptyBorder(4, 10, 10, 10));
    
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.WEST;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(6, 6, 0, 0);
            gbc.gridx = GridBagConstraints.RELATIVE;
            gbc.gridy = 0;
    
            panel.add(firstNameLabel, gbc);
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            panel.add(firstName, gbc);
            
            gbc.gridy++;
            gbc.gridwidth = 1;
            
            panel.add(lastNameLabel, gbc);
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            panel.add(lastName, gbc);    
            
            gbc.gridy++;
            gbc.gridwidth = 2;    
            
            panel.add(scrollPane, gbc);
            gbc.gridwidth = GridBagConstraints.REMAINDER;
    
            JPanel buttonPanel = new JPanel(new GridBagLayout());
            gbc.gridwidth = 1;
            gbc.gridy = 0;
            buttonPanel.add(okButton, gbc);
            buttonPanel.add(cancelButton, gbc);
    
            panel.add(buttonPanel,
                    new GridBagConstraints(1, 3, 4, 1, 0, 0,
                            GridBagConstraints.EAST, GridBagConstraints.NONE,
                            new Insets(0, 0, 0, 0), 0, 0));
    
            panel.add(Box.createGlue(),
                    new GridBagConstraints(0, 4, 4, 1, 0, 1,
                            GridBagConstraints.EAST, GridBagConstraints.NONE,
                            new Insets(0, 0, 0, 0), 0, 0));
            
            //Set up the content pane.
        	//myPanel = buildGridBagLayout();
        	panel.setOpaque(true); //content panes must be opaque
        	panel.repaint();
            frame.setContentPane(panel);
            addWindowListener(this);    
            
            //Display the window.
            //frame.setSize( 300, 250 );
            frame.pack();
            frame.setVisible(true);
            System.out.println("Win_TF_Layout GUI method is finishing up");
    	}
    	
    	//All the window listeners are implemented down here
    }

  2. #2
    Join Date
    Apr 2007
    Posts
    442

    Re: Gui becomes unresponsive!

    Have to say, I dont have the foggiest idea what you are trying to accomplish. Now, manipulation of the "quit" variable demands AWT EDT to visit there, which it never will do, if you keep it sleeping forever in that loop of yours. Can you explain why in the name of gods are you doing that?

    Keep in mind, that there is ONLY one AWT Event dispatch thread, the functioning of anything Gui related is based on it. So, doing anything that tampers the thread in its usual business, such as this does, is always a bad idea.

    When making a Gui, first thing to remember, is not to try to be clever. The simpler you can keep it the better. If it is your idea, that you will have the InputWindow´s variables updated, wait for the widow to dispose, and then update the rowEntry. Why not simply give the row entry to the InputWidow? Once user has entered the values, you update the row, simple as that.

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
  •  





Click Here to Expand Forum to Full Width

Featured