Solved

Checkbox setSate help

Posted on 2007-11-19
13
245 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

705 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

21 Experts available now in Live!

Get 1:1 Help Now