Solved

save textfield data session

Posted on 2007-11-17
10
235 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:zolf
  • 6
  • 4
10 Comments
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20303750
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
 

Author Comment

by:zolf
ID: 20303764

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

Author Comment

by:zolf
ID: 20303864

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
 

Author Comment

by:zolf
ID: 20303869

do i have to add three JTextfield or one will do for all the option selected
0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20303887
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:zolf
ID: 20303926

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
 
LVL 13

Accepted Solution

by:
Bart Cremers earned 500 total points
ID: 20303938
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
 

Author Comment

by:zolf
ID: 20303958

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
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20303977
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
 

Author Comment

by:zolf
ID: 20303992

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

http://www.experts-exchange.com/Programming/Languages/Java/Q_22967438.html
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now