From 41740249cacc387346b9db16cf40086657028dd7 Mon Sep 17 00:00:00 2001 From: mudabbir-ahmad Date: Wed, 2 Apr 2025 15:28:49 +0100 Subject: [PATCH] Added edit and delete buttons, added dialogue box for editing attributes, added a separater to add a footer to the panel which is to add a new user. --- src/main/java/cvbuilder/view/MainViewer.java | 71 +++++++++++++------- 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/src/main/java/cvbuilder/view/MainViewer.java b/src/main/java/cvbuilder/view/MainViewer.java index 3313189..a878121 100644 --- a/src/main/java/cvbuilder/view/MainViewer.java +++ b/src/main/java/cvbuilder/view/MainViewer.java @@ -4,6 +4,7 @@ import cvbuilder.model.User; import cvbuilder.model.UserGroup; import javax.swing.*; +import java.awt.*; public class MainViewer { @@ -101,30 +102,19 @@ public class MainViewer { JButton editButton = new JButton("Edit"); final int userIndex = i; editButton.addActionListener(e -> { - JTextField textField = new JTextField(value, 20); - JPanel dialogPanel = new JPanel(); - dialogPanel.add(new JLabel("Edit:")); - dialogPanel.add(textField); - - int result = JOptionPane.showConfirmDialog(panel, dialogPanel, - "Edit " + panelName, JOptionPane.OK_CANCEL_OPTION, - JOptionPane.PLAIN_MESSAGE); - - if (result == JOptionPane.OK_OPTION) { - String newValue = textField.getText(); - if (!newValue.isEmpty()) { - // Update the user attribute - switch (panelName.toLowerCase()) { - case "title" -> user.setTitle(newValue); - case "name" -> user.setName(newValue); - case "email" -> user.setEmail(newValue); - case "phone" -> user.setPhone(newValue); - case "address" -> user.setAddress(newValue); - } - - // Update the radio button label - radioButton.setText(newValue); + String newValue = showDialog("Edit " + panelName, value); + if (newValue != null && !newValue.isEmpty()) { + // Update the user attribute + switch (panelName.toLowerCase()) { + case "title" -> user.setTitle(newValue); + case "name" -> user.setName(newValue); + case "email" -> user.setEmail(newValue); + case "phone" -> user.setPhone(newValue); + case "address" -> user.setAddress(newValue); } + + // Update the radio button label + radioButton.setText(newValue); } }); entryPanel.add(editButton); @@ -169,6 +159,7 @@ public class MainViewer { // Add the "Add Row" interface at the bottom JPanel addRowPanel = new JPanel(); addRowPanel.setLayout(new BoxLayout(addRowPanel, BoxLayout.X_AXIS)); + addRowPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 80)); JTextField newValueField = new JTextField(15); JButton addButton = new JButton("Add Row"); @@ -182,6 +173,7 @@ public class MainViewer { addButton.addActionListener(e -> { String newValue = newValueField.getText().trim(); if (!newValue.isEmpty()) { + // Create a new user User newUser = new User(); @@ -225,4 +217,37 @@ public class MainViewer { // Add vertical glue to push all components to the top panel.add(Box.createVerticalGlue()); } + + private String showDialog(String title, String initialValue) { + JTextField textField = new JTextField(initialValue); + JLabel label = new JLabel("Enter new text:"); + + JPanel panel = new JPanel(); + panel.setLayout(new GridLayout(0, 1)); + + panel.add(label); + panel.add(textField); + + JOptionPane pane = new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION); + JDialog dialog = pane.createDialog("Rename Dialogue Box"); + dialog.setVisible(true); + + // Process the user's choice + Object value = pane.getValue(); + if (value instanceof Integer) { + int choice = (Integer) value; + + if (choice == JOptionPane.OK_OPTION) { + String newValue = textField.getText().trim(); + System.out.println("OK option pressed and set text to: " + newValue + " for tab: " + title); + return newValue; + } else if (choice == JOptionPane.CANCEL_OPTION) { + System.out.println("Cancel option pressed for tab: " + title); + } + } + + dialog.dispose(); + return null; + } + } \ No newline at end of file