Results 1 to 3 of 3
- 05-03-2008, 01:30 AM #1
Member
- Join Date
- May 2008
- Posts
- 18
- Rep Power
- 0
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:
For those who want to read my codes here is the link.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; } }
Thank you very much for reading this.
- 05-03-2008, 02:34 AM #2
just add an action listener that pops up another window.
My IP address is 127.0.0.1
- 05-03-2008, 07:04 AM #3
To use your comActionPerformed you can add an anonymous listener to your comboBox and forward the event, like this: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); } }
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
-
Is it possible to get and store items to a variable in a combo box?
By Soda in forum New To JavaReplies: 2Last Post: 12-05-2007, 01:19 PM -
Combo Box Event handling
By smajidali26 in forum AWT / SwingReplies: 1Last Post: 11-29-2007, 05:57 PM -
Require Links for free swing component
By Gajesh Tripathi in forum Advanced JavaReplies: 2Last Post: 08-11-2007, 10:24 PM -
Internationalization: Select language from combo box
By alejandrgarcia in forum AWT / SwingReplies: 0Last Post: 08-11-2007, 10:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks