• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

Checkbox setSate help

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
zolf
Asked:
zolf
  • 8
  • 5
1 Solution
 
zolfAuthor Commented:

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
 
zolfAuthor Commented:


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
 
objectsCommented:
at what point did u want it to become checked?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
objectsCommented:
you need to check the *selected* checkbox
0
 
zolfAuthor Commented:

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

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
 
zolfAuthor Commented:

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
 
objectsCommented:
> 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
 
zolfAuthor Commented:

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
 
zolfAuthor Commented:

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
 
objectsCommented:
you haven't said when they should be checked and unchecked, ie. under what condition should they change state
0
 
zolfAuthor Commented:

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
 
objectsCommented:
0

Featured Post

Industry Leaders: 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!

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now