I am creating a small application . In this application I have 5 words. I have one JtextField and JButton.The name of JtextField is set as name1 and JButton name set as next.
I want to set a default word in name1 from 1st word of string array ( from this code I am giving one example is need to see in JtextField is as "me" ) when application run I want to see the word from String array before the the next button click. how I can set the data to name1?
code is as follows:
String s[]={"me","and","my","friends","are"};
int i=0;
private void nextActionPerformed(java.awt.event.ActionEvent evt) {
if(i>=s.length)
i=0;
name1.setText(s[i]);
i++;
}
Call the method after you created your fields with null as argument as you don't use the argument.
nextActionPerformed(null);
This will simulate that the button is pressed when the program starts up.
Assuming you have a button which has action Listener attached. If not then it should be something like below:
nextButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
nextActionPerformed(e);
// Or move your code here from nextActionPerformed(e)
}
});
//And you can also initialize your text filed with first value of array:
textField.setText(s[0]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.