您的位置:首页 > 编程语言

JTable 的TableModel (使用TMF框架只用一行代码)

2007-11-12 16:04 627 查看
简单实用:

使用JTable再也不用写TableModel了,只写一个XML文件另一行java代码就可以了.

一般读取数据都是这样:

List result=new ArrayList();

JavaBean b=new JavaBean();

b.setA("A");

result.add(b);

xml文件配置的是要显示的列,不需要显示的,只要把相应的XML代码删除就可以了.

所用的包:

castor-0.9.5.3-xml.jar

commons-collections-3.1.jar

hr.jar

j2x.jar

xml.jar


package com.andnnl;






/** *//**


* The EmployeeVO contains all the information about an employee.


* @author MAbernethy


*/


public class EmployeeVO




...{


private String firstName;


private String lastName;


private int bandLevel;


private int yearsService;


private java.math.BigDecimal salary;


private String address;


private String state;


private int zipCode;


private boolean married;






/** *//**


* Gets the address.


* @return the address


*/


public String getAddress()




...{


return address;


}






/** *//**


* Gets the band level.


* @return the band level


*/


public int getBandLevel()




...{


return bandLevel;


}






/** *//**


* Gets the first name.


* @return the first name


*/


public String getFirstName()




...{


return firstName;


}






/** *//**


* Gets the last name.


* @return the last name


*/


public String getLastName()




...{


return lastName;


}






/** *//**


* Gets the married status.


* @return the married status


*/


public boolean isMarried()




...{


return married;


}






/** *//**


* Gets the salary.


* @return the salary


*/


public java.math.BigDecimal getSalary()




...{


return salary;


}






/** *//**


* Gets the state.


* @return the state


*/


public String getState()




...{


return state;


}






/** *//**


* Gets the years of service.


* @return the years of service


*/


public int getYearsService()




...{


return yearsService;


}






/** *//**


* Gets the zip code


* @return the zip code


*/


public int getZipCode()




...{


return zipCode;


}






/** *//**


* Sets the address.


* @param string the address


*/


public void setAddress(String string)




...{


address = string;


}






/** *//**


* Sets the band level


* @param i the band level


*/


public void setBandLevel(int i)




...{


bandLevel = i;


}






/** *//**


* Sets the first name


* @param string the first name


*/


public void setFirstName(String string)




...{


firstName = string;


}






/** *//**


* Sets the last name


* @param string the last name


*/


public void setLastName(String string)




...{


lastName = string;


}






/** *//**


* Sets the married status.


* @param b the married status


*/


public void setMarried(boolean b)




...{


married = b;


}






/** *//**


* Sets the salary.


* @param decimal the salary


*/


public void setSalary(java.math.BigDecimal decimal)




...{


salary = decimal;


}






/** *//**


* Sets the state.


* @param string the state


*/


public void setState(String string)




...{


state = string;


}






/** *//**


* Sets the years of service.


* @param i the service


*/


public void setYearsService(int i)




...{


yearsService = i;


}






/** *//**


* Sets the zip code.


* @param i the zip code


*/


public void setZipCode(int i)




...{


zipCode = i;


}




}




package com.andnnl;






/** *//**


* The EmployeeGenerator randomly pieces together employee information to produce a random employee.


* @author MAbernethy


*/


public class EmployeeGenerator




...{




private static String[] states = ...{"TX", "PA", "NC", "AK", "AR", "AZ", "MD", "SD", "SC", "FL", "AL", "CA", "NV" };




private static String[] first = ...{"Allen", "Carmelo", "LeBron", "Lamar", "Tim", "Carlos", "Stephon", "Emeka", "Richard", "Amare" };




private static String[] last = ...{"Holmes", "Tomlinson", "Portis", "Alexander", "Green", "McAllister", "James", "Lewis", "Faulk", "Bennett", "Henry", "Davis", "Dillon", "Taylor", "Johnson"};




private static String[] address = ...{"550 Elm St", "1 Main Str", "742 Evergreen Terrace", "1600 Pennsylvania Dr", "10 Downing Str", "550 5th Ave" };






/** *//**


* Pieces together random information to form an EmployeeVO


* @return the random Employee


*/


public static EmployeeVO getNewEmployee()




...{


EmployeeVO empVO = new EmployeeVO();


empVO.setAddress(getRandomAddress());


empVO.setBandLevel(getRandomBandLevel());


empVO.setFirstName(getRandomFirstName());


empVO.setLastName(getRandomLastName());


empVO.setMarried(getRandomMarried());


empVO.setSalary(getRandomSalary());


empVO.setState(getRandomState());


empVO.setYearsService(getRandomService());


empVO.setZipCode(getRandomZip());


return empVO;


}




private static String getRandomFirstName()




...{


return first[(int)(Math.random() * first.length)];


}




private static String getRandomLastName()




...{


return last[(int)(Math.random() * last.length)];


}




private static int getRandomBandLevel()




...{


return (int)(Math.random() * 10);


}




private static int getRandomService()




...{


return (int)(Math.random() * 25);


}




private static java.math.BigDecimal getRandomSalary()




...{


return new java.math.BigDecimal(Math.random() * 100000).setScale(2, java.math.BigDecimal.ROUND_HALF_UP);


}




private static String getRandomAddress()




...{


return address[(int)(Math.random() * address.length)];


}




private static String getRandomState()




...{


return states[(int)(Math.random() * states.length)];


}




private static int getRandomZip()




...{


return (int)(Math.random() * 100000);


}




private static boolean getRandomMarried()




...{


return (((int)(Math.random()*2)) % 2 == 0);


}




}




