Solved

save textfield data session

Posted on 2007-11-17
10
252 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example issue 6 38
web application structure 18 76
check java version using powershell 13 80
hibernate example for saving data 19 37
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

816 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

8 Experts available now in Live!

Get 1:1 Help Now