?
Solved

Checkbox setSate help

Posted on 2007-11-19
13
Medium Priority
?
287 Views
Last Modified: 2012-08-14
hello there,

i have a popup button which when clicked shows the options in it.next to the popup i have a textfield which allows the user to enter the value for that popup option choosen.now i want the checkbocmenuitem to have a check mark only after the user enters value in the textfiled. can somebody help me to acheive this.
for example i have a popup menu with three email. email1,email2,email3.
look at this image to get an idea

http://good-times.webshots.com/photo/2185636830102266092ObqydT

JPopupMenuButton emailOpt = new JPopupMenuButton();
            JPopupMenu popupMenuEmail = new JPopupMenu();
            email_1 = new JCheckBoxMenuItem("Email 1");
            email_2 = new JCheckBoxMenuItem("Email 2");
            email_3 = new JCheckBoxMenuItem("Email 3");
            
            
          ActionListener listener = new JMenuEmailActionListener(textField_Email_1, emailLabel_1, email_1);

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

class JMenuEmailActionListener implements ActionListener
      {
          
          JTextField linkedTextField;
          JLabel labelForTextField;
          JCheckBoxMenuItem currentItem;

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

          public void actionPerformed(ActionEvent e)
          {
                email_1.setState(false);
                  email_2.setState(false);
                  email_3.setState(false);
                System.out.println(linkedTextField.getText()+ "        " +currentItem.getText());
                mappingEmail.put(currentItem, linkedTextField.getText());

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

              if (mappingEmail.containsKey(source))
              {
                  linkedTextField.setText(mappingEmail.get(source));
              }
              else
              {
                  linkedTextField.setText("");
              }
              currentItem = source;
          }
      }
0
Comment
Question by:zolf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 

Author Comment

by:zolf
ID: 20317993

class  JTFEmailListener implements ActionListener
      {
            
            public void actionPerformed(ActionEvent e)
          {
                  if(textField_Email_1.hasFocus())
                  {
                        email_1.setState(true);
                        email_2.setState(true);
                        email_3.setState(true);
                  }
          }
            
      }
0
 

Author Comment

by:zolf
ID: 20318015


ignore my second comment.now i can check mark the menu item when i getfocus on the textfield.but how do i do the same for the second and third item.i have one textfield for all three option

class  JTFEmailListener implements FocusListener
      {
            
            public void focusGained(FocusEvent focusEvent)
          {
                  if(textField_Email_1.hasFocus())
                  {
                        System.out.println("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq");
                        email_1.setState(true);
                        //email_2.setState(true);
                        //email_3.setState(true);
                  }
          }
            
            public void focusLost(FocusEvent focusEvent)
          {
                  
          }
            
      }
0
 
LVL 92

Expert Comment

by:objects
ID: 20318023
at what point did u want it to become checked?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 20318038
you need to check the *selected* checkbox
0
 

Author Comment

by:zolf
ID: 20318073

when a user enters a value in the textbox.i want the checkbox to appear next to the popup item.
0
 

Author Comment

by:zolf
ID: 20318176

how to i disable the checkbox to check mark the item i check on.because now i can assign the check mark when i clcik in the textfield but again when i click on a option the sheck mark disappears.
0
 

Author Comment

by:zolf
ID: 20318181

my code looks like this

public void actionPerformed(ActionEvent e)
          {
                //email_1.setState(false);
                  //email_2.setState(false);
                  //email_3.setState(false);
                System.out.println(linkedTextField.getText()+ "        " +currentItem.getText());
                mappingEmail.put(currentItem, linkedTextField.getText());

              JCheckBoxMenuItem source = (JCheckBoxMenuItem) e.getSource();
              labelForTextField.setText(source.getActionCommand());
              linkedTextField.addFocusListener(
                             new FocusAdapter() {
                                      public void focusGained(FocusEvent focusEvent)
                                      {
                                            if(currentItem.getText().equals("Email 1"))
                                            {
                                                  email_1.setState(true);
                                            }
                                            else if(currentItem.getText().equals("Email 2"))
                                            {
                                                  System.out.println(email_1.getSelectedObjects());
                                                  email_2.setState(true);
                                            }
                                            else if(currentItem.getText().equals("Email 3"))
                                            {
                                                  email_3.setState(true);
                                            }
                                      }
                                   }
                                );
              if (mappingEmail.containsKey(source))
              {
                  linkedTextField.setText(mappingEmail.get(source));
              }
              else
              {
                  linkedTextField.setText("");
              }
              currentItem = source;
          }
      }
0
 
LVL 92

Expert Comment

by:objects
ID: 20318389
> when i clcik in the textfield but again when i click on a option the sheck mark disappears.

whats unchecking it?

wouldn't u be better off using a document or property listener
when exactly should it be checked and when not?
0
 

Author Comment

by:zolf
ID: 20318430

yes i think i have to redesign the logic.because after getting those values  i need to save those values in the db. which i think will get more complicated.
0
 

Author Comment

by:zolf
ID: 20318436

can you please give me some ideas how i should go about with this.i have other popup menu also.for example address popup will have home,business,warehouse. how do i collect these data till the user clciks on the save button.which will save all those values in db.
0
 
LVL 92

Expert Comment

by:objects
ID: 20318456
you haven't said when they should be checked and unchecked, ie. under what condition should they change state
0
 

Author Comment

by:zolf
ID: 20318500

when the jtextfield has value inside it.the item should be checked.when it has no value inside the textfield it should be unchecked.
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 20318535
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

770 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