save textfield data session

hello there,

i have a popup menu button and i have three options on it.corresponding to the option i want to save data in textfield. for example when i clcik on the popup menu and select option 1 and enter a value in the textfield.then i clcik on the popup menu again and select the option 2.i should get a clear textfield to enter data for the second option in the textfield.now when i again click on the first option i should get to see the value entered in the textfield.how can i do this.

cheers
zolf
zolfAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bart CremersJava ArchitectCommented:
I can think of two ways to solve this:

1. Create 3 textfields and put them in a CardLayout. Make your popup listener display the correct "card" for the correct option.

2. Keep a mapping to the saved data somewhere accessible from your listener. Map to options to a String.

I think I would go for option 2.
0
zolfAuthor Commented:

can you please give me more details on your second suggestion.
0
zolfAuthor Commented:

refer to this image to get an idea what i am trying to acheive.

http://good-times.webshots.com/album/560907322SMsknK?vhost=good-times
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

zolfAuthor Commented:

do i have to add three JTextfield or one will do for all the option selected
0
Bart CremersJava ArchitectCommented:
Something like this:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.*;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class PopupExample {

    public static void main(String[] args) throws Exception {
        final JPopupMenu popup = new JPopupMenu();

        JMenuItem itemOne = new JMenuItem("One");
        JMenuItem itemTwo = new JMenuItem("Two");
        JMenuItem itemThree = new JMenuItem("Three");

        popup.add(itemOne);
        popup.add(itemTwo);
        popup.add(itemThree);

        JTextField textField = new JTextField(30);
        textField.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                maybeShowPopup(e);
            }

            public void mouseReleased(MouseEvent e) {
                maybeShowPopup(e);
            }

            private void maybeShowPopup(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popup.show(e.getComponent(), e.getX(), e.getY());
                }
            }

        });
        ActionListener actionListener = new MyActionListener(textField, itemOne);
        itemOne.addActionListener(actionListener);
        itemTwo.addActionListener(actionListener);
        itemThree.addActionListener(actionListener);

        JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());
        frame.add(textField);

        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

class MyActionListener implements ActionListener {
    private final Map<JMenuItem, String> mapping = new HashMap<JMenuItem, String>();
    private final JTextField linkedTextField;
    private JMenuItem currentItem;

    public MyActionListener(JTextField linkedTextField, JMenuItem currentItem) {
        this.linkedTextField = linkedTextField;
        this.currentItem = currentItem;
    }

    public void actionPerformed(ActionEvent e) {
        mapping.put(currentItem, linkedTextField.getText());

        JMenuItem source = (JMenuItem) e.getSource();

        if (mapping.containsKey(source)) {
            linkedTextField.setText(mapping.get(source));
        } else {
            linkedTextField.setText("");
        }

        currentItem = source;
    }
}
0
zolfAuthor Commented:

at present my code is like this.what this does is.depending on which popup option you choose it changes the label to the left of the popupbutton.now do i have to use your actionlistener class or can i insert them into my actionlistener

JPopupMenuButton emailOpt = new JPopupMenuButton();
            JPopupMenu popupMenuEmail = new JPopupMenu();
            JMenuItem email_1 = new JMenuItem("Email 1");
            JMenuItem email_2 = new JMenuItem("Email 2");
            JMenuItem  email_3 = new JMenuItem("Email 3");
            
            email_1.setActionCommand("Email 1");
            email_2.setActionCommand("Email 2");
            email_3.setActionCommand("Email 3");
            
            email_1.addActionListener(JMenuEmailActionListener);
            email_2.addActionListener(JMenuEmailActionListener);
            email_3.addActionListener(JMenuEmailActionListener);

      ActionListener JMenuEmailActionListener = new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                  /*JDialog JPAdd = new AddNewCustomer();
                  JPAdd.setModal(true);
                  JPAdd.show();*/
                  String str = e.getActionCommand();
                  
                  if(str.equals("Email 1"))
                  {                  
                        emailLabel_1.setText(str);
                  }
                  else if(str.equals("Email 2"))
                  {
                        emailLabel_1.setText(str);
                  }
                  else if(str.equals("Email 3"))
                  {
                        emailLabel_1.setText(str);
                  }
            }
      };
