Java Form Uygulaması For döngüsüyle

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Tasarim implements ActionListener {
   JList[] lbox=new JList[4];
   DefaultListModel[] dlm=new DefaultListModel[4]; 
   JTextField[] tbox=new JTextField[3];
   JTextField[] tbox1=new JTextField[3];
	JButton[] buton=new JButton[4];
	JComboBox csehir,csehir2;
	JPanel anajpn,gunjpn,jpn[]=new JPanel[6];
	JScrollPane[] scp=new JScrollPane[4];
	String[] btitle= {"Ekle","Sil","Güncelle"};
	DefaultComboBoxModel dcmsehir=new DefaultComboBoxModel();
	DefaultComboBoxModel dcmsehir2=new DefaultComboBoxModel();
	JLabel[] jlb=new JLabel[4]; 
	JLabel[] jlb2=new JLabel[4]; 
	String[] baslik= {"AD","SOYAD","YAŞ","ŞEHİR"}; 
	String[] sehirler= {"K.Maraş","Osmaniye","Hatay","Gaziantep","Malatya","Mersin"};
	JFrame frm,frm2;
   public Tasarim()
	{
	    frm=new JFrame("Baslik");
		frm.setSize(600, 600);
		frm.setLayout(new FlowLayout());
		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
		anajpn=new JPanel();
		anajpn.setPreferredSize(new Dimension(400,500));
		anajpn.setLayout(new GridLayout(1,4));
	  for(int i=0;i<tbox.length;i++)
	  {
		  jpn[i]=new JPanel();
		  dlm[i]=new DefaultListModel();
		  lbox[i]=new JList(dlm[i]);
		  scp[i]=new JScrollPane(lbox[i]);
		  scp[i].setPreferredSize(new Dimension(100,300));
		  tbox[i]=new JTextField();
		  tbox[i].setPreferredSize(new Dimension(100,25));
		  buton[i]=new JButton(btitle[i]);
		  buton[i].setPreferredSize(new Dimension(100,25));
		  buton[i].addActionListener(this);
		  buton[i].setActionCommand(btitle[i]);
		  jlb[i]=new JLabel(baslik[i]);
		  jpn[i].add(jlb[i]);
		  jpn[i].add(scp[i]);
		  jpn[i].add(tbox[i]);
		  jpn[i].add(buton[i]);
	      anajpn.add(jpn[i]);
		if(i==tbox.length-1)
		{
			  jpn[i+1]=new JPanel();
			  dlm[i+1]=new DefaultListModel();
			  lbox[i+1]=new JList(dlm[i+1]);
			  scp[i+1]=new JScrollPane(lbox[i+1]);
			  scp[i+1].setPreferredSize(new Dimension(100,300));
			  csehir=new JComboBox(dcmsehir);
			  csehir.setPreferredSize(new Dimension(100,25));
			  jlb[i+1]=new JLabel(baslik[i+1]);
			  jpn[i+1].add(jlb[i+1]);
			  jpn[i+1].add(scp[i+1]);
			  jpn[i+1].add(csehir);
		      anajpn.add(jpn[i+1]);
			break;
		}
	     
	  }
	  csehir2=new JComboBox(dcmsehir2);
	  csehir2.setPreferredSize(new Dimension(100,25));
	  for(int i=0;i<sehirler.length;i++)
	  {
		  dcmsehir.addElement(sehirler[i]);
		  dcmsehir2.addElement(sehirler[i]);
	  } 
	     frm.add(anajpn);
	     frm.setVisible(true);
	   
	}
   private void gungoster(String[] txtler)
   {
	     frm2=new JFrame("Güncelle");
	    frm2.setSize(300, 300);
	    frm2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frm2.setLayout(new FlowLayout());
		  //Güncelleme
		  gunjpn=new JPanel();
		  gunjpn.setPreferredSize(new Dimension(200,170));
		  gunjpn.setLayout(new FlowLayout());
		  jpn[4]=new JPanel();
		  jpn[4].setPreferredSize(new Dimension(75,150));
		  jpn[5]=new JPanel();
		  jpn[5].setPreferredSize(new Dimension(100,150));
		  for(int i=0;i<tbox1.length;i++)
		  {     tbox1[i]=new JTextField();
		        tbox1[i].setPreferredSize(new Dimension(100,25));
		        tbox1[i].setText(txtler[i]);
			    jlb2[i]=new JLabel(baslik[i]);
		        jlb2[i].setPreferredSize(new Dimension(50,25));
			    jpn[4].add(jlb2[i]);
			    jpn[5].add(tbox1[i]);
		  }
		  
		  jlb2[3]=new JLabel(baslik[3]);
		  jlb2[3].setPreferredSize(new Dimension(50,25));
		  buton[3]=new JButton("Güncelle");
		  buton[3].setPreferredSize(new Dimension(100,25));
		  buton[3].setActionCommand("gun");
		  buton[3].addActionListener(this);
		  jpn[4].add(jlb2[3]);
		   jpn[5].add(csehir2);
		   jpn[5].add(buton[3]);
		  gunjpn.add(jpn[4]);
		  gunjpn.add(jpn[5]);
	     frm2.add(gunjpn);
	     frm2.setVisible(true);
   }
   //Override
   public void actionPerformed(ActionEvent arg)
   {    int yas=sayicevir(tbox[2].getText());
	    int secilen=csehir.getSelectedIndex();
	    int secilen2=lbox[0].getSelectedIndex();
	    int secilen3=csehir2.getSelectedIndex();
	   boolean durum=dogrula(tbox);
	    if(arg.getActionCommand()=="Ekle"&&yas!=-1)
	    {
	    	dlm[0].addElement(tbox[0].getText());
	    	dlm[1].addElement(tbox[1].getText());
	    	dlm[2].addElement(tbox[2].getText());
	    	dlm[3].addElement(dcmsehir.getElementAt(secilen));
	    	for(int i=0;i<tbox.length;i++)
	    		tbox[i].setText("");
	    }
	    if(arg.getActionCommand()=="Sil"&&secilen2>-1)
	    {
	    	 for(int i=0;i<lbox.length;i++)
                 dlm[i].removeElementAt(secilen2);
	    }
	    if(arg.getActionCommand()=="Güncelle"&&secilen2>-1)
	    {   String[] txtler=new String[3]; 
	        for(int i=0;i<txtler.length;i++)
	        	  txtler[i]=(String)dlm[i].getElementAt(secilen2);
	    	 frm.setVisible(false);
	    	 gungoster(txtler);
	    	 
	    }
        if(arg.getActionCommand()=="gun"&&secilen3>-1)
        {   if(dogrula(tbox1)) {
        	 for(int i=0;i<lbox.length;i++)
                 dlm[i].removeElementAt(secilen2);
         for(int i=0;i<tbox1.length;i++)
            dlm[i].insertElementAt(tbox1[i].getText(), secilen2);
            dlm[3].insertElementAt(dcmsehir2.getElementAt(secilen3), secilen2);
        	frm2.setVisible(false);
        	frm.setVisible(true);
        }
        }
   }
   private boolean dogrula(JTextField[] txt)
   {
	    int sayac=0;
	    for(int i=0;i<txt.length;i++)
	    	if(txt[i].getText().length()>0)
	    		sayac++;
         if(txt.length==sayac)
        	 return true;
         else
        	 return false;
   }
   private int sayicevir(String yas)
   {
	   int sayi=-1;
	   try {
		   sayi=Integer.parseInt(yas);
		   return sayi;
	   }catch(Exception ex)
	   {
		   sayi=-1;
		   return sayi;
	   }
   }
	public static void main(String[] arg)
	{
		new Tasarim();
	}
}

 

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir