Results 1 to 3 of 3
  1. #1
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Identify JTextField and JComboBox (editable)

    How can I separate JtextField and JComboBox? I use 'instanceof' in if statement but even if the focus is in a JComboBox the computer detects that it is a JTextField. I think it is because JComboBox's editable is set to true.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ComBoXample extends JFrame implements FocusListener
    {	JComboBox combo1;
    	JComboBox combo2;
    	JTextField tf;
    	JPanel p = new JPanel();
    	public ComBoXample()
    	{	String[] strCombo = {"ABC", "ACD", "BCD", "BDE", "CDE", "CEF", "EFG"};
    		combo1 = new JComboBox(strCombo);
    		combo2 = new JComboBox(strCombo);
    		tf = new JTextField();
    		
    		combo1.getEditor().getEditorComponent().addFocusListener(this);
    		combo2.getEditor().getEditorComponent().addFocusListener(this);
    		tf.addFocusListener(this);
    
    		combo1.setEditable(true);
    		combo2.setEditable(true);
    
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setLayout(new GridLayout(0,1,3,3));
    		this.add(p);
    
    		p.setLayout(new GridLayout(2,2,5,3));
    		p.add(combo1);
    		p.add(combo2);
    		p.add(tf);
    		pack();
    	}
    
    	public void focusGained(FocusEvent ef)
    	{	Object source = ef.getSource();
    		System.out.println("focusGained event");
    		if(source instanceof JComboBox)
    		{	System.out.println("Combo Gained");
    		}
    
    		if(source instanceof JTextField)
    		{	System.out.println("Text Gained");
    		}
    	}
    
    	public void focusLost(FocusEvent ef)
    	{	Object source = ef.getSource();
    		System.out.println("focuslost event");
    		if(source instanceof JComboBox)
    		{	System.out.println("Combo Lost");
    		}
    
    		if(source instanceof JTextField)
    		{	System.out.println("Text Lost");
    		}
    	}
    
    	public static void main(String[] args)
    	{	ComBoXample cbx = new ComBoXample();
    		cbx.setVisible(true);
    	}
    }

    Thanks for always helping me,
    geje

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    808
    Rep Power
    6

    Default

    As you have added the combobox to the editor ,it is getting the editor as source object whereas textfield has been added directly,it is able to identify the textfield
    Ramya:cool:

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Because in this situation you want to identify which source object is the text field instance you added (as opposed to the combo box editor text field), you can compare the source object with the text field instance using the == operator:
    Java Code:
    // check if they are the same object instance
    if(source == tf) {   
       ... // process text field
    }
    else {
       ... // process combo box
    }
    Alternatively, use different listeners for text field and combo box.

Similar Threads

  1. JComboBox and JTextField
    By fahad in forum NetBeans
    Replies: 3
    Last Post: 05-31-2010, 03:20 AM
  2. Replies: 2
    Last Post: 12-10-2009, 01:58 PM
  3. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  4. Replies: 2
    Last Post: 07-30-2008, 12:33 PM
  5. identify a thread
    By valery in forum Advanced Java
    Replies: 1
    Last Post: 07-25-2007, 01:24 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
  •