0

So, we can create for example a button dynamically:

panel.add(new JButton("Button"));
validate();

But the question is, how do we make calls to those elements later? For example, how do I add an event listener to this button created above, like, 100 lines of code later?

2
  • 3
    You would need a variable for the button, just like you have for panel. Commented Sep 13, 2017 at 20:57
  • Store the JButton away somewhere (e.g. field variable) to access it later Commented Sep 13, 2017 at 20:58

3 Answers 3

1

Create a variable for your JButton:

JButton jButton = new JButton("Button");
panel.add(jButton);
validate();
/*
 *
 *
100 lines of code 
 *
 */

// add an event listener
jButton.addActionListener((ActionEvent) -> {
            // do something
        });
Sign up to request clarification or add additional context in comments.

Comments

1

I've always created my buttons before adding to the panel like so

   private JPanel buttonPanel() { //button panel method containting 
        JPanel panel = new JPanel();
        JButton addButton = new JButton("Add");
        addButton.setToolTipText("Add Customer Data");
        JButton editButton = new JButton("Edit");
        editButton.setToolTipText("Edit selected Customer");
        JButton deleteButton = new JButton ("Delete");
        deleteButton.setToolTipText("Delete selected Customer");

        addButton.addActionListener((ActionEvent) -> {
            doAddButton();
        });
        editButton.addActionListener((ActionEvent) -> {
            doEditButton();
        });
        deleteButton.addActionListener((ActionEvent) -> {
            doDeleteButton();
        });

        panel.add(addButton);
        panel.add(editButton);
        panel.add(deleteButton);

        return panel;
    }

Allows you do something like this later on.

    private void doAddButton() { //provides action for add button
    CustomerForm customerForm = new CustomerForm(this, "Add Customer", true);
    customerForm.setLocationRelativeTo(this);
    customerForm.setVisible(true);
}

2 Comments

Ok, the problem is, I don't know how many rows will be returned from the database query. Say, 23 results come from the query which means I need 23 buttons. Can I use array with size of 23 to store 23 separate buttons?
Arraylist maybe the way to go in that case. I'm not all that familiar with setting an array for to create buttons with each increment though. Sorry.
0

In order to bind event listeners or other functionality into the button, you'd need to store a reference to it in a variable. So, instead of

panel.add(new JButton("Button"));

You could initialize the button with

JButton myButton = new JButton("Button");
panel.add(myButton);

and then later in your code

myButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
         // do something
     } 
});

1 Comment

If you use an AbstractAction, then the first would be perfectly fine: panel.add(new JButton(new ButtonAction()));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.