Search code examples
javaswingjtextfield

How to add JTextField in time running in Java?


In order to develop a edit text in java for study. I'm with a problem: My program once opened by the user, if the user to click in button "Search", then the ActionListener will add a field in Jpanel.

For example: I have a class JToolBar that sets up jtoolbar menu that extends JPanel. Then, I add it in JFrame. Within that JToolbar there's a button "Search", if the user to click at this button, a JTextField will appears side this menu instantly.

I try to create a class private within that JToolBar class. So, I just add the JTextField to JPanel contains the JToolbar. However, is not working. There's no error. Simply not appears the JTextField. What I do to solve that problem ?


Solution

  • When you add a component to a visible GUI the general code is:

    panel.add(...);
    panel.revalidate();
    panel.repaint();  // sometimes needed
    

    You need the revalidate() to tell the layout manager that a component has been added.