I want to know if it is possible to load an array of ImageIcon to be loaded with images from an on line source so that the images are not stored locally. And is there a way to add a action listener to an array of buttons so that when they are pressed a new frame is opened.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MenuView extends JFrame {
String[] names =
{"banana split", "chicken with rice", "rice", "noodles", "fried vermicelli", "smoothie"};
String[] namesImage = {"banana split.jpeg", "chicken with rice.jpeg", "rice.jpeg", "noodles.jpeg",
"fried vermicelli.jpeg", "smoothie.jpeg"};
ImageIcon[] foodImages = new ImageIcon[namesImage.length];
JButton[] jbtChoc = new JButton[names.length];
{
for (int i = 0; i < names.length; i++) {
jbtChoc[i] = new JButton(names[i]);
for (int x = 0; x < names.length; x++) {
foodImages[x] = new ImageIcon(namesImage[x]);
}
}
}
/**
* Constructor for the MenuView.
*/
public MenuView() {
Container cont = getContentPane();
cont.setLayout(new BorderLayout(5, 5));;
cont.setBackground(Color.white);
JPanel girdSetup = new JPanel(new GridLayout(2, 3, 5, 5));
for (int i = 0; i < foodImages.length; i++) {
jbtChoc[i].setIcon(foodImages[i]);
girdSetup.add(jbtChoc[i]);
jbtChoc[i].setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc[i].setHorizontalTextPosition(AbstractButton.CENTER);
}
cont.add(girdSetup, BorderLayout.CENTER);
}
/**
* Main method for test.
*
* @param args Initial setup.
*/
public static void main(String[] args) {
MenuView frame = new MenuView();
frame.setTitle("MenuView");
frame.setSize(950, 400);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}