0
Bart CremersJava ArchitectCommented:
You can of course alter your original actionlistener to have the same behaviour. Using the actionCommand instead of the source is a possibility, that way you can create a mapping from like <String, String>.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zolfAuthor Commented:

i tried doing this.but it is not saving the values for the corresponding menu option.please help

ActionListener JMenuEmailActionListener = new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                  String str = e.getActionCommand();
                  
                  Map<String, String> mapping = new HashMap<String, String>();
                JTextField linkedTextField = textField_Email_1;
                JMenuItem currentItem;

                  if(str.equals("Email 1"))
                  {                  
                        emailLabel_1.setText(str);
                        mapping.put(str, linkedTextField.getText());

                    JMenuItem source = (JMenuItem) e.getSource();
                   
                    if (mapping.containsKey(source))
                    {
                        linkedTextField.setText(mapping.get(source));
                    }
                    else
                    {
                        linkedTextField.setText("");
                    }

                    currentItem = source;

                  }
                  else if(str.equals("Email 2"))
                  {
                        emailLabel_1.setText(str);
                        mapping.put(str, linkedTextField.getText());

                    JMenuItem source = (JMenuItem) e.getSource();

                    if (mapping.containsKey(source))
                    {
                        linkedTextField.setText(mapping.get(source));
                    }
                    else
                    {
                        linkedTextField.setText("");
                    }

                    currentItem = source;
                  }
                  else if(str.equals("Email 3"))
                  {
                        emailLabel_1.setText(str);
                        mapping.put(str, linkedTextField.getText());

                    JMenuItem source = (JMenuItem) e.getSource();

                    if (mapping.containsKey(source))
                    {
                        linkedTextField.setText(mapping.get(source));
                    }
                    else
                    {
                        linkedTextField.setText("");
                    }

                    currentItem = source;
                  }
            }
      };
0
Bart CremersJava ArchitectCommented:
The problem is that in your listener you instantiate a new empty hashmap every time a menu item is selected. So it will never find a previous value in the map.

You should create a real actionlistener class instead of an anonymous one. That way you can create the mapping only when the listener gets instantiated and keep it for the time the gui exists:

class JMenuEmailActionListener implements ActionListener {
    private final Map<JMenuItem, String> mapping = new HashMap<JMenuItem, String>();
    private final JTextField linkedTextField;
    private final JLabel labelForTextField;
    private JMenuItem currentItem;

    public JMenuEmailActionListener(JTextField linkedTextField, JLabel labelForTextField, JMenuItem currentItem) {
        this.linkedTextField = linkedTextField;
        this.labelForTextField = labelForTextField;
        this.currentItem = currentItem;
    }

    public void actionPerformed(ActionEvent e) {
        mapping.put(currentItem, linkedTextField.getText());

        JMenuItem source = (JMenuItem) e.getSource();
        labelForTextField.setText(source.getActionCommand());

        if (mapping.containsKey(source)) {
            linkedTextField.setText(mapping.get(source));
        } else {
            linkedTextField.setText("");
        }

        currentItem = source;
    }
}

As you can see, this one is much shorter then your original one.

You can create it like this:

        ActionListener listener = new JMenuEmailActionListener(textField_Email_1, emailLabel_1, email_1);

and simply add it to the three menu items:

            email_1.addActionListener(listener);
            email_2.addActionListener(listener);
            email_3.addActionListener(listener);



0
zolfAuthor Commented:

please refer to this link.i have opened a new question

http://www.experts-exchange.com/Programming/Languages/Java/Q_22967438.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.