package com.andnnl;






/** *//**


* The EmployeeGenerator randomly pieces together employee information to produce a random employee.


* @author MAbernethy


*/


public class EmployeeGenerator2




...{




private static String[] states = ...{"TX", "PA", "NC", "AK", "AR", "AZ", "MD", "SD", "SC", "FL", "AL", "CA", "NV" };




private static String[] first = ...{"Allen", "Carmelo", "LeBron", "Lamar", "Tim", "Carlos", "Stephon", "Emeka", "Richard", "Amare" };




private static String[] last = ...{"Holmes", "Tomlinson", "Portis", "Alexander", "Green", "McAllister", "James", "Lewis", "Faulk", "Bennett", "Henry", "Davis", "Dillon", "Taylor", "Johnson"};




private static String[] address = ...{"550 Elm St", "1 Main Str", "742 Evergreen Terrace", "1600 Pennsylvania Dr", "10 Downing Str", "550 5th Ave" };






/** *//**


* Pieces together random information to form an EmployeeVO


* @return the random Employee


*/


public static EmployeeVO2 getNewEmployee()




...{


EmployeeVO2 empVO = new EmployeeVO2();


empVO.setAddress(getRandomAddress());


empVO.setBandLevel(getRandomBandLevel());


empVO.setFirstName(getRandomFirstName());


empVO.setLastName(getRandomLastName());


empVO.setMarried(getRandomMarried());


empVO.setSalary(getRandomSalary());


empVO.setState(getRandomState());


empVO.setYearsService(getRandomService());


empVO.setZipCode(getRandomZip());


return empVO;


}




private static String getRandomFirstName()




...{


return first[(int)(Math.random() * first.length)];


}




private static String getRandomLastName()




...{


return last[(int)(Math.random() * last.length)];


}




private static int getRandomBandLevel()




...{


return (int)(Math.random() * 10);


}




private static int getRandomService()




...{


return (int)(Math.random() * 25);


}




private static java.math.BigDecimal getRandomSalary()




...{


return new java.math.BigDecimal(Math.random() * 100000).setScale(2, java.math.BigDecimal.ROUND_HALF_UP);


}




private static String getRandomAddress()




...{


return address[(int)(Math.random() * address.length)];


}




private static String getRandomState()




...{


return states[(int)(Math.random() * states.length)];


}




private static int getRandomZip()




...{


return (int)(Math.random() * 100000);


}




private static boolean getRandomMarried()




...{


return (((int)(Math.random()*2)) % 2 == 0);


}




}




package com.andnnl;




import java.awt.BorderLayout;


import java.awt.event.ActionEvent;


import java.awt.event.ActionListener;


import java.util.ArrayList;


import java.util.List;


import javax.swing.JButton;




import javax.swing.JFrame;


import javax.swing.JPanel;


import javax.swing.JScrollPane;


import javax.swing.JTable;




import com.ibm.j2x.swing.util.TableUtilities;








public class TableModelTest extends JFrame ...{


private List data;


private JTable table;


private int flag=0;




/** *//**


* Launch the application


* @param args


*/




public static void main(String args[]) ...{




try ...{


TableModelTest frame = new TableModelTest();


frame.setVisible(true);




} catch (Exception e) ...{


e.printStackTrace();


}


}






/** *//**


* Create the frame


*/




public TableModelTest() ...{


super();


setBounds(100, 100, 500, 375);


setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




final JPanel panel = new JPanel();


panel.setLayout(new BorderLayout());


getContentPane().add(panel, BorderLayout.CENTER);




final JScrollPane scrollPane = new JScrollPane();


panel.add(scrollPane, BorderLayout.CENTER);




table = new JTable();


scrollPane.setViewportView(table);


//






final JButton button = new JButton();




button.addActionListener(new ActionListener() ...{




public void actionPerformed(ActionEvent e) ...{


change();


}


});


button.setText("New JButton");


panel.add(button, BorderLayout.NORTH);




}






protected void change() ...{




if(flag==0)...{


data = new ArrayList();


for (int i=0; i<25; i++)


data.add(EmployeeGenerator.getNewEmployee());


TableUtilities.setViewToModel("resources/evil_hr_table.xml", "Personal", table, data);


flag=1;




}else...{


data = new ArrayList();


for (int i=0; i<25; i++)


data.add(EmployeeGenerator2.getNewEmployee());


flag=0;


TableUtilities.setViewToModel("resources/evil_hr_table.xml", "Hire", table, data);


}


}




}




