How to restart a JFrame in Java without closing it?

1

I have an application in Java , which after a process, I need to restart, but not close.

That is to say: I need all the components to take the initial state, as when the JFrame opens for the first time.

Does anyone know which finger method to use or if it can?

    
asked by Ivan 01.08.2016 в 03:29
source

3 answers

0

Personally, I think that if you want to clean the content you should assign the initial values

public static void main(String s[]) {

    JFrame frame = new JFrame("JFrame Example");

    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());

    JLabel etiqueta = new JLabel("Pon algo aqui");

    final JTextField texto = new JTextField();
    texto.setPreferredSize(new Dimension(100, 50));

    JButton boton = new JButton();
    boton.setText("Resetear datos");

    boton.addActionListener(new ActionListener()
    {
          public void actionPerformed(ActionEvent e)
          {
              texto.setText(null);
          }
        });

    panel.add(etiqueta);
    panel.add(texto);
    panel.add(boton);

    frame.add(panel);
    frame.setSize(300, 100);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

}

In this example you only have one field but the operation will be the same:

Stuffed field

and when you press the button:

NOTES:

  • the elements to be modified within the listener must be final
  • if you have many elements, create a separate method
answered by 01.08.2016 / 10:05
source
0

you can try the following:

removeAll()//or remove(JComponent)
revalidate();
repaint();

It was an answer to a similar question: link

    
answered by 01.08.2016 в 03:41
0

If all you want to do is get your components in their initial state, then maybe all you need to do is remove all the text components. You could use the restart (), removeAll (), repaint (), depends on what you want to do ...

Greetings !!!

    
answered by 01.08.2016 в 03:48