Results 1 to 9 of 9
  1. #1
    230005 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default FocusGained event doesn't work

    Hi everybody!

    I have this issue with my code. When my app JFrame gains focus, FocusGained method should log some info into the console, but id does not. When I comment out the piece of code responsible for adding buttons (from mainPanel.setLayout(mainPanelLayout); to add(mainPanel); ), it works. Any ideas what am I doing wrong (some parts of the code are NetBeans - generated)? Here's the code:

    Java Code:
    public void focusGained(FocusEvent e) {
    		try {
    			Set<String> contents = grabber.readClipboard();
    			
    			for(String one : contents) {
    				System.out.println(one);
    			}
    				
    		}
    		catch(UnsupportedFlavorException ex) {
    			ex.printStackTrace();
    		}
    		catch(IOException ex) {
    			ex.printStackTrace();
    		}
    	}
    
    private void buildQuickIconsPanel() {
    	    mainPanel = new JPanel();
    	    filesPanel = new JPanel();
    	    grabberPanel = new JPanel();
    	    settingsPanel = new JPanel();
    	    startButton = new JButton(new ImageIcon("http://www.java-forums.org/images/start.png"));
    	    stopButton = new JButton(new ImageIcon("http://www.java-forums.org/images/stop.png"));
    	    stopAfterButton = new JButton(new ImageIcon("http://www.java-forums.org/images/after.png"));
    	    addButton = new JButton(new ImageIcon("http://www.java-forums.org/images/add.png"));
    	    deleteButton = new JButton(new ImageIcon("http://www.java-forums.org/images/del.png"));
    	    jTabbedPane1 = new JTabbedPane();
    	    
    	    mainPanel.setName("mainPanel"); 
    
    	    jTabbedPane1.setName("jTabbedPane1"); 
    
    	    filesPanel.setName("filesPanel"); 
    
    	    GroupLayout filesPanelLayout = new GroupLayout(filesPanel);
    	    filesPanel.setLayout(filesPanelLayout);
    	    filesPanelLayout.setHorizontalGroup(
    	    	filesPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
    	        .addGap(0, 877, Short.MAX_VALUE)
    	    );
    	    filesPanelLayout.setVerticalGroup(
    	        filesPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
    	        .addGap(0, 584, Short.MAX_VALUE)
    	    );
    
    	    
    	    jTabbedPane1.addTab("Ściągane pliki", filesPanel); 
    
    	    grabberPanel.setName("grabberPanel"); 
    
    	    GroupLayout grabberPanelLayout = new GroupLayout(grabberPanel);
    	    grabberPanel.setLayout(grabberPanelLayout);
    	        grabberPanelLayout.setHorizontalGroup(
    	            grabberPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
    	            .addGap(0, 877, Short.MAX_VALUE)
    	        );
    	    grabberPanelLayout.setVerticalGroup(
    	        grabberPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
    	            .addGap(0, 584, Short.MAX_VALUE)
    	    );
    
    	    jTabbedPane1.addTab("Łapacz linków", grabberPanel); 
    
    	    settingsPanel.setName("settingsPanel"); 
    
    	    GroupLayout settingsPanelLayout = new GroupLayout(settingsPanel);
    	    settingsPanel.setLayout(settingsPanelLayout);
    	    settingsPanelLayout.setHorizontalGroup(
    	        settingsPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
    	        .addGap(0, 877, Short.MAX_VALUE)
    	    );
    	    settingsPanelLayout.setVerticalGroup(
    	        settingsPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
    	        .addGap(0, 584, Short.MAX_VALUE)
    	    );
    
    	    jTabbedPane1.addTab("Ustawienia", settingsPanel); 
    
    	    GroupLayout mainPanelLayout = new GroupLayout(mainPanel);
    	    
    	    mainPanel.setLayout(mainPanelLayout);
    	    mainPanelLayout.setHorizontalGroup(
    	        mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
    	        .addComponent(jTabbedPane1, GroupLayout.DEFAULT_SIZE, 882, Short.MAX_VALUE)
    	        .addGroup(mainPanelLayout.createSequentialGroup()
    	            .addContainerGap(0, 10)
    	            .addComponent(startButton)
    	            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
    	            .addComponent(stopButton)
    	            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
    	            .addComponent(stopAfterButton)
    	            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
    	            .addComponent(addButton)
    	            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
    	            .addComponent(deleteButton)
    	            .addGap(687, 687, 687))
    	    );
    	    mainPanelLayout.setVerticalGroup(
    	        mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
    	        .addGroup(mainPanelLayout.createSequentialGroup()
    	            .addContainerGap()
    	            .addGroup(mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
    	                .addComponent(stopAfterButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    	                .addComponent(stopButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    	                .addComponent(startButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    	                .addComponent(addButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    	                .addComponent(deleteButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    	            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
    	            .addComponent(jTabbedPane1, GroupLayout.DEFAULT_SIZE, 612, Short.MAX_VALUE))
    	    );
    	    
    	    add(mainPanel);
    	}
    Last edited by 230005; 05-01-2010 at 12:02 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    230005 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    Yes, I add it to JFrame, because I need to know when my application gets focus. Besides, that's not the point - it's as I said, when lines mentioned are commented everything works fine. Would you like to see all of the code, to compile it, and see for yourself :)?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by 230005 View Post
    Yes, I add it to JFrame, because I need to know when my application gets focus. Besides, that's not the point - it's as I said, when lines mentioned are commented everything works fine. Would you like to see all of the code, to compile it, and see for yourself :)?
    No, it's full of netbeans-generated cr@p which is impossible to read and uncompilable on my system.

    I think that you may be misunderstanding what a FocusListener is for, and when there is any doubt, always go to the API. It states:
    The listener interface for receiving keyboard focus events on a component.

    The key word here is "component". It detects focus gained on components that are focusable such as JButtons, JTextFields, etc, but not on the window itself. For that you may want to try a WindowFocusListener. The API should help you to use this.

    Much luck!

  5. #5
    230005 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    Seems you were correct about implementing WindowFocusListener. Many thanks :D! Still it's odd why it worked without these lines:

    Java Code:
     GroupLayout mainPanelLayout = new GroupLayout(mainPanel);
    	    
    	    mainPanel.setLayout(mainPanelLayout);
    	    mainPanelLayout.setHorizontalGroup(
    	        mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
    	        .addComponent(jTabbedPane1, GroupLayout.DEFAULT_SIZE, 882, Short.MAX_VALUE)
    	        .addGroup(mainPanelLayout.createSequentialGroup()
    	            .addContainerGap(0, 10)
    	            .addComponent(startButton)
    	            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
    	            .addComponent(stopButton)
    	            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
    	            .addComponent(stopAfterButton)
    	            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
    	            .addComponent(addButton)
    	            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
    	            .addComponent(deleteButton)
    	            .addGap(687, 687, 687))
    	    );
    	    mainPanelLayout.setVerticalGroup(
    	        mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
    	        .addGroup(mainPanelLayout.createSequentialGroup()
    	            .addContainerGap()
    	            .addGroup(mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
    	                .addComponent(stopAfterButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    	                .addComponent(stopButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    	                .addComponent(startButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    	                .addComponent(addButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    	                .addComponent(deleteButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    	            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
    	            .addComponent(jTabbedPane1, GroupLayout.DEFAULT_SIZE, 612, Short.MAX_VALUE))
    	    );
    I mean - for me it's just makes no sense why code responsible for adding components to JFrame would have any impact on JFrame ability to gain focus using FocusListener instead of WindowFocusListener...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Perhaps it's because when you add components to the JFrame, you are adding objects that will gain the focus in place of the JFrame itself. If so, then it makes sense that a focuslistener might work for an empty JFrame but not for a focusable component-filled JFrame.

  7. #7
    230005 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    But apart from this component, there were also a menu on JFrame. How comes it didn't capture the focus?

    Forgive me, if I'm testing your patience ;P. I'm just mighty curious about this JMagic :).

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by 230005 View Post
    But apart from this component, there were also a menu on JFrame. How comes it didn't capture the focus?
    I don't know. Sorry.


    Forgive me, if I'm testing your patience ;P. I'm just mighty curious about this JMagic :).
    There's nothing wrong with asking questions and being curious. Much luck!

  9. #9
    230005 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Netbeans - focusGained - focusLost
    By dbashby in forum New To Java
    Replies: 1
    Last Post: 04-15-2009, 11:17 PM
  2. checking for an event during an event
    By infinity in forum AWT / Swing
    Replies: 22
    Last Post: 04-09-2009, 02:08 AM
  3. Key event
    By ivvgangadhar in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-11-2008, 10:27 AM
  4. Event
    By nt5515 in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 05:44 PM
  5. Need help with JButton event
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 02:15 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
  •