|
|
![]() |
tekzaf@yahoo.com |
Bu örnekte PropertiesTableModel class'ını yaratıyoruz. Bu class AbstractTableModel class'ını extend etmekte. Bu model ile properties dosyalarını göstermek için JTable kullanılabilir. Böylece properties dosyaları bir tabloda görüntülenmiş olur.
Göster Gizle Kopar Satır Gizle Satır Göster |
1 package com.godoro.test; 2 import java.awt.*; 3 import javax.swing.*; 4 import javax.swing.table.*; 5 import javax.swing.event.*; 6 import java.util.*; 7 import java.io.*; 8 public class PropertiesTableModel extends AbstractTableModel{ 9 private final static int NONE=0; 10 private final static int VALUE=1; 11 private final static int BOTH=2; 12 private int thisEditable=NONE; 13 private String thePath=null; 14 private Properties theProperties=new Properties(); 15 private ArrayList dataList=new ArrayList(); 16 private String[] columnname=new String[]{"Key","Value"}; 17 public PropertiesTableModel(String systemid) throws FileNotFoundException,IOException{ 18 setSystemid(systemid); 19 theProperties.load(new FileInputStream(thePath)); 20 setDatas(); 21 } 22 public PropertiesTableModel(String path,int editable) throws FileNotFoundException,IOException{ 23 setSystemid(path); 24 theProperties.load(new FileInputStream(thePath)); 25 setEditable(editable); 26 setDatas(); 27 } 28 public Properties getProperties(){ 29 return theProperties; 30 } 31 public void setSystemid(String path){ 32 thePath=path; 33 } 34 public void setEditable(int editable){ 35 thisEditable=editable; 36 } 37 public int getEditable(){ 38 return thisEditable; 39 } 40 public int getRowCount(){ 41 return dataList.size()/2; 42 } 43 public int getColumnCount(){ 44 return 2; 45 } 46 public Object getValueAt(int row, int column){ 47 String[] keys=getKeys(); 48 if(column==0){ 49 return dataList.get(row*2); 50 }else if(column==1){ 51 return dataList.get(row*2+1); 52 }else{ 53 return null; 54 } 55 } 56 public void setValueAt(Object a,int row,int column){ 57 if(column==0){ 58 dataList.set(2*row,(String)a); 59 } 60 else if(column==1){ 61 dataList.set(2*row+1,(String)a); 62 } 63 } 64 public String getColumnName(int column){ 65 return columnname[column]; 66 } 67 public boolean isCellEditable(int rowindex,int colindex){ 68 if(thisEditable==2){ 69 return true; 70 }else if(thisEditable==1){ 71 if(colindex==1){ 72 return true; 73 }else{ 74 return false; 75 } 76 }else{ 77 return false; 78 } 79 } 80 public void updateProperties(){ 81 Properties newProperties=new Properties(); 82 for(int i=0;i<dataList.size()/2;i++){ 83 newProperties.put(dataList.get(i*2),dataList.get(i*2+1)); 84 } 85 theProperties=newProperties; 86 } 87 public void store() 88 throws IOException,FileNotFoundException 89 { 90 store(thePath); 91 } 92 public void store(String path) 93 throws IOException,FileNotFoundException 94 { 95 updateProperties(); 96 FileOutputStream fos=new FileOutputStream(path); 97 theProperties.store(fos,""); 98 } 99 public void insertEmptyRow(){ 100 dataList.add(""); 101 dataList.add(""); 102 } 103 public void deleteRow(int index){ 104 dataList.remove(index*2); 105 dataList.remove(index*2); 106 } 107 public void deleteRows(int[] indexs){ 108 for(int i=0;i<indexs.length;i++){ 109 deleteRow(indexs[i]); 110 } 111 } 112 private void setDatas(){ 113 String[] keys=getKeys(); 114 for(int i=0;i<keys.length;i++){ 115 dataList.add(2*i,keys[i]); 116 dataList.add(2*i+1,theProperties.get(keys[i])); 117 } 118 } 119 private String[] getKeys(){ 120 String[] keys=new String[theProperties.size()]; 121 int i=0; 122 for (Enumeration e=theProperties.keys();e.hasMoreElements();){ 123 keys[i]=(String)e.nextElement(); 124 i=i+1; 125 } 126 return keys; 127 } 128 public static void main(String[] args) throws Exception{ 129 String filepath="E:\\Zafer\\test.properties"; 130 JFrame frame=new JFrame(); 131 frame.setBounds(0,0,600,600); 132 PropertiesTableModel model=new PropertiesTableModel(filepath,PropertiesTableModel.BOTH); 133 JTable table=new JTable(model); 134 frame.getContentPane().setLayout(new BorderLayout()); 135 frame.getContentPane().add(new JScrollPane(table),BorderLayout.CENTER); 136 frame.setVisible(true); 137 } 138 }
AbstractTableModel abstract bir class'tır.
public int getRowCount(); public int getColumnCount(); public Object getValueAt(int row, int column);
yukarıdaki üç method'u abstract'tır. PropertiesTableModel AbstractTableModel'i extend ettiği için bu üç methodu yazmıştır.
public int getRowCount(){ return dataList.size()/2; } public int getColumnCount(){ return 2; } public Object getValueAt(int row, int column){ String[] keys=getKeys(); if(column==0){ return dataList.get(row*2); }else if(column==1){ return dataList.get(row*2+1); }else{ return null; } }
Property'ler bir key ve buna karşılık gelen bir value'den oluşur. Tüm veri dataList'te atanır. Liste key1,value1,key2,value2 vs.. şeklinde gider. dataList'teki verilerin tablolarda gözükmesi için yukarıdaki üç method gerçekleştirilir. dataList'tin kaç elemanlı ise yarısı satır sayısını verir. Sütün sayısı zaten iki'dir. getValue ise (row,column)'da gözükecek elemanı vermek için kullanılır. row*2 key'i, row*2+1 ise value'yi vermektedir. Kullanıcı tablodaki değerleri değiştirdiğinde model'deki setValueAt methodu çağrılır. Bu method getValueAt method'unun tersini yapar.
public void setValueAt(Object a,int row,int column){ if(column==0){ dataList.set(2*row,(String)a); } else if(column==1){ dataList.set(2*row+1,(String)a); } }
isCellEditable methodu ise (rowindex,colindex)'deki bir değerin değiştirilip değiştirilemiyeceğine karar vermek içindir. PropertiesTableModel class'ımız kurucuda editable değişkenini almaktadır. Bu değişekene göre key veya value değerlerinin değiştirilip değiştirilemeyeceğine karar verilmektedir. 2 ise key,value, 1 ise value değiştirilebilir. 0 ise ikiside değişitirelemez.
public boolean isCellEditable(int rowindex,int colindex){ if(thisEditable==2){ return true; }else if(thisEditable==1){ if(colindex==1){ return true; }else{ return false; } }else{ return false; } }
PropertiesTableModel ile boş bir satır ekleyebilirsiniz. Bir satırı silebilirsiniz. Properties'i bir dosyaya yazabilirsiniz.
PropertiesTableModel ile aşağıdaki gibi
PropertiesTableModel model=new PropertiesTableModel(filepath,PropertiesTableModel.BOTH); JTable table=new JTable(model);
bir tablo yaratılabilir. Bu tablo bir panel'e veya frame'e eklenerek görüntülenebilir.
|
|