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:
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.