0

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.

    }

}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Niels Van Steen
  • 188
  • 1
  • 17
  • What is your Button? Why should it have a `setId` method? – matt May 29 '20 at 19:20
  • Currently my button is just doing nothing but later when the button is clicked i want to get the ID of a user (currently the int i of the for loop) and delete the record. I thought of passing the value in the setId() and then on the action performed doing getId(). – Niels Van Steen May 29 '20 at 19:24
  • This either looks like a job for a `JTable`, for [example](https://stackoverflow.com/questions/10347983/making-a-jbutton-clickable-inside-a-jtable). And you can get the button that triggered the event by usign `e.getSource()`, see [How to use Buttons](https://docs.oracle.com/javase/tutorial/uiswing/components/button.html) tutorial, for [example](https://stackoverflow.com/questions/42935966/press-buttons-one-by-one/42936848#42936848) – Frakcool May 29 '20 at 19:28
  • 1
    Does this answer your question? [Is there a way to store data in a JButton?](https://stackoverflow.com/questions/21533356/is-there-a-way-to-store-data-in-a-jbutton) – Progman May 29 '20 at 20:28
  • Are you using Swing or JavaFX? For Swing, you'd need a JButton; for AWT (an older framework) a Button, and only the Button has the setId() method. You probably shouldn't be putting a Button into a JPanel. – NomadMaker May 31 '20 at 07:54

2 Answers2

1

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);
}
matt
  • 10,892
  • 3
  • 22
  • 34
0

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.

pafau k.
  • 1,667
  • 12
  • 20