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 8437329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:24:02+00:00 2026-06-10T07:24:02+00:00

I’m writing a user’s GUI and I want to create a method that will

  • 0

I’m writing a user’s GUI and I want to create a method that will be create various number of buttons with previously defined names and actions. But I have no idea how to write method selection which is based on variable value. Google gives no useful information about it. Can anyone help with this or it’s just impossible?

Here is some code example:

    String[] actions={"testAction1","testAction2","testAction3"};
    defaultDialogWindow(actions,"test1", "test2", "test3");

    void defaultDialogWindow(String[] actions, String... bNames){
          double layoutX = 25;
          double spacing = 15;
          final Stage dialogStage = new Stage();
          dialogStage.initOwner(stage);
          dialogStage.initModality(Modality.WINDOW_MODAL);   
          dialogStage.setFullScreen(false);
          dialogStage.setResizable(false);
          dialogStage.setHeight(100);
          dialogStage.setWidth(bNames.length*100+(bNames.length-1)*spacing+2*layoutX+5);
          dialogStage.setScene(new Scene(buttonBuilder(actions,spacing,layoutX,bNames)));
          dialogStage.show();  
    }

    HBox buttonBuilder(String[] actions, double spacing,double layoutX,String... bNames){
          HBox lBar = new HBox(10);
          final ReadOnlyDoubleProperty menuWidthProperty = lBar.widthProperty();
          lBar.setAlignment(Pos.CENTER_LEFT);
          lBar.setLayoutX(layoutX);
          lBar.setSpacing(spacing);
          for(String text : bNames){
              Button newButton = new Button();
              newButton.setText(text);
              newButton.setFont(Font.font("Times New Roman", 22));
              newButton.prefWidthProperty().set(100);
              newButton.prefHeightProperty().set(50);
              newButton.setOnAction(new EventHandler<ActionEvent>() {
                  @Override
                  public void handle(ActionEvent paramT) {

                      **HERE MUST BE ACTION CALL BASED ON bNames VALUE**

                      System.out.println("button pressed");
                  }
              });
              lBar.getChildren().add(newButton);
          }
          System.out.println(lBar.prefWidth(-1));
          return lBar;
      }

      void testAction1(){
          System.out.println("this is test action one");
      }

      void testAction2(){
          System.out.println("this is test action two");
      }

      void testAction3(){
          System.out.println("this is test action three");
      }**strong text**
  • 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-10T07:24:03+00:00Added an answer on June 10, 2026 at 7:24 am

    Use HashMap:

    Map<String, Runnable> actions2methods = new HashMap<>;
    actions2methods.put("Action1", new Runnable { public void run() { testAction1(); }));
    

    You can even make this easier if your actions would be Runnable instead of methods from the start.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.