I have a Java Program that I need to create that will show five random images selected form my image folder of 50 images. upon clicking a button the program should select 5 new random images. I have most of it figured out except for my ActionListener. Is it possible to implement an ActionListener that will simply run my main class over again thereby "restarting" the program and selecting 5 new images?

Any help would be greatly appreciated

Heres what I have so far

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class CardDisplay extends JFrame {

//Creat JButton
JButton jbtNew = new JButton("New Cards");

//Initialize Variables  
        int number1 = (int)(Math.random()*54+1);  
        int number2 = (int)(Math.random()*54+1);
        int number3 = (int)(Math.random()*54+1);
        int number4 = (int)(Math.random()*54+1);
        int number5 = (int)(Math.random()*54+1);    

        private ImageIcon card1 = new ImageIcon(number1 + ".png");
        private ImageIcon card2 = new ImageIcon(number2 + ".png");
        private ImageIcon card3 = new ImageIcon(number3 + ".png");
        private ImageIcon card4 = new ImageIcon(number4 + ".png");
        private ImageIcon card5 = new ImageIcon(number5 + ".png");



//Construct Layout
    public CardDisplay() {
        setLayout(new GridLayout(1,5));
        add(new JLabel(card1));
        add(new JLabel(card2));
        add(new JLabel(card3));
        add(new JLabel(card4));
        add(new JLabel(card5));

        //Create Panel
        JPanel panel = new JPanel();
            panel.add(jbtNew);
            add(panel); //add Panel

        //Register Listener
        ListenerClass listener = new ListenerClass();
        jbtNew.addActionListener(listener);


    } 
//Main Method
    public static void main(String[] args) {

        CardDisplay frame = new CardDisplay();
        frame.setSize(800, 200);
        frame.setTitle("Card Display");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    private static class ListenerClass implements ActionListener{
        public void actionPerformer(ActionEvent e) {



        }
    }
}

add(new JLabel(card1));

use varibles for labels
first declare in proper place: JLabel label1;
In constructor create new instance of label: label1 = new JLabel(card1);
add this label ...
///
In method actionPerformed your labels stays avilable to set new icons

Or keep them in a List.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.