Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8611863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:28:50+00:00 2026-06-12T04:28:50+00:00

I have a loop, in which several JLabel’s are created, containing a link to

  • 0

I have a loop, in which several JLabel’s are created, containing a link to an image.

For each JLabel, there is a JButton created.

The behaviour I’m looking for is that for each JButton, to add a click listener, which fires a method.

The first issue is that it seems as though I am unable to add a Listener within the thread.

The second issue is that I don’t see how I can specify each JButton to fire the click listener with a different value.

I’m looking for something similar to the following (obviously onClick method doesn’t exist):

Thread thread = new Thread(new Runnable()
{
    public void run()
    {
        JPanel mainPanel = new JPanel();
        for (int counter = 0; counter < imageSources.size(); counter++)
        {
            JLabel imageSource = imageSources.get(counter);
            // JButton saveToDisk = new JButton("Save Image");
            // saveToDisk.onClick(saveFavourite(imageSources.get(counter)));
            mainPanel.add(imageSource);
            // mainPanel.add(saveToDisk);
        }

.
.
.

public void saveFavourite(String imageUrl)
{
    BufferedImage image = null;
    try
    {
        URL url = new URL(imageUrl);
        image = ImageIO.read(url);
        ImageIO.write(image, "jpg", new File("/Users/USERNAME/Desktop/" + webPage.getMemeId(imageUrl) + ".jpg"));
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:28:51+00:00Added an answer on June 12, 2026 at 4:28 am

    This is a simple example of creating buttons and adding to them ActionListeners that take String arguments during a loop

    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Test extends JFrame {
    
        private final JLabel[] label = new JLabel[5];
        private final JButton[] button = new JButton[5];
    
        public Test() {
            JPanel mainPanel = new JPanel(new GridLayout(0,2));
            for (int counter = 0; counter < label.length; counter++) {
                label[counter] = new JLabel("Label " + counter);
                button[counter] = new JButton("Button " + counter);
                button[counter].addActionListener(new MyActionListener("Row " + counter));
                mainPanel.add(label[counter]);
                mainPanel.add(button[counter]);
            }
            this.add(mainPanel);
        }
    
        public static void main(String args[]) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    Test run = new Test();
                    run.pack();
                    run.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    run.setVisible(true);
                }
            });
        }
    
        private class MyActionListener implements ActionListener {
    
            String text;
    
            public MyActionListener(String text) {
                this.text = text;
            }
    
            public void actionPerformed(ActionEvent e) {
                System.out.println(text);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a loop MC which will be duplicate to stage several times according
i have a php loop which displays only one record even if there is
I have the following loop which is giving me problems $(#divResults).append('<table>'); $.each( results.d, function(
In VBA on Excel, I have a loop over several thousands of cells, which
I have a parent folder called myfiles which contains several children folders. Each of
I have to loop through several of these eachLocation Div which holds various places
I'm using an e-commerce system which allows you to have several variations for each
I have a while loop in Main() which goes through several methods. Although one
I have several ArrayLists which work as queues of data. Each of the queues
I have a for loop in which I placed several if statements. The objective

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.