Niels Van Steen
Niels Van Steen

Reputation: 308

Java dynamically create buttons and pass a parameter to action performed

I am new to java and i am trying a small project on my own, i want to list the first and lastname of users from a sql database (this all works fine, but i don't just want to list them

I want to list all users in a GUI with a delete button, naturally this delete button will be generated dynamically and i want to pass the userID of the button to the action performed. like this:

 John      Doe      'Delete button'
 Jane      Doe      'Delete button'

In my code below i just generate 16 buttons dynamically (without a users table) instead of passing the userID i am trying to pass the 'i' value of the for loop, but my code does not seem to work

CODE

    public class UsersView implements ActionListener {

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();


    public UsersView() {

        //Create the 16 buttons.
        for (int i=0; i<16; i++) {
            Button button = new Button("Click "+i);
            button.setId(i);  //this gives me and error 'Symbol not find' on the 'setId'
            panel.add(button);
            button.addActionListener(this);
        }

        panel.setBorder(BorderFactory.createBevelBorder(0, Color.lightGray, Color.yellow));
        //panel.setBorder(BorderFactory.createEmptyBorder(300, 300, 100, 300));
        panel.setLayout(new GridLayout(4,4)); //Rows Cols

        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("App GUI");
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        // TODO code application logic here
        new UsersView();
    }


    //On button click.
    @Override
    public void actionPerformed(ActionEvent e) {
        //I know i have nothing here (yet) that is because the 'setId' gives me an error.

    }

}

Upvotes: 0

Views: 1038

Answers (2)

matt
matt

Reputation: 12337

One of the issues you're having is creating a monolithic action listener, and then expecting to delegate actions from that. One nice feature of java are anonymous classes, or since java 7 lambdas.

JButton button = new JButton("Click " + i);
panel.add(button);
int id = i;
button.addActionListener( evt->{
    performActionOnId( id );
});

Now instead of having the main class be an action listener, the main class has methods that are descriptive.

public void addUser( User a ){
    //just showing delete button.

    JButton delete = new JButton("X");
    delete.addActionListener( evt ->{
        removeUser( a );
        //clean up gui.
    });
}

This puts some of the steps of delegation at the creation of the user. Otherwise you'll have to delegate in your action performed.

public void actionPerformed( ActionEvent evt ){
    //has to be a new class to have id attribute.
    CustomButton b = (CustomButton)evt.getSource();
    int id = b.getId();
    User u = getUserById(id);
    removeUser(u);
}

Upvotes: 1

pafau k.
pafau k.

Reputation: 1687

Use a JButton instead of Button. Mixing AWT and Swing components rarely works well, if at all. If you want to set a custom field for a component, just subclass it (use direct subclassing or a decorator pattern with composition). E.g.:

public class IdButton extends JButton {
    private int id;

    public IdButton(String label, int id) {
        super(label);
        this id = id;
    }

    public int getId() {
        return id;
    }
}

The J/Button classes do not have set/getId methods on it's own.

Upvotes: 0

Related Questions