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

  • SEARCH
  • Home
  • 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 8478133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:39:39+00:00 2026-06-10T18:39:39+00:00

I’m a newbie in Java/JavaFX (I began yesterday evening). I’m building a dynamic GUI

  • 0

I’m a newbie in Java/JavaFX (I began yesterday evening). I’m building a dynamic GUI (crud) reading off a MySQL database.

  • I managed to display the data in a table and add a button next to each row.
  • Since the number of buttons is variable, I want to define only a common eventhandler.

The problem is that whenever I use event.getSource() (it’s an ActionEvent) and display it, I get something like “Button[id=0, styleClass=button].

Question 1: Is there any way I could put the id in a variable? I can’t get it out of the object.

As far as I know, I have to use the id, since I can’t do something like this “if(event.getSource() == somebutton) {…}” since every generated button had the same variable name.

Now, this is the loop (inside a method called make_buttons) that builds the buttons. n_buttons is the number of buttons I want to build.

for(int counter = 0; counter < n_buttons; counter++){
        String newtext = new String("btn"+counter);
        Button btn = new Button();
        btn.setText(newtext);
        btn.setId(Integer.toString(counter));
        btn.setOnAction(myHandler);
        grid.add(btn,0,counter);
    }

Note that I’m placing the buttons on a gridpane one on top of the other.

Before that part I have my handler:

    final EventHandler<ActionEvent> myHandler = new EventHandler<ActionEvent>(){

    public void handle(final ActionEvent event) {
        Object new_output = event.getSource();
        System.out.println(new_output);
        event.consume();
        }
    };

Question 2: so, how can I differentiate which button fired the event in my particular case?

I know quite a few programming languages (Matlab, R, Python, C, Assembly, etc… but I’m a hobbyist), but it’s the first time I’m working with GUI elements (except web languages and ActionScript 3).
In actionscript I could just do something like event.getCurrentTarget and the use it exactly as if it were the object itself to read the id, properties, etc.

I looked everywhere and couldn’t find anything (maybe my terminology was a bit approximative…).

  • 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-10T18:39:40+00:00Added an answer on June 10, 2026 at 6:39 pm

    If I understand your question correcty, you can simply access the clicked button in you handle method with the following code:

    Object source = event.getSource();
    if (source instanceof Button) { //should always be true in your example
        Button clickedBtn = (Button) source; // that's the button that was clicked
        System.out.println(clickedBtn.getId()); // prints the id of the button
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a reasonable size flat file database of text documents mostly saved in
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.