JComboBox??


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: JComboBox??

  1. #1
    Join Date
    Jul 2009
    Posts
    7

    JComboBox??

    Having recently found and implemented JComboBox in my code, I cant seem to find anywhere that will help me with the action or itemlistener. What I want to do is set certain TextFields to "uneditable" if "NO" in my drop down menu in my ComboBox is selected...and editable if "YES" in the ComboBox is selected. I dont know if to use itemlisteneres or actionlisteners. Ive tried both and failed miserablly both times. Can someone help?

    Here is the specific part of the code:

    String [] pp = {"NO", "YES"};
    jcbppayment = new JComboBox(pp);
    jcbppayment.addItemListener(this);
    jcbppayment.addActionListener(this);

    jtfamount = new JTextField();
    jtfswpayment = new JTextField();
    jtfamount.setEditable(false);
    jtfswpayment.setEditable(false);

    ...


    public void actionPerformed(ActionEvent e) {
    }

    ...

    public void itemStateChanged(ItemEvent ie){
    }

  2. #2
    Join Date
    Jul 2009
    Posts
    3
    Here's a quick example of handling a performed action.

    Make sure to read through java tutorials before posting questions because lots of questions will probably already be answered:
    http://java.sun.com/docs/books/tutor.../combobox.html


    Code:
    public class ComboDemo extends JPanel implements ActionListener{
        public ComboDemo(){
            String[] yesNo= {"YES","NO!"};
            JComboBox comboBox = new JComboBox(yesNo);
            comboBox.addActionListener(this);
    
            add(comboBox);
        }
        public void actionPerformed(ActionEvent evt){
            String selected;
            JComboBox c = (JComboBox)evt.getSource();
            selected = (String)c.getSelectedItem();
            System.out.println("You selected: "+selected);
        }
        public static void createAndShowGUI(){
            JFrame myFrame = new JFrame();
            JPanel panel = new ComboDemo();
            myFrame.add(panel);
            myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            myFrame.setSize(200, 100);
            myFrame.setVisible(true);
    
        }
        public static void main(String args[]) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    Then you will change your components in the actionPerformed function based on the selected item.
    eg. myComponent.setEditable(false);

    Good luck
    Last edited by toonx; 07-08-2009 at 01:55 PM. Reason: gave a better example

Similar Threads

  1. Replies: 1
    Last Post: 11-12-2007, 02:33 AM
  2. Display Value in JcomboBox
    By daina in forum Java
    Replies: 7
    Last Post: 10-10-2007, 08:21 PM
  3. Jcombobox value
    By daina in forum Java
    Replies: 3
    Last Post: 10-03-2005, 12:27 PM
  4. JComboBox Problem
    By JavaMixin in forum Java
    Replies: 3
    Last Post: 09-02-2005, 01:59 AM
  5. Replies: 1
    Last Post: 08-01-2002, 10:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center