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?
 
Bart CremersConnect With a Mentor Java 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
 
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.