import java.awt.Color;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.*;
import java.util.Map.*;

import javax.swing.*;

public class BelediyelerNufusTable {
	
	public static String deger = "";

	public static boolean tfAdiyaman;
	public static boolean tfAntalya;
	public static boolean tfIstanbul;

	public static String[][] data = new String[20][4];

	BelediyelerNufusTable() {
		
		int x = 10;
		int y = 10;
		int w = 100; 
		int h = 30;
		int xf = w + 15;
		int yf = h + 15;
		
		JFrame frame = new JFrame("CheckBox ile HashMap ve JTable Kullanımı");
		frame.setLayout(null);
		
		JPanel anapanel = new JPanel();
		anapanel.setLayout(null);
		anapanel.setBackground(new Color(217, 192, 145));
		anapanel.setBounds(25,25,350,450);
		
		JLabel sehirSecBaslik = new JLabel("Şehir İşaretleyiniz");
		sehirSecBaslik.setFont(new Font("Ubuntu", Font.BOLD ,20));
		sehirSecBaslik.setBounds(x,y,w+100,h); x = 15; y = y + yf;
		
		JCheckBox cbAdiyaman = new JCheckBox("Adıyaman");
		cbAdiyaman.setOpaque(false);
		cbAdiyaman.setBounds(x,y,w,h); x = x + xf;
		
		JCheckBox cbAntalya = new JCheckBox("Antalya");
		cbAntalya.setOpaque(false);
		cbAntalya.setBounds(x, y, w, h); x = x + xf;

		JCheckBox cbIstanbul = new JCheckBox("İstanbul");
		cbIstanbul.setOpaque(false);
		cbIstanbul.setBounds(x, y, w, h); x = 15; y = y + yf;
		
		JLabel belediyelerBaslik = new JLabel("Belediyeler");
		belediyelerBaslik.setHorizontalAlignment(JLabel.CENTER);
		belediyelerBaslik.setFont(new Font("Ubuntu", Font.BOLD ,25));
		belediyelerBaslik.setBounds(x,y,350,30); y = y + yf;
		
		String[] sutun = {"Sıra", "Şehir", "Belediye", "Nüfus"};
		
		JTable table = new JTable(data,sutun);
		
		JScrollPane scrollP = new JScrollPane(table);
	    scrollP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollP.setBounds(x,y,320,270); y = y + yf;
				
	    cbAdiyaman.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {  
				
				tfAdiyaman = cbAdiyaman.isSelected();
				tfAntalya = cbAntalya.isSelected();
			    tfIstanbul = cbIstanbul.isSelected();
				
			    hashMapSehirler(tfAdiyaman,tfAntalya,tfIstanbul);
			    table.repaint();
			    
			}
		});
		
		cbAntalya.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				
				tfAdiyaman = cbAdiyaman.isSelected();
				tfAntalya = cbAntalya.isSelected();
			    tfIstanbul = cbIstanbul.isSelected();
				
			    hashMapSehirler(tfAdiyaman,tfAntalya,tfIstanbul);
			    table.repaint();
			    
			}
		});
		
		cbIstanbul.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				
				tfAdiyaman = cbAdiyaman.isSelected();
				tfAntalya = cbAntalya.isSelected();
			    tfIstanbul = cbIstanbul.isSelected();
				
			    hashMapSehirler(tfAdiyaman,tfAntalya,tfIstanbul);
			    table.repaint();
			    
			}
		});
		
		anapanel.add(sehirSecBaslik);
		anapanel.add(cbAdiyaman);
		anapanel.add(cbAntalya);
		anapanel.add(cbIstanbul);
		anapanel.add(belediyelerBaslik);
		anapanel.add(scrollP);
		
		frame.add(anapanel);
		
		frame.setSize(400,540);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setVisible(true);
	}
	
	public static String[][] hashMapSehirler(boolean tf1, boolean tf2, boolean tf3) {
		
		for(int i = 0; i<20; i++) {
			
			data[i][0] = "";
			data[i][1] = "";
			data[i][2] = "";
			data[i][3] = "";
			
		}
		
		HashMap<String, String> belediyeler = new HashMap<>();
		
		        		// key     value
		belediyeler.put("Gerger","Adıyaman");
		belediyeler.put("Besni","Adıyaman");
		belediyeler.put("Gölbaşı","Adıyaman");
		belediyeler.put("Kahta","Adıyaman");
		belediyeler.put("Çelikhan", "Adıyaman");
		belediyeler.put("Samsat","Adıyaman");
		belediyeler.put("Sincik","Adıyaman");
		belediyeler.put("Tut","Adıyaman");
		
		belediyeler.put("Alanya","Antalya");
		belediyeler.put("Muratpaşa","Antalya");
		belediyeler.put("Kemer","Antalya");
		belediyeler.put("Serik","Antalya");
		belediyeler.put("Kepez", "Antalya");
		
		belediyeler.put("Kadıköy","İstanbul");
		belediyeler.put("Adalar","İstanbul");
		belediyeler.put("Beşiktaş","İstanbul");
		belediyeler.put("Üsküdar","İstanbul");
		belediyeler.put("Fatih", "İstanbul");
		belediyeler.put("Bayrampaşa","İstanbul");
		belediyeler.put("Pendik", "İstanbul");
		
		HashMap<Integer, String> nufus = new HashMap<>();
		
		nufus.put(525395,"Üsküdar");
		nufus.put(485233, "Kadıköy");
		nufus.put(1789358, "Beşiktaş");
		nufus.put(16372, "Adalar");
		nufus.put(382990, "Fatih");
		nufus.put(274884, "Bayrampaşa");
		nufus.put(741895, "Pendik");
		
		nufus.put(591895, "Kepez");
		nufus.put(521183, "Muratpaşa");
		nufus.put(350636, "Alanya");
		nufus.put(134953, "Serik");
		nufus.put(46615, "Kemer");
		
		nufus.put(127534, "Kahta");
		nufus.put(16416, "Gerger");
		nufus.put(15294, "Çelikhan");
		nufus.put(50150, "Gölbaşı");
		nufus.put(77207, "Besni");
		nufus.put(7313, "Samsat");
		nufus.put(16341, "Sincik");
		nufus.put(9686, "Tut");
		
		
		String deger = "";
	
		int dataInt = 0;
		
		if(tf1) { 
			
			for(Entry giris: belediyeler.entrySet()) {
				if(giris.getValue().equals("Adıyaman")) {
					
					String ilce = (String) giris.getKey();
					
					for(Entry nufusGiris: nufus.entrySet()) {
						if(nufusGiris.getValue().equals(ilce)) {
										      		
							data[dataInt][0] = "" + (dataInt+1);
							data[dataInt][1] = (String) giris.getValue();
							data[dataInt][2] = ilce;
							data[dataInt][3] = ""+nufusGiris.getKey();
							
							dataInt++;
						}
						
					}
					
				}	            	
				
			}
		}
		
		if(tf2) { 
			
			for(Entry giris: belediyeler.entrySet()) {
				if(giris.getValue().equals("Antalya")) {
					
					String ilce = (String) giris.getKey();
					
					for(Entry nufusGiris: nufus.entrySet()) {
						if(nufusGiris.getValue().equals(ilce)) {
							
							data[dataInt][0] = "" + (dataInt+1);
							data[dataInt][1] = (String) giris.getValue();
							data[dataInt][2] = ilce;
							data[dataInt][3] = ""+nufusGiris.getKey();
							
							dataInt++;							
						}
						
					}
					
				}	            	
				
			}
		}
		
		if(tf3) { 
			
			for(Entry giris: belediyeler.entrySet()) {
				if(giris.getValue().equals("İstanbul")) {
					
					String ilce = (String) giris.getKey();
					
					for(Entry nufusGiris: nufus.entrySet()) {
						if(nufusGiris.getValue().equals(ilce)) {
							
							data[dataInt][0] = "" + (dataInt+1);
							data[dataInt][1] = (String) giris.getValue();
							data[dataInt][2] = ilce;
							data[dataInt][3] = ""+nufusGiris.getKey();
							
							dataInt++;							
						}
						
					}
					
				}	            	
				
			}
		}
		
		return data;
				
	}

}
