simple GUI implementation problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: simple GUI implementation problem

  1. #1
    Join Date
    Nov 2005
    Posts
    2

    Question simple GUI implementation problem

    I have to research GUI components to provide an interface for a university OOP project. I am halfway through chapter 11 of the Deitel "Java how to Program" 6th edition and have tried to compile and run the code shown below but get 4 errors which appear to relate to the format method of String:

    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JPasswordField;
    import javax.swing.JOptionPane;

    public class TextFieldFrame extends JFrame
    {
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JPasswordField passwordField;

    public TextFieldFrame(){
    super("Testing JTextField and JPasswordField");
    setLayout(new FlowLayout());

    textField1 = new JTextField(10);
    add(textField1);

    textField2 = new JTextField("Enter text here");
    add(textField2);

    textField3 = new JTextField("Uneditable text field", 21);
    textField3.setEditable(false);
    add(textField3);

    passwordField = new JPasswordField("Hidden text");
    add(passwordField);

    TextFieldHandler handler = new TextFieldHandler();
    textField1.addActionListener(handler);
    textField2.addActionListener(handler);
    textField3.addActionListener(handler);
    passwordField.addActionListener(handler);
    }

    private class TextFieldHandler implements ActionListener{
    public void actionPerformed(ActionEvent event){
    String string = "";

    if (event.getSource()==textField1)
    string = String.format("textField1: %s", event.getActionCommand());
    else if (event.getSource()==textField2)
    string = String.format("textField2: %s", event.getActionCommand());
    else if (event.getSource()==textField3)
    string = String.format("textField3: %s", event.getActionCommand());
    else if (event.getSource()==passwordField)
    string = String.format("passwordField: %s", new String(passwordField.getPassword()));

    JOptionPane.showMessageDialog(null, string);
    }
    }
    }






    import javax.swing.JFrame;
    public class TextFieldTest
    {
    public static void main(String[] args)
    {
    TextFieldFrame textFieldFrame = new TextFieldFrame();
    textFieldFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    textFieldFrame.setSize(325,100);
    textFieldFrame.setVisible(true);
    }
    }





    The errors are as follows:

    ---------- Compile ----------
    TextFieldFrame.java:45: cannot resolve symbol
    symbol : method format (java.lang.String,java.lang.String)
    location: class java.lang.String
    string = String.format("textField1: %s", event.getActionCommand());
    ^
    TextFieldFrame.java:47: cannot resolve symbol
    symbol : method format (java.lang.String,java.lang.String)
    location: class java.lang.String
    string = String.format("textField2: %s", event.getActionCommand());
    ^
    TextFieldFrame.java:49: cannot resolve symbol
    symbol : method format (java.lang.String,java.lang.String)
    location: class java.lang.String
    string = String.format("textField3: %s", event.getActionCommand());
    ^
    TextFieldFrame.java:51: cannot resolve symbol
    symbol : method format (java.lang.String,java.lang.String)
    location: class java.lang.String
    string = String.format("passwordField: %s", new String(passwordField.getPassword()));
    ^
    4 errors

    Output completed (0 sec consumed) - Normal Termination


    Could anyone please tell me how to correct the code to get the application running properly?

    Thanks.
    Last edited by Stevo27; 11-26-2005 at 07:09 PM.

  2. #2
    Join Date
    Nov 2005
    Posts
    2

    Thumbs up Problem solved

    Figured out the solution - I had the compile tool set to Java 1.4 rather than 1.5. Changed it and it works fine.
    Thanks anyway.

Similar Threads

  1. Problem with Drag and Drop
    By Jim Pragit in forum .NET
    Replies: 5
    Last Post: 08-10-2005, 07:16 AM
  2. Simple problem
    By Nate in forum .NET
    Replies: 1
    Last Post: 06-20-2002, 11:04 AM
  3. simple XML APPENDING problem
    By scopee in forum XML
    Replies: 4
    Last Post: 04-13-2001, 03:31 AM
  4. Simple SQL Parameter problem
    By Eric in forum Database
    Replies: 1
    Last Post: 11-10-2000, 02:05 AM
  5. Simple report problem
    By Gary Thompson in forum authorevents.kurata
    Replies: 1
    Last Post: 04-20-2000, 08:13 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center