2

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);

  }
}
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
  • Add action listener to array of buttons `jbtChoc[i].addActionListener(this);` – SatyaTNV Feb 11 '16 at 12:24
  • *"so that when they are pressed a new frame is opened."* - Please see [this](http://stackoverflow.com/q/9554636/4857909), there are better alternatives than using multiple `JFrame`s. – Lukas Rotter Feb 11 '16 at 12:55

1 Answers1

3

if it is possible to load an array of ImageIcon to be loaded with images from an on line source

Image icon from url

URL url = new URL("UrlPath");
BufferedImage img = ImageIO.read(url);
ImageIcon icon = new ImageIcon(img);

In your case

URL url = new URL(namesImage[x]);
BufferedImage img = ImageIO.read(url);
foodImages[x] = new ImageIcon(img);

is there a way to add a action listener to an array of buttons

Add action listener to array of buttons

jbtChoc[i].addActionListener(this);

when they are pressed a new frame is opened.

Your action performed method

public void actionPerformed(ActionEvent e){
   if( e.getSource() instanceof JButton) {
       //do your action here
   }
}
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31