Solved

Checkbox setSate help

Posted on 2007-11-19
13
282 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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:
The viewer will learn how to implement Singleton Design Pattern in Java.

707 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