Results 1 to 10 of 10
  1. #1
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default focus on a specific component

    Hi,

    I don't understand how the focus work. I have a swing program with the jContentpane with two Components:
    1. in the north: a toolbarmenu
    2. int the center a Panel (own class that implements JPanel with two other panels on it).

    the center panel has to have the focus, because it has a keyboard listener, but it is always the toolbarmenu who has the focus. I tried a lot with the methods:
    • setfocusable()
    • grabFocus()
    • requetFocus()
    • requestFocusInWindow()

    but nothing seem to help. I only can change the focus when I first click on the tab key, but that is not the solution. Does someone have a solution?

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default Re: focus on a specific component

    What is a "toolbarmenu"?

    Post your Short, Self Contained, Correct Example, that demonstrates the problem.

  3. #3
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default Re: focus on a specific component

    Hi,

    i meant a "JToolBar".

    Here is a short,self contained example of what I mean. There are two classes: MainView and ViewBoard. The MainView contains the main, so you can copy paste this two classes and run it to see what I mean.
    Class MainView:
    Java Code:
    package view;
    
    import java.awt.BorderLayout;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JToolBar;
    import javax.swing.JButton;
    import javax.swing.SwingUtilities;
    
    public class MainView extends JFrame {
    
    	private static final long serialVersionUID = 1L;
    	private JPanel jContentPane = null;
    	private JToolBar jJToolBarBar = null;
    	private JButton jButtonExit = null;
    	private JButton jButtonTest = null;
    	private JPanel jPanelViewBoard = null;
    
    	private JPanel getJPanelViewBoard() {
    		if (jPanelViewBoard == null) {
    			jPanelViewBoard = new ViewBoard();
    			
    		}
    		return jPanelViewBoard;
    	}
    
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				MainView thisClass = new MainView();
    				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				thisClass.setLocationRelativeTo(null);
    				thisClass.setVisible(true);
    			}
    		});
    	}
    	
    	public MainView() {
    		super();
    		initialize();
    	}
    
    	private void initialize() {
    		this.setSize(300, 200);
    		this.setContentPane(getJContentPane());
    		this.setTitle("JFrame");
    	}
    
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jContentPane = new JPanel();
    			jContentPane.setLayout(new BorderLayout());
    			jContentPane.add(getJJToolBarBar(), BorderLayout.NORTH);
    			getJJToolBarBar().setFocusable(false);
    			jContentPane.add(getJPanelViewBoard(), BorderLayout.CENTER);
    			getJPanelViewBoard().setFocusable(true);
    			getJPanelViewBoard().requestFocusInWindow();
    		}
    		return jContentPane;
    	}
    
    	private JToolBar getJJToolBarBar() {
    		if (jJToolBarBar == null) {
    			jJToolBarBar = new JToolBar();
    			jJToolBarBar.add(getJButtonExit());
    			jJToolBarBar.add(getJButtonTest());
    		}
    		return jJToolBarBar;
    	}
    
    	private JButton getJButtonExit() {
    		if (jButtonExit == null) {
    			jButtonExit = new JButton();
    			jButtonExit.setText("Exit");
    		}
    		return jButtonExit;
    	}
    
    	private JButton getJButtonTest() {
    		if (jButtonTest == null) {
    			jButtonTest = new JButton();
    			jButtonTest.setText("Test");
    		}
    		return jButtonTest;
    	}
    
    }
    Class ViewBoard
    Java Code:
    package view;
    
    import java.awt.GridBagLayout;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import java.awt.GridBagConstraints;
    
    public class ViewBoard extends JPanel {
    
    	private static final long serialVersionUID = 1L;
    	private JButton jButtonFocus = null;
    
    	public ViewBoard() {
    		super();
    		initialize();
    	}
    
    	private void initialize() {
    		GridBagConstraints gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.gridx = 0;
    		gridBagConstraints.gridy = 0;
    		this.setSize(300, 200);
    		this.setLayout(new GridBagLayout());
    		this.add(getJButtonFocus(), gridBagConstraints);
    	}
    
    	private JButton getJButtonFocus() {
    		if (jButtonFocus == null) {
    			jButtonFocus = new JButton();
    			jButtonFocus.setText("This button wants always the focus");
    		}
    		return jButtonFocus;
    	}
    
    }

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default Re: focus on a specific component

    Java Code:
    jContentPane.add(getJPanelViewBoard(), BorderLayout.CENTER);
    getJPanelViewBoard().setFocusable(true);
    getJPanelViewBoard().requestFocusInWindow();
    There are 2 reasons why that code won't work:

    1. You can only request focus on a component once the GUI is visible.

    2. You have 3 instances of the ViewBoard. Only the first instance was added to the GUI. The last 2 are just sitting in memory wasting space.

    The common solution is to make the toolbar button non-focusable:

    Java Code:
    button.setFocusable( false );

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: focus on a specific component

    center panel has to have the focus, because it has a keyboard listener
    Where are there any listeners in your posted code?

  6. #6
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default Re: focus on a specific component

    camickr asked a short code, so I didn't put my whole code. That's why the keyboard listener isn't in the code I gave you.

  7. #7
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default Re: focus on a specific component

    Quote Originally Posted by camickr View Post
    [code]


    2. You have 3 instances of the ViewBoard. Only the first instance was added to the GUI. The last 2 are just sitting in memory wasting space.
    I only have one instance of ViewBoard, I don't see the 2 others! I only do once new ViewBoard(). I don't know what you meant here.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: focus on a specific component

    I thought your problem was with a key listener.
    What problem does your posted code show? How would anyone execute your code and see the problem?

  9. #9
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default Re: focus on a specific component

    The problem was: the JToolBar always grab the focus, but (in my short example) I want that the button ""This button wants always the focus"" that have the focus. I think my example couldn't be more simple and clear.

    The solution of camicr seems to work in my short example. I will try it now in my application. Thanks a lot!

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: focus on a specific component

    Try adding this to your MainView constructor:
    Java Code:
     
            setVisible(true); //<<<<<<<<<<
            ((ViewBoard)getJPanelViewBoard()).requestFocusOnButton();   //<<<<<<<<<<<
    and this to the ViewBoard:
    Java Code:
        public void requestFocusOnButton() {
            jButtonFocus.requestFocusInWindow(); //<<<<<<<<<<<
            System.out.println("requestFocus");
        }

Similar Threads

  1. Loosing TableCellEditor Component Focus
    By sandeepsai39 in forum AWT / Swing
    Replies: 0
    Last Post: 03-24-2009, 06:39 AM
  2. Loosing TableCellEditor Component Focus
    By sandeepsai39 in forum New To Java
    Replies: 0
    Last Post: 03-23-2009, 05:42 AM
  3. Replies: 2
    Last Post: 11-08-2008, 10:40 AM
  4. Replies: 1
    Last Post: 04-24-2008, 06:35 PM
  5. How to Focus Next Component Sample
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 08:18 PM

Posting Permissions

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