import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MenuActionAddRow implements ActionListener { private DataTableModel tableModel; private boolean askForPosition; public MenuActionAddRow(DataTableModel tableModel, boolean askForPosition) { this.tableModel = tableModel; // If set to true user will be prompted to input row name this.askForPosition = askForPosition; } public void actionPerformed(ActionEvent e) { System.out.println("Create Row"); // Default parameters // Direction: If true - append north, else append south boolean direction = false; int row = -1; if (askForPosition) { Object[] options = {"Добавить на север", "Добавить на юг", "Отмена"}; JPanel panel = new JPanel(); panel.add(new JLabel("Введите номер строки который должен быть добавлен")); JTextField textField = new JTextField(10); panel.add(textField); // Keep asking for number until valid response is received while (row == -1) { int result = JOptionPane.showOptionDialog( null, panel, "Введите номер строки", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, null ); // Canceled by user if (result == JOptionPane.CLOSED_OPTION || result == 2) { return; } // Process user response boolean inputDirection = result == 0; IndexValidator indexValidator = new IndexValidator(tableModel); int inputRow = indexValidator.validateRowIndex(textField.getText()); // If an error was encountered if (inputRow == -1) { continue; } // This point is reached only upon receival of valid number direction = inputDirection; row = inputRow; } } else { // If askForPosition is false, then append row to the end row = tableModel.getRowCount(); } // If direction is north, the actual index is the preceeding one if (direction) { row--; } tableModel.addRow(row); } }