Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

save textfield data session

Posted on 2007-11-17
10
Medium Priority
?
280 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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
 

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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

580 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