Solved

Checkbox setSate help

Posted on 2007-11-19
13
276 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
  • 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
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!

 
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 500 total points
ID: 20318535
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example for saving data 19 77
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 77
Eclipse with various Java releases 7 50
spring maven example issues 3 43
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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:

733 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