Skip to main content
Announcements
A fresh, new look for the Data Integration & Quality forums and navigation! Read more about what's changed.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Clear text from JTextField when mouse courser is placed

Hi All,

 

I'm trying to create a form and place a text box inside with instruction of what the user should type in, and when the user place the mouse courser in inside the textbox, the default text will be cleared.

Below is my tJava code. 

JFrame a = new JFrame ("example");
JTextField b = new JTextField("Insert Username");
b.setBounds(50,100,200,30);
a.add(b);
a.setSize(300,300);
a.setLayout(null);
a.setVisible(true);

what code should I add to clear the text box? 
for googling I think I should use a mouse listener, but I'm not a developer (only learning from examples 0683p000009MACn.png) so would appreciate if someone can modify the code above to include the additional code and post it.

 

Thanks in advance !

Labels (3)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

Thank you !!!

i used part of your code with another code I found online and merged them. I capture the user input into context to use them later in the job

 

attaching in case anyone need

 

globalMap.put("stop", false);
JFrame frame = new JFrame();
frame.setTitle("Configuration Form");

JPanel mainPanel = new JPanel();

mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel headingPanel = new JPanel();
JLabel headingLabel = new JLabel("This is the heading panel for our demo course");
headingPanel.add(headingLabel);

               // Panel to define the layout. We are using GridBagLayout
        JPanel panel = new JPanel(new GridBagLayout());
        // Constraints for the layout
        GridBagConstraints constr = new GridBagConstraints();
        constr.insets = new Insets(5, 5, 5, 5);     
        constr.anchor = GridBagConstraints.WEST;
        
                // Set the initial grid values to 0,0
        constr.gridx=0;
        constr.gridy=0;
  
        // Declare the required Labels
        JLabel userNameLabel = new JLabel("Enter your name :");
        JLabel pwdLabel = new JLabel("Enter your password :");
        JLabel emailLabel = new JLabel("Enter email :");
         
        // Declare Text fields
        JTextField userNameTxt = new JTextField(20);
        JPasswordField pwdTxt = new JPasswordField(20);
        JTextField emailTxt = new JTextField(20);
         
        panel.add(userNameLabel, constr);
        constr.gridx=1;
        panel.add(userNameTxt, constr);
        constr.gridx=0; constr.gridy=1;
         
        panel.add(pwdLabel, constr);
        constr.gridx=1;
        panel.add(pwdTxt, constr);
        constr.gridx=0; constr.gridy=2;
         
        panel.add(emailLabel, constr);
        constr.gridx=1;
        panel.add(emailTxt, constr);
        constr.gridy=3;
         
        constr.gridwidth = 2;
        constr.anchor = GridBagConstraints.CENTER;
        
               // Button with text "Register"
        JButton button = new JButton("Register");
        // add a listener to button
        button.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            headingLabel.setText("Thanks for registering. We'll get back to you shortly.");
            context.UserName = userNameTxt.getText();
            context.Password = pwdTxt.getText();
            context.email = emailTxt.getText();
            userNameTxt.setText("");
            pwdTxt.setText("");
            emailTxt.setText("");
            			frame.dispose();
			globalMap.put("stop", true);
          }
        });


  
               // Add label and button to panel
               panel.add(button, constr);
  
        mainPanel.add(headingPanel);
        mainPanel.add(panel);
 
        // Add panel to frame
        frame.add(mainPanel);
        frame.pack();
                frame.setSize(400, 400);
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        while(!((Boolean)globalMap.get("stop"))){
	Thread.sleep(1);
}

View solution in original post

6 Replies
Anonymous
Not applicable
Author

First of all, how is this job going to be run? Unless it is run via the Studio, it will not work. Running from the command line will not reveal the text box and neither will it be revealed via the TAC. 

 

If you are intending to run this via the Studio only, there is an easier way of doing this. You need to use the tMsgBox component. It allows you to format it to take text responses, which are then set inside the globalMap. As an example, I have quickly out the below together....

 

0683p000009M8I6.png

The above shows a mini job I have written. The config of the tMsgBox is as below....

0683p000009M8OD.png

You can set the "Question" and "Title" to be whatever you want them to be. You need to select the "Question" button. Icon can be anything you want.

 

The component where I return the response to this tMsgBox is a tJava. I am simply printing the result out to the terminal window. The code I used is this....

System.out.println(((String)globalMap.get("tMsgBox_1_RESULT")));

If you try this method, you will not have to worry about listeners, etc. 

Anonymous
Not applicable
Author

Thank you, but this is not what I'm looking for.

I'm looking in building my own custom frame which contains several text box rather than using the tMsgBox.

Anonymous
Not applicable
Author

