Results 1 to 3 of 3
  1. #1
    impact is offline Member
    Join Date
    May 2008
    Posts
    18
    Rep Power
    0

    Default how to add links (Action) to a combo box

    Hi,
    I got a window now, a button on it and a combo box. The combo box got few animal name list in it. The button is a search button. Can some one please show me how to add action to these combo box lists?

    for example some one selects the 3rd option and click search, I want to go to a link. For every option there will be a separate link. How do I add this in the combo box action?

    I am novice in java, so please explain it to me or show me some tutorials.

    Here is my combo box code:

    Java Code:
    public void addComboBox() {
        
            String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
            
            //Create the combo box, select the item at index 0.
            JComboBox petList = new JComboBox(petStrings);
            petList.setSelectedIndex(0);
            petList.addActionListener(this);
            
            //Lay out the demo.
            picture = new JLabel();
            picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
            picture.setHorizontalAlignment(JLabel.CENTER);
            add(petList, BorderLayout.PAGE_START);
            add(picture, BorderLayout.PAGE_END);
            setBorder(BorderFactory.createEmptyBorder(40,40,40,40));
        }
        
        // This is for ComboBox
        public void comActionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox)e.getSource();
            String petName = (String)cb.getSelectedItem();
            updateLabel(petName);
        }
            
        protected void updateLabel(String name) {
            ImageIcon icon = createImageIcon("images/" + name + ".gif");
            picture.setIcon(icon);
            picture.setToolTipText("A drawing of a " + name.toLowerCase());
            if (icon != null) {
                picture.setText(null);
            } else {
                picture.setText("Image not found");
            }
        }
    
        /** Returns an ImageIcon, or null if the path was invalid. */
        protected static ImageIcon createImageIcon(String path) {
            java.net.URL imgURL = NewClass.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
    For those who want to read my codes here is the link.

    Thank you very much for reading this.

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    10

    Default

    just add an action listener that pops up another window.
    My IP address is 127.0.0.1

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    11

    Default

    Java Code:
    public class NewClassRx extends JPanel implements ActionListener {
    
        // The implementation for "this" (the enclosing class)
        // ActionListener.
        public void actionPerformed(ActionEvent e) {
            // This method will be receiving events generated
            // by the JComboBox.
            if ("disable".equals(e.getActionCommand())) {
                b1.setEnabled(true);
            } else {
                b1.setEnabled(true);
            }
        }  
    
        public void addComboBox() {
            ...
            petList.addActionListener(this);
            ...
        }
    
        public void comActionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox)e.getSource();
            String petName = (String)cb.getSelectedItem();
            updateLabel(petName);
        }
    }
    To use your comActionPerformed you can add an anonymous listener to your comboBox and forward the event, like this:
    Java Code:
            petList.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    // The events from the comboBox will be
                    // received in this method.
                    // Pass them forward to your custom method:
                    comActionPerformed(e);
                }
            });

Similar Threads

  1. Replies: 2
    Last Post: 12-05-2007, 02:19 PM
  2. Combo Box Event handling
    By smajidali26 in forum AWT / Swing
    Replies: 1
    Last Post: 11-29-2007, 06:57 PM
  3. Require Links for free swing component
    By Gajesh Tripathi in forum Advanced Java
    Replies: 2
    Last Post: 08-11-2007, 10:24 PM
  4. Internationalization: Select language from combo box
    By alejandrgarcia in forum AWT / Swing
    Replies: 0
    Last Post: 08-11-2007, 10:26 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
  •