

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.*;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;


public class insertion extends JFrame implements ActionListener {
        
    private JTextField [] textfields=new JTextField[7];
    JPanel resultPanel=new JPanel(new GridLayout());
    JButton b2 = new JButton("sort");
    
    public insertion() {
        this.setLocation(200,200);
        this.setTitle("homwork # 1");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setSize(400,300);
        
        setLayout(new BorderLayout());
      // setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        JPanel textFieldPanel = new JPanel(new GridLayout(1, 5, 5, 5));
        for (int i = 0; i < textfields.length; i++) {
                JTextField field=new JTextField(3);
                field.setText((int)(Math.random() * 10)+"");
                textfields[i]=field;
                textFieldPanel.add(field);
                }
        textFieldPanel.setBorder(new TitledBorder(" "));
 
       textFieldPanel.add(b2);
         b2.addActionListener(this);
        
        add(textFieldPanel, BorderLayout.NORTH);
                this.pack();
    }

    private void sortAction(){
        resultPanel.removeAll();
        GridLayout g=(GridLayout)resultPanel.getLayout();
            g.setRows(1);
        insertionSort ();
        resultPanel.updateUI();
        add(resultPanel, BorderLayout.CENTER);
                this.pack();
    }
                
       
 public void actionPerformed(ActionEvent e) {
     if (e.getActionCommand() == "sort"){
        sortAction();

     }}            
    public static void main (String[ ] args){   
                 insertion frame = new insertion();
        }       
        
         public void insertionSort () {
                int[] x = new int[textfields.length];
                for (int i = 0; i < textfields.length; i++) {
                        x[i] = Integer.parseInt(textfields[i].getText());
                }
                for (int i = 1; i < x.length; i++) {
                        for (int j = i; j > 0 && x[j - 1] > x[j]; j--) {
                                swap(x, j, j - 1);
                                printArray(x);
                        }
                }
        }

        public  void swap (int  [ ] x, int a, int b) {
                int  t = x[a];
                x[a] = x[b];
                x[b] = t;
        } 
                
    private void printArray(int [ ] x){
        JPanel panel=new JPanel(new GridLayout(1, x.length));
            for (int i = 0; i < x.length; i++){
                JTextField field=new JTextField(3);
                field.setText(""+x[i]);
                panel.add(field);
            }
            GridLayout g=(GridLayout)resultPanel.getLayout();
            g.setRows(g.getRows()+1);
            resultPanel.add(panel);
    }

    
    
    
}
