I am using a card layout and have 2 panels associate with it. One panel has a J text field. I want the value I previously entered into Jpanel to disappear after I follow these steps:
- Enter something in JtextField
- Go to the other panel
- Then go back to the first panel
Also, while I am on the Text Panel, I don't want the text to disappear when I lost focus to the text field. How can I accomplish this behavior?
Here is a simple program to show my problem.
import java.awt.BorderLayout;
public class CardLayoutDemo implements ItemListener
{
JPanel cards; //a panel that uses CardLayout
final static String BUTTONPANEL = "Card with JButtons";
final static String TEXTPANEL = "Card with JTextField";
public void addComponentToPane(Container pane)
{
//Put the JComboBox in a JPanel to get a nicer look.
JPanel comboBoxPane = new JPanel(); //use FlowLayout
String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };
JComboBox cb = new JComboBox(comboBoxItems);
cb.setEditable(false);
cb.addItemListener(this);
comboBoxPane.add(cb);
//Create the "cards".
JPanel card1 = new JPanel();
card1.add(new JButton("Button 1"));
card1.add(new JButton("Button 2"));
card1.add(new JButton("Button 3"));
//my TEst panel----------------------------------------------------------------
JPanel card2 = new JPanel();
card2.setLayout(null);
JLabel lblNewLabel = new JLabel("ncurrency");
lblNewLabel.setBounds(77, 136, 92, 27);
card2.add(lblNewLabel);
//NCurrencyTextField currencyTextField = new NCurrencyTextField();
JTextField currencyTextField = new JTextField();
currencyTextField.setBounds(179, 139, 113, 27);
card2.add(currencyTextField);
JLabel lblNewLabel_1 = new JLabel("NText field label");
lblNewLabel_1.setBounds(77, 212, 79, 14);
card2.add(lblNewLabel_1);
JTextField textField = new JTextField();
textField.setBounds(179, 209, 113, 20);
card2.add(textField);
textField.setColumns(10);
//END of my test panel----------------------------------------------------
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);
((CardLayout) cards.getLayout()).show(cards, TEXTPANEL);
pane.add(comboBoxPane, BorderLayout.PAGE_START);
pane.add(cards, BorderLayout.CENTER);
}
@Override
public void itemStateChanged(ItemEvent evt)
{
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, (String) evt.getItem());
}
/**
* Create the GUI and show it. For thread safety, this method should be invoked from the event dispatch thread.
*/
private static void createAndShowGUI()
{
//Create and set up the window.
JFrame frame = new JFrame("CardLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
CardLayoutDemo demo = new CardLayoutDemo();
demo.addComponentToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
/* Use an appropriate Look and Feel */
try
{
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
catch (UnsupportedLookAndFeelException ex)
{
ex.printStackTrace();
}
catch (IllegalAccessException ex)
{
ex.printStackTrace();
}
catch (InstantiationException ex)
{
ex.printStackTrace();
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}
}