0

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:

  1. Enter something in JtextField
  2. Go to the other panel
  3. 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();
        }
    });
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2628641
  • 2,035
  • 4
  • 29
  • 45
  • `card2.setLayout(null);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead [use layout managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jun 07 '14 at 02:13

3 Answers3

1

I want the value I previously entered into Jpanel to disappear.

Add a ComponentListener to the panel and listen for the componentShow(...) event to reset the text field.

while I am on the Text Panel, I don't want the text to disappear when I lost focus to the text field

I don't see this behaviour. Once I enter text in either of the two text field, the text remains there.

As a general comment, don't use a null layout. Swing was designed to be used with layout managers.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

You can discard values like this:

panel.remove();
revalidate();
repaint();

So on mouseClick or whatever you think is appropriate, you might try the method above.

Arash Saidi
  • 2,228
  • 20
  • 36
  • So when I run this, all components in the Text Panel disappeared because of the removeAll() method. – user2628641 Jun 06 '14 at 16:02
  • @user2628641 Ok, replace removeAll() with just remove() on the panel you want to have the text removed and see if that works. – Arash Saidi Jun 06 '14 at 16:12
0

change your itemStateChanged(ItemEvent evt) like below

public void itemStateChanged(ItemEvent evt)
{
    CardLayout cl = (CardLayout) (cards.getLayout());
    cl.show(cards, (String) evt.getItem());
    currencyTextField.setText("");
    textField.setText("");
}

Also don't forget to declare JTextField global

Full Code:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

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";
    JTextField currencyTextField;
    JTextField textField;

    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();
        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);

        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);
    }

    public void itemStateChanged(ItemEvent evt) {
        CardLayout cl = (CardLayout) (cards.getLayout());
        cl.show(cards, (String) evt.getItem());
        currencyTextField.setText("");
        textField.setText("");
    }

    /**
     * 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() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
Arijit
  • 1,633
  • 2
  • 21
  • 35