Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Swing: allow / prohibit focus change

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Swing: allow / prohibit focus change

    Hello!

    I have to find place where I can allow / prohibit focus transfer.
    I have to know both components - component which loses focus and component which required focus.
    I need a place (or all places) to be tracked on tabbing, mnemonic key press, mouse press.

    It seems like VetoableChangeListener method vetoableChange(...) is the right place for me.

    But I encountered strange behaviour. See code below.

    When I change VETOFOCUSCHANGE = true, vetoableChange(...) is not executed anymore (no console output).
    However focus change is prohibited.

    Why method vetoableChange(...) is not executed? Or it is? Why there is no console output?

    import java.awt.*;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyVetoException;
    import java.beans.VetoableChangeListener;
     
    import javax.swing.*;
     
     
     
    public class VetoableFocus extends JDialog implements FocusListener {
     
    	private static int counter = 0;
     
    	//***********************************************
    	//Change this to test FocusVetoableChangeListener
    	private static boolean VETOFOCUSCHANGE = false;
    	//***********************************************
     
     
     
     
    	class FocusVetoableChangeListener implements VetoableChangeListener {
    		  public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
     
    			System.out.println((++counter) + " " + "VetoableChange " + evt.getPropertyName());
     
    			Component oldComp = (Component) evt.getOldValue();
    		    Component newComp = (Component) evt.getNewValue();
     
    		    if ("focusOwner".equals(evt.getPropertyName())) {
    		    	System.out.println((++counter) + " " + "Vetoable 'focusOwner' change " + oldComp + " >> " + newComp);
     
    		    } 
     
     
    		    if (VETOFOCUSCHANGE) {
    		      throw new PropertyVetoException("message", evt);
    		    }
     
     
    		  }
    		}
     
    	class MyTextField extends JTextField {
    		private String name;
    		public MyTextField(String name) {
    			this.name = name;
    			setText(name);
    		}
    		@Override
    		public boolean requestFocusInWindow() {
    			System.out.println((++counter) + " " + this + " requestFocusInWindow");
    			return super.requestFocusInWindow();
    			//return false;
    		}
     
    		@Override
    		public void requestFocus() {
    			System.out.println((++counter) + " " + this + " requestFocus");
    			super.requestFocus();
    		}
     
    		@Override
    		public void transferFocus() {
     
    			Container focusCycleRoot = getFocusCycleRootAncestor ();
    		    FocusTraversalPolicy policy = focusCycleRoot.getFocusTraversalPolicy ();
     
    		    Component next = policy.getComponentAfter (focusCycleRoot, this);
    		    System.out.println((++counter) + " " + this + " transferFocus to " + next);
     
    			super.transferFocus();
    		}
     
    		@Override
    		public String toString() {
    			return name;
    		}
    	};
     
    	public VetoableFocus() {
     
    		KeyboardFocusManager.getCurrentKeyboardFocusManager().addVetoableChangeListener(new FocusVetoableChangeListener());
     
    		setLayout(new BoxLayout(VetoableFocus.this.getContentPane(), BoxLayout.Y_AXIS));
     
    		JTextField text1 = new MyTextField("text1");
    		text1.addFocusListener(this);
     
    		JLabel label1 = new JLabel("label1"){
    			public String toString() {
    				return "label1";
    			}
    		};
    		label1.setDisplayedMnemonic('1');
    		label1.setLabelFor(text1);
     
    		JTextField text2 = new MyTextField("text2");
    		text2.addFocusListener(this);
     
    		JLabel label2 = new JLabel("label2"){
    			public String toString() {
    				return "label2";
    			}
    		};
    		label2.setDisplayedMnemonic('2');
    		label2.setLabelFor(text2);
     
    		add(label1);
    		add(text1);
    		add(label2);
    		add(text2);
     
     
    		setSize(new Dimension(300, 300));
    		setVisible(true);
    	}
     
    	public String toString() {
    		return "Dialog";
    	}
     
    	@Override
    	public void focusGained(FocusEvent evt) {
    		System.out.println((++counter) + " " + evt.getSource() + " focus gained");
     
     
    	}
     
    	@Override
    	public void focusLost(FocusEvent evt) {
    		System.out.println((++counter) + " " + evt.getSource() + " focus lost");	
    	}
     
    	public static void main(String[] args) {
    		new VetoableFocus();
    	}
    }



    Thank you in advance!
    Last edited by aigarzs; June 22nd, 2014 at 04:35 AM.


Similar Threads

  1. my first post
    By aigarzs in forum The Cafe
    Replies: 0
    Last Post: June 21st, 2014, 06:18 PM
  2. Why does my post always get deleted at every forum I post at?
    By WebDevGuy in forum Forum Updates & Feedback
    Replies: 3
    Last Post: April 30th, 2014, 11:30 AM
  3. my first post...
    By cr0ck3t in forum The Cafe
    Replies: 0
    Last Post: April 29th, 2014, 04:17 AM
  4. First post!
    By coreymcdonald855 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 27th, 2014, 03:59 AM
  5. My First Post
    By Student X in forum Member Introductions
    Replies: 1
    Last Post: July 26th, 2011, 03:14 AM