The run check does not delete the string. Help me fix the error! program code

import java.awt.EventQueue; import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; import java.util.Scanner; import java.util.Map.Entry; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JButton; import javax.swing.JFileChooser; import java.awt.TextField; import java.awt.Label; import java.awt.Font; import java.awt.event.ActionListener; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.awt.event.ActionEvent; import java.awt.Button; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; public class jfMyFrame extends JFrame { /** * */ private static final long serialVersionUID = 1L; public static HashMap<Integer,Smartphone> ListSmart = new HashMap<Integer,Smartphone>(); private JTable table; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { jfMyFrame frame = new jfMyFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } JButton button_5 = new JButton("\u0418\u0437\u043C\u0435\u043D\u0438\u0442\u044C"); //создаем стандартную модель DefaultTableModel dtm = new DefaultTableModel(); /** * Create the frame. */ public jfMyFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 549, 394); getContentPane().setLayout(null); JScrollPane scrollPane = new JScrollPane(); scrollPane.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { } }); scrollPane.setBounds(118, 66, 405, 278); getContentPane().add(scrollPane); //задаем названия столбцов dtm.setColumnIdentifiers(new String[] {"Модель", "Раз-р Экрана", "Дисплей","Об-м Флешпам."}); //наполняем модель данными table = new JTable(dtm); scrollPane.setViewportView(table); final TextField textField = new TextField(); textField.setBounds(118, 38, 62, 22); getContentPane().add(textField); Label label = new Label("Model"); label.setFont(new Font("Dialog", Font.PLAIN, 14)); label.setBounds(115, 10, 62, 22); getContentPane().add(label); Label label_1 = new Label("Razmer(diag-l)");//\u0418\u0437\u0433\u043E\u0442\u043E\u0432\u0438\u0442\u0435\u043B\u044C label_1.setFont(new Font("Dialog", Font.PLAIN, 14)); label_1.setBounds(204, 10, 110, 22); getContentPane().add(label_1); Label label_2 = new Label("Tip_displeya");//\u0427\u0430\u0441\u0442\u043E\u0442\u0430 label_2.setFont(new Font("Dialog", Font.PLAIN, 14)); label_2.setBounds(332, 10, 89, 22); getContentPane().add(label_2); Label label_3 = new Label("VFlash(GB)"); label_3.setFont(new Font("Dialog", Font.PLAIN, 14)); label_3.setBounds(440, 10, 62, 22); getContentPane().add(label_3); final TextField textField_1 = new TextField(); textField_1.setBounds(204, 38, 111, 22); getContentPane().add(textField_1); final TextField textField_2 = new TextField(); textField_2.setBounds(332, 38, 89, 22); getContentPane().add(textField_2); final TextField textField_3 = new TextField(); textField_3.setBounds(440, 38, 83, 22); getContentPane().add(textField_3); JButton btnNewButton = new JButton("\u0414\u043E\u0431\u0430\u0432\u0438\u0442\u044c"); //добавление btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(textField_1.getText().length()!=0){//must use a "final" pharameters String s1 = new String (textField.getText()); Double d2 = new Double(textField_1.getText()); String s3 = new String(textField_2.getText()); Integer d4 = new Integer(textField_3.getText()); Smartphone smrt = new Smartphone(s1,d2, s3, d4); if( ListSmart.containsKey(d4))return; ListSmart.put(d4, smrt);//4 column is key //заполнение таблицы dtm.addRow(new String[]{textField.getText(), textField_1.getText(), textField_2.getText(), textField_3.getText()}); } } }); btnNewButton.setBounds(10, 10, 96, 23); getContentPane().add(btnNewButton); //очистка таблицы Button button_1 = new Button("\u0423\u0434\u0430\u043B\u0438\u0442\u044C \u0432\u0441\u0435"); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { while(table.getRowCount()>0){ dtm.removeRow(0); } ListSmart.clear(); } }); button_1.setBounds(10,122, 89, 22); getContentPane().add(button_1); final TextField textField_4 = new TextField(); textField_4.setBounds(10, 66, 89, 22); getContentPane().add(textField_4); Label label_4 = new Label("Razmer(diag-l)"); label_4.setFont(new Font("Dialog", Font.PLAIN, 12)); label_4.setBounds(10, 50, 96, 22); getContentPane().add(label_4); //удаление строки Button button = new Button("\u0423\u0434\u0430\u043B\u0438\u0442\u044C"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(textField_4.getText().length()==0) return; for(int i = 0; i < table.getRowCount(); i++){ if(dtm.getValueAt(i, 1).equals(textField_1.getText())){ Integer in = new Integer(""+dtm.getValueAt(i, 0)); ListSmart.remove(in); dtm.removeRow(i); } } } }); button.setBounds(10, 94, 89, 22); getContentPane().add(button); } 
  • 2
    I propose to start with issuing meaningful names to all fields and variables. Now you have the key of the table when added is the value of the field textField_3 , and when you delete - the value of the zero column of the table, which is the value of textField . - zRrr

1 answer 1

The main mistake was not matching key fields: in the function code

  button.addActionListener(new ActionListener() 

line was not replaced

 Integer in = new Integer(""+dtm.getValueAt(i, 0)) 

on

  Double in = new Double(""+dtm.getValueAt(i, 1)) 

That led to an error.