Java change the image in a JPanel when a button is pressed

I am well aware of the several other posts of similar nature. I’ve been trying to use them to help me, however either I am simply overlooking something or my code is missing something essential to properly work. I have a main JPanel that I am trying to have the image change every time a button is pressed (and I am starting to thing that my plan to change the layout when a different button is pressed will be a lot harder), but when it comes to the @Override section, I find that the icon cannot be changed, or when I try to implement something that I find from stackOverflow or from suggested in VSCode, it leads to lots of red lines.

What I am Wanting is to have an array of images available so that when the button is pressed, it runs through a random number generator and outputs a number that grabs a picture from the array at random. Fret not, I am well educated in the random number generation department, I hope.

and yes, I am aware this is a hot mess. I’ve tried several times over the past couple of weeks to figure this out (started with only a couple hours a day to stay sane, ending with non-stop 72 hours sessions, waking from sleep with different ideas to try)

Here are some sections of my code, I am including that which I think is necessary as I have written a lot and navigating is a nightmare.

`private JPanel topRow, leftPane, mainPane, bottomRow, mPane1;
private JButton btnOne, btnTen, btnHun, btnTho;
private JTextArea mPane2, test;
private String imagePath, name;
private ImageIcon image;

public  WSgui() {
super("Character Wish Simulator");
    this.setSize(900, 600);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new BorderLayout());

this.mainPane = new JPanel();
    this.mainPane.setBackground(Color.ORANGE);
    this.mainPane.setMinimumSize(new Dimension(650, 400));
    this.mainPane.setBorder(new EmptyBorder(40,40,40,40));
    this.mainPane.setLayout(new BorderLayout());


//buttons
    this.btnOne = new JButton("x1 wish");
    this.btnOne.setSize(200, 100);
    
//setting panes for main
    this.mPane1 = new JPanel();
    int xSize = ((int) mainPane.getSize().getWidth());  
    int ySize = ((int) mainPane.getSize().getHeight());  
    int height = (int)(Math.round(ySize * 0.80));
    int width = (int)(Math.round(xSize * 0.80));
    this.mPane1.setPreferredSize(new Dimension(width, height));
    this.mPane1.setMinimumSize(new Dimension(xSize, ySize));
    this.mPane1.setMaximumSize(new Dimension(1000, 1000));

    this.mPane2 = new JTextArea();
    this.mPane2.setText("This should be below the picture");
    this.mPane2.setFont(new Font("Dialog", Font.PLAIN, 15));
    this.mPane2.setMaximumSize(new Dimension(width, 50));
    this.mPane2.setWrapStyleWord(true);
    this.mPane2.setLineWrap(true);
    this.mPane2.setEditable(false);



    this.btnTen = new JButton("x10 wish");
    this.btnTen.setSize(200, 50);
    this.btnTen.addActionListener(this);
//This idea (action listener) came from StackOverflow, but sadly is not working

    this.btnHun = new JButton("x100 wish");
    this.btnHun.setSize(200, 50);
    this.btnHun.addActionListener(this);

    this.btnTho = new JButton("x1000 wish");
    this.btnTho.setSize(200, 50);
    this.btnTho.addActionListener(this);

//Test image for Main Pane
    image = new ImageIcon(bnr);//some more ideas I found
    JLabel image5 = new JLabel(image);
    JLabel image2 = new JLabel(new ImageIcon(lpImage));
    JLabel image3 = new JLabel(new ImageIcon(mImage));
    JLabel image4 = new JLabel(new ImageIcon(xtra));
    this.mPane1.add(image3);
    this.leftPane.add(image2);
    this.topRow.add(image5);
    this.bottomRow.add(image4);

    this.mainPane.add(this.mPane1, BorderLayout.CENTER);
    this.mainPane.add(this.mPane2, BorderLayout.SOUTH);

    this.btnOne.addActionListener(e -> image5.setIcon(images[3]));

ImageIcon[] images = new ImageIcon[] {//my pathetic attempt at an array
    new ImageIcon("img/t1.png"), new ImageIcon("img/t2.png"), new ImageIcon("img/t3.jpg"),
    new ImageIcon("img/t4.jpg"), new ImageIcon("img/t5.png")
};

String[] image8 = new String[] {
    "img/t1.png"//idk how to explain this one
};







@Override
public void actionPerformed(ActionEvent e) {
    
    if(e.getSource() == btnOne) {
        String image9 = image8[0];//and this is what I struggle with
        ImageIcon icon9 = new ImageIcon(image9);
        icon9.getImage().flush();
        image.setIcon(icon9);
        
    }
    }
    
  

If you need me, I may be occupied with this code. It’s been alone long enough. I must tend to it again.

New contributor

William neko Elizondo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật