Add delete cottage action on list
This commit is contained in:
parent
0e42491489
commit
b2563a3ed2
4 changed files with 33 additions and 7 deletions
BIN
C15-TP-JDBC_Partie2.pdf
Normal file
BIN
C15-TP-JDBC_Partie2.pdf
Normal file
Binary file not shown.
|
@ -62,4 +62,20 @@ public class Request {
|
||||||
}
|
}
|
||||||
return cottages;
|
return cottages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean deleteCottage(Cottage cottage) {
|
||||||
|
Connection c = Connexion.getConnexion();
|
||||||
|
try {
|
||||||
|
PreparedStatement ps = c.prepareStatement("DELETE FROM GITE WHERE IDGITE = ?");
|
||||||
|
ps.setInt(1, cottage.getId());
|
||||||
|
int res = ps.executeUpdate();
|
||||||
|
ps.close();
|
||||||
|
c.close();
|
||||||
|
return res == 1;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println(e);
|
||||||
|
JOptionPane.showMessageDialog(null, "Error when delete cottage !");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,13 +32,13 @@
|
||||||
<text value="Exit"/>
|
<text value="Exit"/>
|
||||||
</properties>
|
</properties>
|
||||||
</component>
|
</component>
|
||||||
<component id="51ebf" class="javax.swing.JButton" binding="actionButton" default-binding="true">
|
<component id="51ebf" class="javax.swing.JButton" binding="deleteButton">
|
||||||
<constraints>
|
<constraints>
|
||||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
</constraints>
|
</constraints>
|
||||||
<properties>
|
<properties>
|
||||||
<enabled value="false"/>
|
<enabled value="false"/>
|
||||||
<text value="Action"/>
|
<text value="Delete"/>
|
||||||
</properties>
|
</properties>
|
||||||
</component>
|
</component>
|
||||||
</children>
|
</children>
|
||||||
|
|
|
@ -5,26 +5,33 @@ import Objects.Cottage;
|
||||||
import Objects.Owner;
|
import Objects.Owner;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import javax.swing.event.ListSelectionEvent;
|
||||||
|
import javax.swing.event.ListSelectionListener;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
|
|
||||||
public class ListCottageGUI extends JDialog {
|
public class ListCottageGUI extends JDialog {
|
||||||
private JPanel contentPane;
|
private JPanel contentPane;
|
||||||
private JButton exitButton;
|
private JButton exitButton;
|
||||||
private JList cottageList;
|
private JList cottageList;
|
||||||
private JButton actionButton;
|
private JButton deleteButton;
|
||||||
|
|
||||||
public ListCottageGUI(Owner owner) {
|
public ListCottageGUI(Owner owner) {
|
||||||
setTitle("List cottage");
|
setTitle("List cottage");
|
||||||
setLocationRelativeTo(null);
|
setLocationRelativeTo(null);
|
||||||
setContentPane(contentPane);
|
setContentPane(contentPane);
|
||||||
setModal(true);
|
setModal(true);
|
||||||
getRootPane().setDefaultButton(actionButton);
|
getRootPane().setDefaultButton(deleteButton);
|
||||||
fillCottageList(owner);
|
fillCottageList(owner);
|
||||||
|
|
||||||
actionButton.addActionListener(actionEvent -> onAction());
|
deleteButton.addActionListener(actionEvent -> onDelete());
|
||||||
|
|
||||||
exitButton.addActionListener(actionEvent -> onExit());
|
exitButton.addActionListener(actionEvent -> onExit());
|
||||||
|
|
||||||
|
cottageList.addListSelectionListener(listSelectionEvent -> {
|
||||||
|
if (!deleteButton.isEnabled())
|
||||||
|
deleteButton.setEnabled(true);
|
||||||
|
});
|
||||||
|
|
||||||
// call onCancel() when cross is clicked
|
// call onCancel() when cross is clicked
|
||||||
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
||||||
addWindowListener(new WindowAdapter() {
|
addWindowListener(new WindowAdapter() {
|
||||||
|
@ -39,9 +46,12 @@ public class ListCottageGUI extends JDialog {
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onAction() {
|
private void onDelete() {
|
||||||
// add your code here
|
// add your code here
|
||||||
dispose();
|
Cottage cottage = (Cottage) cottageList.getSelectedValue();
|
||||||
|
if (JOptionPane.showConfirmDialog(null, "Do you want to remove " + cottage.getName() + " ?", "Remove a cottage", JOptionPane.YES_NO_OPTION) == 0)
|
||||||
|
if (Request.deleteCottage(cottage))
|
||||||
|
((DefaultListModel) cottageList.getModel()).remove(cottageList.getSelectedIndex());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onExit() {
|
private void onExit() {
|
||||||
|
|
Reference in a new issue