0

So I am trying to make a GUI in Java. I am pretty new to Java GUIs. So here is my code:

private class thehandler implements  ActionListener {
    public void actionPerformed(ActionEvent event){//this is what is going to handle an event
        String string = "";

        if(event.getSource() == item1)//if they click enter on item1
                string=String.format("field 1: %s", event.getActionCommand());
        else if(event.getSource() == item2)//if they click enter on item2
            string = String.format("field 2: %s", event.getActionCommand());
        else if(event.getSource() == item3)//if they click enter on item3
            string = String.format("field 3: %s", event.getActionCommand());
        else if(event.getSource() == passField)//if they click enter on passField
            string = String.format("Password field is: %s", event.getActionCommand());
        }
}

I get an error on string=String.format("field 1: %s", event.getActionCommand()); and all the other String.format lines. It says "The method format(String, Object[]) in the type String is not applicable for the arguments (String, String)"

I have no clue how to fix this. I just downloaded the JRE and JDK 8 if that helps at all. Thank you!

slackmart
  • 4,754
  • 3
  • 25
  • 39
  • possible duplicate of [How to use java.String.format?](http://stackoverflow.com/questions/3695230/how-to-use-java-string-format) – Harry Johnston Apr 20 '14 at 00:03

2 Answers2

1

Simply convert it to a string

event.getActionCommand().toString()

Alternatively (without stringFormat):

string="field 1:" + event.getActionCommand();
meda
  • 45,103
  • 14
  • 92
  • 122
  • getActionCommand returns a String already. http://docs.oracle.com/javase/6/docs/api/java/awt/event/ActionEvent.html – csvan Apr 19 '14 at 20:23
1

You are using String.format() the wrong way. To see how it should be used (applicable to your use case), consult this question (and answers):

How to use java.String.format in Scala?

Community
  • 1
  • 1
csvan
  • 8,782
  • 12
  • 48
  • 91