How to clear text fields in jpanel?

JFrame/JPanel refreshing and text fields

  • I'm trying to switch between displaying different JPanels in BorderLayout.CENTER in a JFrame. The JPanels each contain a JTextField and a JTextPane. Calling revalidate() and repaint() on the JFrame, I get a very strange result: the first time I switch to secondPanel by clicking secondButton, everything works as expected. Then when I try to switch back using firstButton, nothing happens. It only switches back when I click secondButton again, and after that it works just as expected, except in reverse: clicking firstButton displays secondPanel and clicking secondButton displays firstPanel. Calling revalidate() and repaint() on the JPanel to be displayed instead, everything works fine, until I click either text area. With every click, it's like part of the text "falls off" like it was just a facade, revealing the text of the corresponding area of the other JPanel underneath. If I type something, the text changes instantly to that of the other JPanel, and whatever I typed is added to it even after switching JPanels. It's really weird and hard to describe properly, so just see for yourselves. I have three questions: 1. what the hell, 2. why the hell, and 3. how do I fix it? import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MyFrame extends JFrame { class MyActionListener implements ActionListener { JPanel panel; MyActionListener(JPanel _panel) { panel = _panel; } @Override public void actionPerformed(ActionEvent ae) { MyFrame.this.add(panel, BorderLayout.CENTER); //MyFrame.this.revalidate(); //MyFrame.this.repaint(); panel.revalidate(); panel.repaint(); } } JPanel buttonPanel = new JPanel(); JButton firstButton = new JButton("first"); JPanel firstPanel = new JPanel(); JTextPane firstPane = new JTextPane(); JTextField firstField = new JTextField("first field"); JButton secondButton = new JButton("second"); JPanel secondPanel = new JPanel(); JTextPane secondPane = new JTextPane(); JTextField secondField = new JTextField("second field"); MyFrame() { firstPane.setText("first pane"); firstPanel.setLayout(new BorderLayout()); firstPanel.add(firstPane, BorderLayout.CENTER); firstPanel.add(firstField, BorderLayout.SOUTH); firstButton.addActionListener(new MyActionListener(firstPanel)); secondPane.setText("second pane"); secondPanel.setLayout(new BorderLayout()); secondPanel.add(secondPane, BorderLayout.CENTER); secondPanel.add(secondField, BorderLayout.SOUTH); secondButton.addActionListener(new MyActionListener(secondPanel)); buttonPanel.setLayout(new GridLayout(0,1)); buttonPanel.add(firstButton); buttonPanel.add(secondButton); add(buttonPanel, BorderLayout.WEST); add(firstPanel, BorderLayout.CENTER); setSize(new Dimension(300,300)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { new MyFrame(); } }

  • Answer:

    how do I fix it? Use a CardLayout. See the section from the Swing tutorial on http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html for more information and examples.

Blrp at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.