-1

I'm doing a Swing app in Java with a database. I got different JButton which delete, add or modified row in a JTable.

I'm stuck because I don't know where I can manage my action listener.

I have a class named DaoClef where I will execute an SQL statement, send my table model etc...

I got a view where all my button are set but I don't know what to do in my controller.

For now I got this in the view:

    btnNew.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // check for selected row first
            if (t.getSelectedRow() != -1) {
                // remove selected row from the model

                String sqlMaj = "DELETE FROM clefs WHERE IdClef = ?";
                try (PreparedStatement pst = Connect.getConnection().prepareStatement(sqlMaj) )
                {

                    pst.setInt(1, (int) t.getValueAt(t.getSelectedRow(), 0));   
                    pst.executeUpdate();
                    t.addNotify();
                } 
                catch (SQLException e)
                {
                }
            }
        }
    });

But it's not good because the SQL statement need to be execute in a method in the controller.

Can you give me some clues?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • To be more clear i want to catch the action performed in my controller which come from a JButton in my view ... – Pierre-max Halldegare Dec 26 '18 at 19:16
  • 1
    1) `catch (SQLException e) { }` Don't ignore exceptions! They inform us exactly what went wrong. Unless logging is implemented, at least call `Throwable.printStackTrace()` 2) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Dec 26 '18 at 22:40
  • Your `ActionListener` is one of the controllers. It can modify the model and update the view. Your SQL should be separated from your GUI code. – Gilbert Le Blanc Feb 24 '23 at 23:14

1 Answers1

-1

From an MVC perspective, the issue here is that the Model and the View are mixed together. Since the Controller is supposed to act as an intermediary between the two, it has no role here.

The View, presenting the GUI (windows, buttons, text fields, etc.) to the user, should (ideally, more on this later) tell the Controller when a delete button has been pressed and its corresponding id. Then the Controller should pass this info to the Model which, in turn, will carry out the communication with the database using its connection and prepared statements.


Here's a good explanation on the MVC pattern:

  1. Model-View-Controller: Does the user interact with the View or with the Controller?

Note (or maybe omit) that there's some controversy about different takes on the MVC pattern as shown in the different answers here.

There is also a good answer on the topic of MVC in Swing here:

  1. Java and GUI - Where do ActionListeners belong according to MVC pattern?

Although the example given may be a little complex as the author tries to more strictly separate View and Controller. I'd go on and read the comments as well since they add further clarification.

I tried finding a simpler example but most didn't include action listeners. Here's a couple that do:

  1. a) https://gist.github.com/Sammy30/7ebc606e7bb76cefac0f

    b) http://www.fredosaurus.com/notes-java/GUI/structure/40mvc.html

The action listeners get added from the Controller unlike the first example linked. It's simpler but has the disadvantage of having Controller and View intertwined, which doesn't allow switching the latter easily (i.e., without modifying the Controller), which is one of the advantages of MVC.


The steps would be:

Preparation:

  1. The View and Model get created (the Model may set its connection to the database now)
  2. The Controller gets created with instances of the View and Model as properties
  3. The Controller gets ready by setting action listeners in the View (or tells the View to set them, in which case the View should contact the Controller whenever an event happens)

Usage:

  1. The user presses a button in the View
  2. The Controller notices the button press through its action listener (or the View tells the Controller which button has been pressed)
  3. The Controller decides what to do with the event. In this case, tell the Model that a record needs to be deleted
  4. The Model receives the order and modifies the database, and returns a value with the updated rows count
  5. The Model closes the ResultSet, Statement, and Connection
DystD
  • 99
  • 4