Results 1 to 3 of 3
- 07-09-2010, 09:12 AM #1
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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
- 07-09-2010, 10:50 AM #2
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:
- 07-09-2010, 04:36 PM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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:
Alternatively, use different listeners for text field and combo box.Java Code:// check if they are the same object instance if(source == tf) { ... // process text field } else { ... // process combo box }
Similar Threads
-
JComboBox and JTextField
By fahad in forum NetBeansReplies: 3Last Post: 05-31-2010, 03:20 AM -
Activate JComboBox 1 when object is selected in JComboBox 2...
By bahumbaba in forum AWT / SwingReplies: 2Last Post: 12-10-2009, 01:58 PM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM -
Making a Table Row with JPanel Composed of JTextField and JComboBox Selectable
By datechie in forum AWT / SwingReplies: 2Last Post: 07-30-2008, 12:33 PM -
identify a thread
By valery in forum Advanced JavaReplies: 1Last Post: 07-25-2007, 01:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks