I was just writing a basic GUI
program in Java using event handler. all code is working fine except a little error at
string s = String.format("field 1 is %s", event.getActionCommand())
Error is
the method format(String, object) of type String is not applicable for arguments format(String, String)`'.
So definitely event.getActionCommand())
is returning a string. But format method is not accepting that.
I was doing this code in eclipse (kepler) using one of the famous tutorial on internet. His code worked but not mine. I am quite a beginner in Java. Please help me in finding this silly error.
Here is my code.
package tushar_GUI;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
public class tush extends JFrame{
private JTextField textbox1;
private JTextField textbox2;
private JTextField textbox3 ;
private JPasswordField textbox_pass1;
public tush(){
super("TITLE");
setLayout(new FlowLayout());
textbox1 = new JTextField(10);
add(textbox1);
textbox2 = new JTextField("Enter Your Text Here", 30);
add(textbox2);
textbox3 = new JTextField("This is UNEDITABLE textbox", 30);
textbox3.setEditable(false);
add(textbox3);
textbox_pass1 = new JPasswordField("password");
add(textbox_pass1);
thehandler handler = new thehandler();
textbox1.addActionListener(handler);
textbox2.addActionListener(handler);
textbox3.addActionListener(handler);
textbox_pass1.addActionListener(handler);
}
private class thehandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String string = "";
if(event.getSource() == textbox1)
string = String.format("field 1 is %s", event.getActionCommand());
else if(event.getSource() == textbox2)
string = String.format("field 2 is %s", event.getActionCommand());
else if(event.getSource() == textbox3)
string = String.format("field 3 is %s", event.getActionCommand());
else if(event.getSource() == textbox_pass1)
string = String.format("password field is %s", event.getActionCommand());
JOptionPane.showMessageDialog(null, string);
}
}
}
And here my main class
package tushar_GUI;
import javax.swing.JFrame;
public class gui1 {
public static void main (String[] args){
tush tushObject = new tush();
tushObject.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tushObject.setSize(500, 500);
tushObject.setVisible(true);
}
}
You haven't added a action command to your textboxes
solution:
textbox1 = new JTextField(10);
textbox1.setActionCommand("textbox1 ");
add(textbox1);
textbox2 = new JTextField("Enter Your Text Here", 30);
textbox2.setActionCommand("textbox2 ");
add(textbox2);
textbox3 = new JTextField("This is UNEDITABLE textbox", 30);
textbox3.setActionCommand("textbox3 ");
textbox3.setEditable(false);
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.