import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;

import javax.swing.border.EmptyBorder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.imageio.ImageIO;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.List;
import java.awt.*;

import java.awt.*;
import java.beans.DefaultPersistenceDelegate;
import java.beans.Encoder;
import java.beans.Statement;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;

//http://www.myjavazone.com/2011/02/jcheckbox-en-la-cabecera-de-un-jtable.html

//https://www.rgagnon.com/javadetails/java-0213.html

public class jdialog09 extends JDialog 
{
	private boolean close_Jdialog = false;
	private String pathDatafile = "D:\\JAVA\\workspace\\SPLIT01\\file.dat";
	
	JTable table;
	JScrollPane scroll;
	
	public jdialog09(JFrame f, boolean modal) 
	{
	       super(f, modal);
	       
	       // fermer la fenetre
	       addWindowListener(new java.awt.event.WindowAdapter() 
	       {
	           public void windowClosing(java.awt.event.WindowEvent e) 
	           {
	        	   dispose();
	           }
	       });
	      
	       init();
     
	}
  
	private void init()
	{
		  	setResizable( false );
		  	
			setBounds(100, 100, 926, 550);
			setTitle("Jtable et Progress bar...");
			getContentPane().setLayout(null);
	     	
			ImageIcon icon = null;
			icon = createImageIcon("resources/settings.png", "");
			setIconImage( icon.getImage() );
			
		
			/*******************/

			JTable table = new JTable( new MyTableModel(pathDatafile) );
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(294, 39, 532, 159);
			getContentPane().add(scrollPane);
			scrollPane.setViewportView(table);
			
			/*******************/
		
			JButton btn_ok = new JButton("OK");
			btn_ok.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent arg0) 
				{
					close_Jdialog = false;
					System.out.println("Btn Cancel  \n");
					dispose();
				}
			});
			btn_ok.setBounds(135, 391, 89, 23);
			getContentPane().add(btn_ok);
	
			/*******************/
			
			JButton btn_cancel = new JButton("Cancel");
			btn_cancel.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent e) 
				{
					close_Jdialog = false;
					System.out.println("Btn Cancel  \n");
					dispose();
				}
			});
			btn_cancel.setBounds(359, 391, 89, 23);
			getContentPane().add(btn_cancel);
		
	}
    
		
	public ImageIcon createImageIcon(String path,  String description) 
	{
		URL imgURL = getClass().getResource(path);

		if (imgURL != null)
		{
  			return new ImageIcon(imgURL, description);
		} 
		else 
		{
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}
	
	class MyTableModel extends AbstractTableModel 
	{
		
		protected Vector data;
	    protected Vector columnNames;  
	    
	    public MyTableModel(String pathDatafile)
	    {
	    	loadDatas(pathDatafile);
	    }
	    
	    public void loadDatas(String pathDatafile)
	    {
	    	String aLine ;
	        data = new Vector();
	        columnNames = new Vector();
	        
	    	try 
	    	{
	    	      FileInputStream fin =  new FileInputStream(pathDatafile);
	    	      BufferedReader br = new BufferedReader(new InputStreamReader(fin));
	    	      // extract column names
	    	      StringTokenizer st1 =  new StringTokenizer(br.readLine(), "|");
	    	        while(st1.hasMoreTokens())
	    	          columnNames.addElement(st1.nextToken());
	    	      // extract data
	    	      while ((aLine = br.readLine()) != null) 
	    	      {  
	    	    	  StringTokenizer st2 =   new StringTokenizer(aLine, "|");
	    	    	  while(st2.hasMoreTokens())
	    	    		  data.addElement(st2.nextToken());
	    	      }
	    	      
	    	      br.close();  
	    	}
	    	 
	    	catch (Exception e) 
	    	{
	    		e.printStackTrace();
	    	}
	    	
	    }
	    
	    public int getColumnCount() 
	    {
	    	return columnNames.size();
	    }

	    public int getRowCount() 
	    {
	    	return data.size() / getColumnCount();
	    }

	    public String getColumnName(int columnIndex) 
	    {
	    	 String colName = "";

	    	 if (columnIndex <= getColumnCount())
	    	       colName = (String)columnNames.elementAt(columnIndex);

	    	 return colName;
	    }

	    public Object getValueAt(int rowIndex, int columnIndex) 
	    {
	    	return (String)data.elementAt( (rowIndex * getColumnCount()) + columnIndex);
	    }

	   
	    public Class getColumnClass(int columnIndex) 
	    {
	    	return String.class;
	    }

	    public boolean isCellEditable(int row, int col) 
	    {
	    	return false;
	    }
	   
	    public void setValueAt(Object value, int rowIndex, int columnIndex) 
	    {
	      	 return;
	    }

	}
	
  
}


/****************************************************************************************/
