Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By gimbal2
  • 1 Post By taps

Thread: Call Java Object event/method by JTextField data

  1. #1
    taps is offline Member
    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Default Call Java Object event/method by JTextField data

    Hi All,

    I am new in JAVA. Don't know if it is possible in JAVA or not. Please guide me.


    In my program there are few JTextField boxes and a JButton. Whenever I am selecting a JTextField box the box name is appearing in another JTextField (here textField_25). Now, I am trying to use the JTextField name (which is appearing in JTextField_25) to do another task. Such as trying to hide that JTextField by clicking the JButton.


    1...........

    //Below are the code to capture the JTextField name. Which is doing good.

    Java Code:
    //---------------------------------------------------------------------------------
    		textField = new JTextField();
    		textField.setBounds(12, 12, 24, 39);
    		textField.addFocusListener(new FocusAdapter() {
    			@Override
    			
    			public void focusGained(FocusEvent arg0) {
    				
    				textField.setName("textField");
    				textField_25.setText(textField.getName());
    
    			}
    		});
    
    		textField.setBackground(Color.PINK);
    		textField.setColumns(10);
    
    //---------------------------------------------------------------------------------
    
    		textField_1 = new JTextField();
    		textField_1.setBounds(12, 12, 24, 39);
    		textField_1.addFocusListener(new FocusAdapter() {
    			@Override
    			
    			public void focusGained(FocusEvent arg0) {
    				
    				textField_1.setName("textField_1");
    				textField_25.setText(textField_1.getName());
    
    			}
    		});
    		
    		textField_1.setBackground(Color.PINK);
    		textField_1.setColumns(10);
    //---------------------------------------------------------------------------------
    2............


    //Hiding the JTextField


    textField.setVisible(false);

    //OR
    textField_1.setVisible(false);


    THE MAIN PROBLEM IS......

    I would like to hide (or color or change the position) the JTextField dynamically, ie, something like this..

    Object(textField_25.getText()).setVisible(false);

    Please help me if this is possible.

    Thanks in advance.

    Regards
    taps

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,105
    Rep Power
    6

    Default Re: Call Java Object event/method by JTextField data

    Stick the textfields in a HashMap so you can look them up through a specific key.

    Trail: Collections (The Java™ Tutorials)
    taps likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    taps is offline Member
    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Default Re: Call Java Object event/method by JTextField data

    Hi gimbal2,

    Many thanks for your help. I have gone through with different examples of HashMap in google. Learn a little (I am very new in Java )
    Most of the example shows how we can add key value pair to HashMap and retrieve values. Unfortunately don't find any example where I can pass the JTextField name and invoke an event or change the attributes of that JTextField etc (like change color/hiding).

    It will be very helpful if any of friends provide me the code to get the desire result of the problem mentioned above.

    Thanks again for your support.

    Regards
    taps

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,105
    Rep Power
    6

    Default Re: Call Java Object event/method by JTextField data

    What do you mean by "JTextField name". I have the sneaky suspicion that you are talking about the name of the variable.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    taps is offline Member
    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Default Re: Call Java Object event/method by JTextField data

    Hi gimbal2,

    You are right.

    Firstly, I am getting the variable name in another Textbox and then I would like to use that text to change the postion/color/size etc. by clicking another Button.

    Thanks again for your support.

    Regards
    taps

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,105
    Rep Power
    6

    Default Re: Call Java Object event/method by JTextField data

    No, I'm not right. You are manually setting the name property of the textfield in your code. And apparently you want to get the textfield with a specific name.

    Java Code:
    Map<String, JTextField> textfields = new HashMap<>();
    
    // assuming this is a textfield you created
    textField.setName("textField1");
    textfields.put("textField1", textField);
    
    
    // and when you later want to get the textfield back
    JTextField textField = textfields.get(otherTextField.getText());
    textField.doThingsAndStuff();
    I hope you get the idea. And if not then I'm probably hopelessly confused about what you want to achieve.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    taps is offline Member
    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Default Re: Call Java Object event/method by JTextField data

    Thanks gimbal2,

    Let me learn more Java.

    Regards
    taps
    gimbal2 likes this.

Similar Threads

  1. Replies: 3
    Last Post: 06-22-2012, 07:54 PM
  2. Replies: 3
    Last Post: 03-28-2012, 12:23 AM
  3. JTextField event help
    By micolord in forum AWT / Swing
    Replies: 4
    Last Post: 03-26-2012, 01:23 PM
  4. Replies: 1
    Last Post: 11-29-2011, 02:30 PM
  5. how to call other object's event
    By kirzo in forum New To Java
    Replies: 3
    Last Post: 07-11-2009, 01:23 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
  •