OK, this is a hack to do this in Talend. You have to keep in mind that Talend is not meant for showing user interfaces and components are not supposed to be paused in order to show the UIs. So I needed to add a bit of a hack to ensure the component would not just end after the jFrame is produced.

 

Add this code to a tJava component and run your job. You should be able to work from this.

//Hack to stop the job from moving on from this component before the button is pressed. 
globalMap.put("stop", false);

JFrame f=new JFrame("Button Example"); 
//submit button
JButton b=new JButton("Submit");    
b.setBounds(100,100,140, 40);    
//textfield to enter name
JTextField textfield= new JTextField();
textfield.setBounds(110, 50, 130, 30);
//add to frame
		
f.add(textfield);
		
f.add(b);    
f.setSize(300,300);    
f.setLayout(null);    
f.setVisible(true);    
		
//action listener
b.addActionListener(new ActionListener() {
	        
	@Override
	public void actionPerformed(ActionEvent arg0) {
			System.out.println(textfield.getText());
			globalMap.put("stop", true);				
	}          
});

//Part of hack to prevent the component from finishing
while(!((Boolean)globalMap.get("stop"))){
	Thread.sleep(1);
}
	 
	
Anonymous
Not applicable
Author

Thank you !!!

i used part of your code with another code I found online and merged them. I capture the user input into context to use them later in the job

 

attaching in case anyone need

 

globalMap.put("stop", false);
JFrame frame = new JFrame();
frame.setTitle("Configuration Form");

JPanel mainPanel = new JPanel();

mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel headingPanel = new JPanel();
JLabel headingLabel = new JLabel("This is the heading panel for our demo course");
headingPanel.add(headingLabel);

               // Panel to define the layout. We are using GridBagLayout
        JPanel panel = new JPanel(new GridBagLayout());
        // Constraints for the layout
        GridBagConstraints constr = new GridBagConstraints();
        constr.insets = new Insets(5, 5, 5, 5);     
        constr.anchor = GridBagConstraints.WEST;
        
                // Set the initial grid values to 0,0
        constr.gridx=0;
        constr.gridy=0;
  
        // Declare the required Labels
        JLabel userNameLabel = new JLabel("Enter your name :");
        JLabel pwdLabel = new JLabel("Enter your password :");
        JLabel emailLabel = new JLabel("Enter email :");
         
        // Declare Text fields
        JTextField userNameTxt = new JTextField(20);
        JPasswordField pwdTxt = new JPasswordField(20);
        JTextField emailTxt = new JTextField(20);
         
        panel.add(userNameLabel, constr);
        constr.gridx=1;
        panel.add(userNameTxt, constr);
        constr.gridx=0; constr.gridy=1;
         
        panel.add(pwdLabel, constr);
        constr.gridx=1;
        panel.add(pwdTxt, constr);
        constr.gridx=0; constr.gridy=2;
         
        panel.add(emailLabel, constr);
        constr.gridx=1;
        panel.add(emailTxt, constr);
        constr.gridy=3;
         
        constr.gridwidth = 2;
        constr.anchor = GridBagConstraints.CENTER;
        
               // Button with text "Register"
        JButton button = new JButton("Register");
        // add a listener to button
        button.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            headingLabel.setText("Thanks for registering. We'll get back to you shortly.");
            context.UserName = userNameTxt.getText();
            context.Password = pwdTxt.getText();
            context.email = emailTxt.getText();
            userNameTxt.setText("");
            pwdTxt.setText("");
            emailTxt.setText("");
            			frame.dispose();
			globalMap.put("stop", true);
          }
        });


  
               // Add label and button to panel
               panel.add(button, constr);
  
        mainPanel.add(headingPanel);
        mainPanel.add(panel);
 
        // Add panel to frame
        frame.add(mainPanel);
        frame.pack();
                frame.setSize(400, 400);
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        while(!((Boolean)globalMap.get("stop"))){
	Thread.sleep(1);
}
Anonymous
Not applicable
Author

That looks good. As I said, it was the Thread.sleep hack that was the real trick. Anything else related to the development of a GUI at runtime you can get from Java examples. I mentioned that the tMsgBox component would not work when run outside of the Studio. I may have been wrong (I've not tried that recently). But this method will allow you to run the job from the command line and see your JFrame. It will not work when run on the TAC or TMC though.

Anonymous
Not applicable
Author

Yes that was really helpful, cause before that I used tSleep to keep the frame alive instead of disposing the frame on Click.

 

Yes, the tMsgBox works outside of the Studio - the reason to my question is actually trying to reduce the number of tMsgBox component I prompt to get input from the user and instead create one form.

later on I plan to save the user input so next time they run the job they can pick a previously used configuration.

 

thanks again.