package com.andnnl;






/** *//**


* The EmployeeVO contains all the information about an employee.


* @author MAbernethy


*/


public class EmployeeVO2




...{


private String firstName;


private String lastName;


private int bandLevel;


private int yearsService;


private java.math.BigDecimal salary;


private String address;


private String state;


private int zipCode;


private boolean married;






/** *//**


* Gets the address.


* @return the address


*/


public String getAddress()




...{


return address;


}






/** *//**


* Gets the band level.


* @return the band level


*/


public int getBandLevel()




...{


return bandLevel;


}






/** *//**


* Gets the first name.


* @return the first name


*/


public String getFirstName()




...{


return firstName;


}






/** *//**


* Gets the last name.


* @return the last name


*/


public String getLastName()




...{


return lastName;


}






/** *//**


* Gets the married status.


* @return the married status


*/


public boolean isMarried()




...{


return married;


}






/** *//**


* Gets the salary.


* @return the salary


*/


public java.math.BigDecimal getSalary()




...{


return salary;


}






/** *//**


* Gets the state.


* @return the state


*/


public String getState()




...{


return state;


}






/** *//**


* Gets the years of service.


* @return the years of service


*/


public int getYearsService()




...{


return yearsService;


}






/** *//**


* Gets the zip code


* @return the zip code


*/


public int getZipCode()




...{


return zipCode;


}






/** *//**


* Sets the address.


* @param string the address


*/


public void setAddress(String string)




...{


address = string;


}






/** *//**


* Sets the band level


* @param i the band level


*/


public void setBandLevel(int i)




...{


bandLevel = i;


}






/** *//**


* Sets the first name


* @param string the first name


*/


public void setFirstName(String string)




...{


firstName = string;


}






/** *//**


* Sets the last name


* @param string the last name


*/


public void setLastName(String string)




...{


lastName = string;


}






/** *//**


* Sets the married status.


* @param b the married status


*/


public void setMarried(boolean b)




...{


married = b;


}






/** *//**


* Sets the salary.


* @param decimal the salary


*/


public void setSalary(java.math.BigDecimal decimal)




...{


salary = decimal;


}






/** *//**


* Sets the state.


* @param string the state


*/


public void setState(String string)




...{


state = string;


}






/** *//**


* Sets the years of service.


* @param i the service


*/


public void setYearsService(int i)




...{


yearsService = i;


}






/** *//**


* Sets the zip code.


* @param i the zip code


*/


public void setZipCode(int i)




...{


zipCode = i;


}




}



evil_hr_table.xml


<?xml version="1.0" encoding="GB2312" ?>


<!-- Put your Table column mappings here -->


<data>




<model>


<className>demo.hr.TableModelFreeExample</className>


<name>Hire</name>


<column>


<name>蛾眉</name>


<field>firstName</field>


</column>


<column>


<name>Last Name</name>


<field>lastName</field>


</column>


</model>




<model>


<className>demo.hr.TableModelFreeExample</className>


<name>Personal</name>


<column>


<name>名称</name>


<field>firstName</field>


</column>


<column>


<name>Last Name</name>


<field>lastName</field>


</column>


<column>


<name>Street Address</name>


<field>address</field>


</column>


<column>


<name>State</name>


<field>state</field>


</column>


<column>


<name>Zip</name>


<field>zipCode</field>


</column>


<column>


<name>Married?</name>


<field>married</field>


</column>


</model>




<model>


<className>demo.hr.TableModelFreeExample</className>


<name>Financial</name>




<column>


<name>Last Name</name>


<field>lastName</field>


</column>


<column>


<name>First Name</name>


<field>firstName</field>


</column>


<column>


<name>Band Level</name>


<field>bandLevel</field>


</column>


<column>


<name>Years of Service</name>


<field>yearsService</field>


</column>


<column>


<name>Salary</name>


<field>salary</field>


</column>


</model>




</data>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