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.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.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

public class jdialog08 extends JDialog 
{
	private boolean close_Jdialog = false;
	private TreeModel model;
	private List<TreeNode[]> expanded;
	private JButton btn_load_tree;
	private JButton btn_save_tree;
	private JButton btn_load_table;
	private JButton btn_save_table;
	
	
	 JTable table;
	 JScrollPane scroll;
	 DefaultTableModel model_table;
	 Object [] nom_colum = {"","Nombre","Url"};
	 Object [][] datas=new Object[3][3];
	 TableColumn Tcol;
	 JCheckBoxHeader chkheader; 
	 JButton eliminar;
	
	public jdialog08(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() );
			
		
			/*******************/

			model_table=new DefaultTableModel(datas, nom_colum);
			table = new JTable(model_table);
			  
			table.setValueAt("Java Zone", 0, 1);
			table.setValueAt("www.java-elrincondetucasa.tk", 0, 2);
			table.setValueAt("Google", 1, 1);
			table.setValueAt("www.google.com.co", 1, 2);
			  
			Tcol = table.getColumnModel().getColumn(0);
			Tcol.setCellEditor(table.getDefaultEditor(Boolean.class));  
			Tcol.setCellRenderer(table.getDefaultRenderer(Boolean.class));
			chkheader = new JCheckBoxHeader(table);
			Tcol.setHeaderRenderer(chkheader);
			 
			/*
			eliminar=new JButton("Eliminar");
			eliminar.addActionListener(new ActionListener()
			{
			   @Override
			   public void actionPerformed(ActionEvent e) 
			   {
				   for(int i=0;i<table.getRowCount();i++)
				   {
					   if(table.getValueAt(i, 0)!=null)
					   {
						   chkheader.chk.setSelected(false);
						   table.setValueAt(false, i, 0);
						   table.setValueAt("", i, 1);
						   table.setValueAt("", i, 2);
						   repaint();
						   table.updateUI();
					   }
			    }
			   }
			   
			  });
			  */
			  //scroll=new JScrollPane(tabla);
			  //add(scroll);
			  //JPanel pboton=new JPanel();
			  //pboton.add(eliminar);
			  //add(pboton,BorderLayout.SOUTH);  
			
			  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 JCheckBoxHeader extends JPanel implements TableCellRenderer, MouseListener 
{
    
	//int column;  
	boolean mousePressed = false;  
	JTable table;
	JCheckBox chk;
	JLabel label = new JLabel("Selection") ;
	  
	public JCheckBoxHeader(JTable t) 
	{
		   	table = t;
		   	chk=new JCheckBox();
		   	chk.addItemListener(new ItemListener()
		   	{
		   		@Override
		   		public void itemStateChanged(ItemEvent e) 
		   		{
		   			for(int x = 0; x < table.getRowCount(); x++)  
		   			{  
		   				table.setValueAt( new Boolean(chk.isSelected() ) , x , 0);  
		   			}  
		   		}
		   });
		  
		   add(chk);
		   add(label);
	}  
	
	public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) 
	{  
			if (table != null) 
		    {  
		    	JTableHeader cabecera = table.getTableHeader();  
		      
		    	if (cabecera != null) 
		    	{
		    		cabecera.addMouseListener(this);  
		    	}  
		    }  
		    
		    return this;  
	}  
	  
	public void seleccion(MouseEvent e)
	{
		  TableColumnModel Modelocolumna = table.getColumnModel();  
		  int columnaactual = Modelocolumna.getColumnIndexAtX(e.getX());    

		  if (columnaactual == 0) 
		  {
		   
			  if(chk.isSelected())
			  {
				  chk.setSelected(false);
			  }
			  else
			  {
				  chk.setSelected(true);
			  }
		  }
	    
		  ((JTableHeader)e.getSource()).repaint(); 
	 }
	  
	  public void mouseClicked(MouseEvent e) {  seleccion(e);  }  
	  public void mousePressed(MouseEvent e) {  seleccion(e);  }  
	  public void mouseReleased(MouseEvent e) { }  
	  public void mouseEntered(MouseEvent e) { }  
	  public void mouseExited(MouseEvent e) { }  
}


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