https://pastein.ru/t/B-

  скопируйте уникальную ссылку для отправки


public Tabs(String namePanelBoxes){
		super();
		this.publicPanel= new JPanel();
		this.panelBoxes = new JPanel();
		this.panelControl = new JPanel();
		this.panelField = new JPanel();
		this.panelButton = new JPanel();
		this.addBox = new MultiLineButton().createTwoLineButton("Добавить", "позицию");
		this.deleteCheckBox = new MultiLineButton().createTwoLineButton("Удалить", "выбранные");
		this.deleteAll = new JButton("Удалить все");
		this.addArchive = new MultiLineButton().createThreeLineButton("Добавить", "выбранные", "в архив");
		this.addAllArchive = new MultiLineButton().createTwoLineButton("Добавить все", "в архив");
		this.currentLabel = new JLabel("Текущий пробег:");
		this.currentField = new JTextField(10);
		UIManager.put(                                       
                "FileChooser.saveButtonText", "Сохранить");
       UIManager.put(
                "FileChooser.cancelButtonText", "Отмена");
       UIManager.put(
                "FileChooser.fileNameLabelText", "Наименование файла");
       UIManager.put(
                "FileChooser.filesOfTypeLabelText", "Типы файлов");
       UIManager.put(
                "FileChooser.lookInLabelText", "Директория");
       UIManager.put(
                "FileChooser.saveInLabelText", "Сохранить в директории");
       UIManager.put(
                "FileChooser.folderNameLabelText", "Путь директории");
		this.fileChoose = new JFileChooser();
		FileNameExtensionFilter filter = new FileNameExtensionFilter("Файл Блокнот (.txt)", "txt");
		fileChoose.removeChoosableFileFilter(fileChoose.getFileFilter());
		fileChoose.setFileFilter(filter);
	//преференс панелей	
		publicPanel.setName(namePanelBoxes);
		publicPanel.setLayout(null); 
		panelControl.setLayout(null); //параметры flowlayout (выравнивание,зазор меж компонентами по вертикали, по горизонтали)
		panelControl.setName("panelControl");
		panelBoxes.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
		panelField.setLayout(new BoxLayout(panelField,BoxLayout.Y_AXIS));
		panelField.setName("panelField");
		panelButton.setLayout(new GridLayout(5,1,0,12));
		publicPanel.setBorder(border);
		panelBoxes.setBorder(border);
		panelControl.setBounds(4, 4, 180, 500);
		panelBoxes.setBounds(190, 7, Interface.frame.getWidth()-217, Interface.frame.getHeight()-138);
		panelField.setBounds(5, 20, 172, 40);
		panelButton.setBounds(30, 70, 120, 312);

	//коннект компонентов с классами событий	
		addBox.addActionListener(new AddBox());
		deleteCheckBox.addActionListener(new DeleteCheckBox());
		currentField.addKeyListener(new MainFieldKeyListener());
		deleteAll.addActionListener(new DeleteAll());
		addArchive.addActionListener(new AddChangeArchive());
		addAllArchive.addActionListener(new AddAllArchive());
	//преференс компонентов
		currentField.setName("currentField");
	//добавление компонентов на панели	
		panelField.add(currentLabel);
		panelField.add(currentField);
		panelButton.add(addBox);
		panelButton.add(deleteCheckBox);
		panelButton.add(deleteAll);
		panelButton.add(addArchive);
		panelButton.add(addAllArchive);
		panelControl.add(panelField);
		panelControl.add(panelButton);
		publicPanel.add(panelControl);
		publicPanel.add(panelBoxes);
		
	}
	public JPanel getPublicPanel(){
		return publicPanel;
	}
	public JPanel getPanelBoxes(){
		return panelBoxes;
	}
	public ArrayList<Boxes> getListBoxes(){
		return listBox;
	}
	public void setCurrenField(int dist){
		currentField.setText(Integer.toString(dist));
	}
	public int getCurrentDistance(){
		String text = currentField.getText();
		if (text.isEmpty()){return 0;}
		else {return Integer.parseInt(text);}
	}

	private class AddBox implements ActionListener,Serializable{

		@Override
		public void actionPerformed(ActionEvent e) {
			String[] options = {"Добавить"};
			
			JPanel panel = new JPanel();
			JLabel label = new JLabel("Введите наименованиие работы: ");
			JTextField field = new JTextField(25);
			panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
			panel.add(label);
			panel.add(field);
			
			int selectedOption = JOptionPane.showOptionDialog(null, panel, "Добавление новой позиции", JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, null, options , options[0]);
			if(selectedOption == 0)
			{
			    String nameBox = field.getText();
			    Boxes newBox = new Boxes(nameBox);
			    Box box = newBox.getBox();
			    panelBoxes.add(box);
			    listBox.add(newBox);
			    panelBoxes.revalidate();       //обнавляет отображение компонентов в контейнере 	    
			}	
		}	
	}	
	private class DeleteCheckBox implements ActionListener,Serializable{
    //событие кнопки удаления
		@Override
		public void actionPerformed(ActionEvent e) {  //удаляет выбранные позиции
			
			for (int i=0;i<panelBoxes.getComponents().length;i++){
				Component[] arr = panelBoxes.getComponents();
				String namebox = arr[i].getName();
				
				for (int j=0;j<listBox.size();j++){
					Boxes box = listBox.get(j);
					String nameobj = box.getNameBox();
					
					if (namebox.equals(nameobj)&&box.getIsSelectedCheckBox()){
						arr[i].setVisible(false);
						panelBoxes.remove(i);
						listBox.remove(j);
						i--;
						break;
					}
				}
			}
		}
	}
	private class DeleteAll implements ActionListener,Serializable{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			Component[] arr = panelBoxes.getComponents();
			for (Component c:arr){
				c.setVisible(false);
			}
			panelBoxes.removeAll();
			listBox.clear();
		}	
	}
	private class MainFieldKeyListener extends KeyAdapter implements Serializable{
		
		public void keyReleased(KeyEvent event){
			if (event.getKeyCode() != KeyEvent.VK_BACK_SPACE){ //если нажимается кнопка Backspace код снизу не выполняется
				String line = currentField.getText();
				try{
					int dist = Integer.parseInt(line);
					for (Boxes box:listBox){
						int resource = box.getValueFieldResource();
						int lastReplace = box.getValueFieldLastReplace();
						box.setValueFieldLeft(lastReplace+resource-dist);}
					}
				catch (NumberFormatException exc){
					JOptionPane.showMessageDialog(null, "Введите в поле \"Текущий пробег\" число! Либо число больше 2147483647");
					}
				}
			}
	}
	private class AddChangeArchive implements ActionListener, Serializable {

		@Override
		public void actionPerformed(ActionEvent evenet) {
			String[] options = {"Сохранить"};
			
			JPanel panel = new JPanel();
			JLabel l = new JLabel("Введите дату : ");
			JTextField dataField = new JTextField(2);
			panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
			panel.add(l); panel.add(dataField);
			
			Date nowDate = new Date();
			SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
			dataField.setText(format.format(nowDate));
			
			int selectedOption = JOptionPane.showOptionDialog(null, panel, "Добавление в архив выбранных", JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, null, options , options[0]);
			if (selectedOption==0){
				
				int saveWindow = fileChoose.showSaveDialog(null);
				File file = null;			
				if (saveWindow == JFileChooser.APPROVE_OPTION){
					String nameFile = fileChoose.getSelectedFile().getName();
					if (!nameFile.contains(".txt")){
						file = new File(fileChoose.getCurrentDirectory()+"\\"+nameFile+".txt");
					}
					else {file = fileChoose.getSelectedFile();}
				
				try {
					PrintStream writer = new PrintStream (new FileOutputStream(file,true));
					for (Boxes box:listBox){
						if (box.getIsSelectedCheckBox()){
							String data = dataField.getText();
							String nameTab = publicPanel.getName()+":";
							String nameBox = box.getNameBox();
							String fieldRes = box.getTextFieldRes();
							String fieldLast = box.getTextFieldLast();
							String fieldLeft = "("+box.getTextFieldLeft()+")";
							String lineSave = String.format("%-11s  %-15s %-30s Ресурс-%-12s Пробег последней замены-%-12s Осталось до замены-%-12s",data,nameTab,nameBox,fieldRes,fieldLast,fieldLeft);
							writer.println(lineSave);	
						}
					}
					writer.close();
					JOptionPane.showMessageDialog(null, "Данные в файл добавлены");
				} catch (IOException e) {
				}
				}
				
			}
		}
		
	}
	private class AddAllArchive implements ActionListener, Serializable {

		@Override
		public void actionPerformed(ActionEvent evenet) {
			String[] options = {"Сохранить"};
			
			JPanel panel = new JPanel();
			JLabel l = new JLabel("Введите дату : ");
			JTextField dataField = new JTextField(2);
			panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
			panel.add(l); panel.add(dataField);
			
			Date nowDate = new Date();
			SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
			dataField.setText(format.format(nowDate));
			
			int selectedOption = JOptionPane.showOptionDialog(null, panel, "Добавление в архив выбранных", JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, null, options , options[0]);
			if (selectedOption==0){
				
				int saveWindow = fileChoose.showSaveDialog(null);
				File file = null;			
				if (saveWindow == JFileChooser.APPROVE_OPTION){
					String nameFile = fileChoose.getSelectedFile().getName();
					if (!nameFile.contains(".txt")){
						file = new File(fileChoose.getCurrentDirectory()+"\\"+nameFile+".txt");
					}
					else {file = fileChoose.getSelectedFile();}
				
				try {
					PrintStream writer = new PrintStream (new FileOutputStream(file,true));
					for (Boxes box:listBox){
						String data = dataField.getText();
						String nameTab = publicPanel.getName()+":";
						String nameBox = box.getNameBox();
						String fieldRes = box.getTextFieldRes();
						String fieldLast = box.getTextFieldLast();
						String fieldLeft = "("+box.getTextFieldLeft()+")";
						String lineSave = String.format("%-11s  %-15s %-30s Ресурс-%-12s Пробег последней замены-%-12s Осталось до замены-%-12s",data,nameTab,nameBox,fieldRes,fieldLast,fieldLeft);
						writer.println(lineSave);	
					}
					writer.close();
					JOptionPane.showMessageDialog(null, "Данные в файл добавлены");
				} catch (IOException e) {
				}
				}
				
			}
		}
		
	}
	
}