import java.net.URL;
import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class jtabpane03 extends JDialog 
{
	
	private	JPanel	panel1;
	private	JPanel	panel2;
	private	JPanel	panel3;
	private	JPanel	panel4;
	
	private JTable table;
	private static final int BOOLEAN_COLUMN_INDEX = 0;
	private String[] toolTipStr = {"Select/Deselect all" , "aqw"      ,"Java","French","Latin"};
	
	public jtabpane03(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();
	           }
	       });
	      
	       try { 
	    	   
	            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 
	        } 
	        catch (Exception e) { 
	            System.out.println("Look and Feel not set"); 
	        } 
	       
	       init();
	}
  
	private void init()
	{
		setResizable( false );
		setBounds(0, 0, 1000, 650);
		
		setTitle("Tabpane3 - debug ...");
     	
		ImageIcon icon = null;
		icon = createImageIcon("resources/settings.png", "");
		setIconImage( icon.getImage() );
		
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
				
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(0, 0, 984, 465);
		getContentPane().add(tabbedPane);
		
		String[] columnNames =  { "", "Test", "Deux" , "rien", "plus"};
		
	    Object[][] data = 
	    	{
	            { Boolean.TRUE, "USA", "Washington DC", "179", "false"},
	            { Boolean.TRUE, "Canada", "Ottawa", "32", "true"},
	            { Boolean.TRUE, "United Kingdom", "London", "60", "false"},
	            { Boolean.TRUE, "Germany", "Berlin", "83", "true"},
	            { Boolean.TRUE, "France", "Paris", "28", "true"},
	            { Boolean.FALSE, "Norway", "Oslo", "79", "false"},
	            { Boolean.TRUE, "India", "New Delhi", "1079", "false"},
	            { Boolean.TRUE, "USA", "Washington DC", "179", "false"},
	            { Boolean.TRUE, "Canada", "Ottawa", "32", "true"},
	            { Boolean.TRUE, "United Kingdom", "London", "60", "false"},
	            { Boolean.TRUE, "Germany", "Berlin", "83", "true"},
	            { Boolean.TRUE, "France", "Paris", "28", "true"},
	            { Boolean.FALSE, "Norway", "Oslo", "79", "false"},
	            { Boolean.TRUE, "India", "New Delhi", "1079", "false"}
	        };
		
		
	    TableModel model = new DefaultTableModel(data,  columnNames)
	    {		
	    	@Override
		    public boolean isCellEditable(int row, int column)
		    {
	    		// make read only fields except column 0,13,14
		        return column == 0 ;
	        }
	    	
	    	@Override
	        public Class<?> getColumnClass(int columnIndex) 
	    	{
	            if (columnIndex == 0) 
	            {
	                return Boolean.class;
	            } 
	            else 
	            {
	                return String.class;
	            }
	        }
	    };
	    
	   
	    JTable table =new JTable(model);    
	    
	    
  JTableHeader Theader = table.getTableHeader();
        
        Theader.setBackground(Color.red); // change the Background color
        Theader.setForeground(Color.WHITE); // change the Foreground
        
        Theader.setFont(new Font("Tahome", Font.BOLD, 20)); // font name style size
        ((DefaultTableCellRenderer)Theader.getDefaultRenderer())
                .setHorizontalAlignment(JLabel.CENTER); // center header text
	    
	    
	    // dimension
	   // table.setRowHeight(30);
	   // table.setFont(new Font("Serif", Font.BOLD, 20));
	    
	    ToolTipHeader header = new ToolTipHeader(table.getColumnModel());
	    header.setToolTipStrings(toolTipStr);
	    //header.setToolTipText("Default ToolTip TEXT");
	    table.setTableHeader(header);
		
		table.setDragEnabled(false);
		table.getTableHeader().setReorderingAllowed(false);
		
		TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
	    table.setRowSorter(sorter);
		
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(38, 11, 716, 252);
		
		
		final TableColumn booleanColumn = table.getTableHeader().getColumnModel().getColumn(BOOLEAN_COLUMN_INDEX);

		 
		// Création de la checkBox qui sera affichée en en-tête de colonne.
			final JCheckBox checkBoxHeader = new JCheckBox();

			checkBoxHeader.setSelected(true);
			// Création et affectation du cell renderer qui aura en charge la représentation de l'en-tête de colonne
			// (affichant une checkbox en plus du rendu par défaut)
			booleanColumn.setHeaderRenderer(new CheckBoxHeaderRenderer(checkBoxHeader));

			// Ajout du "forwarder". Listener qui va être informé des clics sue l'en-tête de colonne et les transmettre à la
			// checkbox.
			final HeaderMouseEventsForwarder eventFrowarder =
					new HeaderMouseEventsForwarder(checkBoxHeader, BOOLEAN_COLUMN_INDEX);
			table.getTableHeader().addMouseListener(eventFrowarder);

			// Ajout du listener qui va cocher/décocher toutes les checkbox
			checkBoxHeader.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(final ActionEvent e) {
					if (!(e.getSource() instanceof JCheckBox)) {
						return;
					}
					final boolean selected = ((JCheckBox) e.getSource()).isSelected();
					final TableModel model = table.getModel();
					for (int i = 0; i < model.getRowCount(); i++) {
						model.setValueAt(selected, i, BOOLEAN_COLUMN_INDEX);
					}

				}
			});
		
		/*************************************************************************************************/
		
		String infos[][]={ {"101","Amit","670000"},    
                   			{"102","Jai","780000"},    
                   			{"101","Sachin","700000"}};    
			
		String column[]={"ID","NAME","SALARY"};         
		JTable jt=new JTable(data,column);    
		
		  JTableHeader xTheader = jt.getTableHeader();
	        
	        xTheader.setBackground(Color.red); // change the Background color
	        xTheader.setForeground(Color.WHITE); // change the Foreground
	        
	        xTheader.setFont(new Font("Tahome", Font.BOLD, 20)); // font name style size
	        ((DefaultTableCellRenderer)xTheader.getDefaultRenderer())
	                .setHorizontalAlignment(JLabel.CENTER); // center header text
			
		jt.setDragEnabled(false);
		jt.getTableHeader().setReorderingAllowed(false);
			
		jt.setBounds(30,40,200,300);          
		JScrollPane sp=new JScrollPane(jt);    
		
		/*************************************************************************************************/
			
		JPanel panel = new JPanel();
		tabbedPane.addTab("New tab", null, panel, null);
		panel.setLayout(null);
		panel.add(scrollPane);
		//panel.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("New tab", null, panel_1, null);
		panel.setLayout(null);
		panel_1.add(sp);
		
		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("New tab", null, panel_2, null);
		
		JPanel panel_3 = new JPanel();
		tabbedPane.addTab("New tab", null, panel_3, null);
		panel_3.setLayout(null);
		
		JButton btnNewButton = new JButton("New button");
		btnNewButton.setBounds(76, 93, 89, 23);
		panel_3.add(btnNewButton);
		
		
		getContentPane().setLayout(null);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setToolTipText("mlm\u00F9l");
			buttonPane.setBounds(0, 579, 984, 33);
			buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
			getContentPane().add(buttonPane);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
		
		
		
		
	    
	}	
	
	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 ToolTipHeader extends JTableHeader {
	    String[] toolTips;
	   
	    public ToolTipHeader(TableColumnModel model) {
	      super(model);
	    }
	     
	    public String getToolTipText(MouseEvent e) {
	      int col  = columnAtPoint(e.getPoint());
	      int modelCol = getTable().convertColumnIndexToModel(col);
	      String retStr;
	      try {
	        retStr = toolTips[modelCol];
	      } catch (NullPointerException ex) {
	        retStr = "";
	      } catch (ArrayIndexOutOfBoundsException ex) {
	        retStr = "";
	      }
	      if (retStr.length() < 1) {
	        retStr = super.getToolTipText(e);
	      }
	      return retStr;
	    }  
	     
	    public void setToolTipStrings(String[] toolTips) {
	      this.toolTips = toolTips;
	    }
	  }
}