i have two classes. Main class and tester class. I have to create a textfield so your can enter there name. The problem is that in tester class i am getting error on add() method. is there simple fix for this? i want to create the textfield in tester class tho.

Main class

...
       public void init()
                {        
                    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
                    displayClass = new Display(); 
                    setContentPane(displayClass);  
                    setFocusable(true);      
                    addKeyListener(this);         //keys
                    addMouseListener(this);          //clicking
                    addMouseMotionListener(this);       //mouse motion
            }
       ...      
        public class Display extends JPanel
            {

                public void paintComponent(Graphics g)
                {   
                    super.paintComponent(g);       

                       testerObject.paint(g);
                    }
         }

tester Class

 ...
    JTextField jName = new JTextField(10);
    ...
    public void paint(Graphics g)
    {
    jName.requestFocus();
                jName.setBounds(900, 300, 100, 20);
                add(jName);
                }
}

Edited 3 Years Ago by game06

You have forgotten to include the details of the error. Remember that error messages are not mere annoyances; they are someone's sincere attempt to help you, so you should read them, take them seriously, and share them with anyone else who wants to help you. So if your error includes a message, please share it.

It is hard to guess the cause of the problem based on only the small portion of the code you have included here. It is impossible to reproduce your problem, but you should be aware that it is very strange to add components in the paint method. Normally you setup all the components that you need once and they stay there until you have a reason to change them. Adding the same component repeatedly strongly suggests you are suffering from some misunderstanding.

Edited 3 Years Ago by bguild

The method add(JTextField) is undefined for the type tester

That seems clear enough. Obviously Tester doesn't implement an add method, and doesn't extend any class that it can inherit add from.
ps the paint method is seriously wrong. Every time it's called you add jName again. Depending on what's happening on the screen paint may be called many times - it's outside your control.

well i am trying to create a game in japplet. when game ends i want to get user input. so user can enter their name and i can store in database. joptionpane wont look nature bc another window will pop up. thats why iam thinking jtextfield. is there a better way? or should i stick with jtextfield.

javax.swing.JTextField is usually an excellent way to allow the user to type small amounts of text, but it can't work on its own. It needs to be a component of something that is visible on the screen such as a JFrame or a JPanel inside a JFrame. You need to stop trying to add it to an object that has no add method and decide where you really want your JTextField to live.

This question has already been answered. Start a new discussion instead.