📄 evilhrdirector.java
字号:
package demo.hr;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
/**
* The demonstration application for the TableModel Free design pattern.
* This application shows how multiple tables can be simplified
* greatly by using the TMF. It allows for swapping in and out
* of both TMF code and classical Table Model code, to allow
* for a side by side comparison. Both implementations behave
* exactly the same, the only difference is the code to make it work.
* <p>The application itself acts as an "Evil HR Director's"
* personal tool - a table on the left provides potential new hires,
* while the tables on the right present the personal and financial
* information of current employees. The "Hire" button hires
* an employee from the pool and moves their information over to the
* current employees tables. The "Fire" button deletes the former
* employees records from both current employees tables.
* <p>The main function to examine here is the <code>setupTables()</code>
* function, which sets up the comparison between the TMF and classical
* Table Model design.
* <p>The goal is to show how the TMF can be used - not to provide
* a well rounded bug-free application. Thus you may find
* exceptions.
* @author MAbernethy
*/
public class EvilHRDirector extends JFrame
{
private javax.swing.JPanel jPanel = null;
private javax.swing.JScrollPane personalScrollPane = null;
private javax.swing.JTable personalTable = null;
private javax.swing.JScrollPane financialScrollPane = null;
private javax.swing.JTable financialTable = null;
private javax.swing.JButton btnHire = null;
private javax.swing.JButton btnFire = null;
private TableModelFreeExample tmf;
private TableModelExample tm;
private javax.swing.JScrollPane hireScrollPane = null;
private javax.swing.JTable hireTable = null;
private javax.swing.JPanel hirePanel = null;
private javax.swing.JPanel firePanel = null;
public EvilHRDirector() {
super();
initialize();
}
/**
* This function is used to compare the TMF and the classical Table Model design.
* It contains instances of two classes - the TableModelFreeExample and the
* TableModelExample - and allows the user to swap between the two classes.
* <p>This function initializes the potential new hire list, passes the needed
* information to the table model example class, and then adds the example class
* as an ActionListener on the buttons.
*
*/
public void setupTables()
{
List data = new ArrayList();
for (int i=0; i<25; i++)
data.add(EmployeeGenerator.getNewEmployee());
tmf = new TableModelFreeExample(hireTable, personalTable, financialTable, data);
// tm = new TableModelExample(hireTable, personalTable, financialTable, data);
btnHire.addActionListener(tmf);
btnHire.addActionListener(tm);
btnFire.addActionListener(tmf);
btnFire.addActionListener(tm);
}
private void initialize() {
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex){}
SwingUtilities.updateComponentTreeUI(this);
this.setContentPane(getJPanel());
this.setSize(763, 476);
this.setTitle("Evil HR Director");
setupTables();
}
public static void main(String[] args)
{
EvilHRDirector hm = new EvilHRDirector();
hm.setVisible(true);
}
private javax.swing.JPanel getJPanel() {
if(jPanel == null) {
jPanel = new javax.swing.JPanel();
java.awt.GridBagConstraints consGridBagConstraints6 = new java.awt.GridBagConstraints();
java.awt.GridBagConstraints consGridBagConstraints7 = new java.awt.GridBagConstraints();
consGridBagConstraints6.insets = new java.awt.Insets(12,10,20,6);
consGridBagConstraints6.fill = java.awt.GridBagConstraints.BOTH;
consGridBagConstraints6.weighty = 1.0;
consGridBagConstraints6.weightx = 1.0;
consGridBagConstraints6.gridy = 0;
consGridBagConstraints6.gridx = 0;
consGridBagConstraints7.insets = new java.awt.Insets(12,6,20,13);
consGridBagConstraints7.fill = java.awt.GridBagConstraints.BOTH;
consGridBagConstraints7.weighty = 1.0;
consGridBagConstraints7.weightx = 1.0;
consGridBagConstraints7.gridy = 0;
consGridBagConstraints7.gridx = 1;
jPanel.setLayout(new java.awt.GridBagLayout());
jPanel.add(getHirePanel(), consGridBagConstraints6);
jPanel.add(getFirePanel(), consGridBagConstraints7);
}
return jPanel;
}
private javax.swing.JTable getPersonalTable() {
if(personalTable == null) {
personalTable = new javax.swing.JTable();
}
return personalTable;
}
private javax.swing.JScrollPane getPersonalScrollPane() {
if(personalScrollPane == null) {
personalScrollPane = new javax.swing.JScrollPane();
personalScrollPane.setViewportView(getPersonalTable());
}
return personalScrollPane;
}
private javax.swing.JTable getFinancialTable() {
if(financialTable == null) {
financialTable = new javax.swing.JTable();
}
return financialTable;
}
private javax.swing.JScrollPane getFinancialScrollPane() {
if(financialScrollPane == null) {
financialScrollPane = new javax.swing.JScrollPane();
financialScrollPane.setViewportView(getFinancialTable());
}
return financialScrollPane;
}
private javax.swing.JButton getBtnHire() {
if(btnHire == null) {
btnHire = new javax.swing.JButton();
btnHire.setText("Hire");
}
return btnHire;
}
private javax.swing.JButton getBtnFire() {
if(btnFire == null) {
btnFire = new javax.swing.JButton();
btnFire.setText("Fire");
btnFire.setToolTipText("Purrrrrr");
}
return btnFire;
}
private javax.swing.JTable getHireTable() {
if(hireTable == null) {
hireTable = new javax.swing.JTable();
}
return hireTable;
}
private javax.swing.JScrollPane getHireScrollPane() {
if(hireScrollPane == null) {
hireScrollPane = new javax.swing.JScrollPane();
hireScrollPane.setViewportView(getHireTable());
}
return hireScrollPane;
}
private javax.swing.JPanel getHirePanel() {
if(hirePanel == null) {
hirePanel = new javax.swing.JPanel();
java.awt.GridBagConstraints consGridBagConstraints5 = new java.awt.GridBagConstraints();
java.awt.GridBagConstraints consGridBagConstraints4 = new java.awt.GridBagConstraints();
consGridBagConstraints5.insets = new java.awt.Insets(14,90,21,91);
consGridBagConstraints5.ipadx = 28;
consGridBagConstraints5.gridy = 1;
consGridBagConstraints5.gridx = 0;
consGridBagConstraints4.insets = new java.awt.Insets(13,9,13,11);
consGridBagConstraints4.ipady = -96;
consGridBagConstraints4.ipadx = -207;
consGridBagConstraints4.fill = java.awt.GridBagConstraints.BOTH;
consGridBagConstraints4.weighty = 1.0;
consGridBagConstraints4.weightx = 1.0;
consGridBagConstraints4.gridy = 0;
consGridBagConstraints4.gridx = 0;
hirePanel.setLayout(new java.awt.GridBagLayout());
hirePanel.add(getHireScrollPane(), consGridBagConstraints4);
hirePanel.add(getBtnHire(), consGridBagConstraints5);
hirePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Potential New Hires", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
}
return hirePanel;
}
private javax.swing.JPanel getFirePanel() {
if(firePanel == null) {
firePanel = new javax.swing.JPanel();
java.awt.GridBagConstraints consGridBagConstraints1 = new java.awt.GridBagConstraints();
java.awt.GridBagConstraints consGridBagConstraints2 = new java.awt.GridBagConstraints();
java.awt.GridBagConstraints consGridBagConstraints3 = new java.awt.GridBagConstraints();
consGridBagConstraints1.insets = new java.awt.Insets(14,9,3,10);
consGridBagConstraints1.ipady = -254;
consGridBagConstraints1.ipadx = -18;
consGridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
consGridBagConstraints1.weighty = 1.0;
consGridBagConstraints1.weightx = 1.0;
consGridBagConstraints1.gridy = 0;
consGridBagConstraints1.gridx = 0;
consGridBagConstraints2.insets = new java.awt.Insets(4,9,6,10);
consGridBagConstraints2.ipady = -254;
consGridBagConstraints2.ipadx = -18;
consGridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
consGridBagConstraints2.weighty = 1.0;
consGridBagConstraints2.weightx = 1.0;
consGridBagConstraints2.gridy = 1;
consGridBagConstraints2.gridx = 0;
consGridBagConstraints3.insets = new java.awt.Insets(7,199,20,178);
consGridBagConstraints3.ipadx = 22;
consGridBagConstraints3.gridy = 2;
consGridBagConstraints3.gridx = 0;
firePanel.setLayout(new java.awt.GridBagLayout());
firePanel.add(getPersonalScrollPane(), consGridBagConstraints1);
firePanel.add(getFinancialScrollPane(), consGridBagConstraints2);
firePanel.add(getBtnFire(), consGridBagConstraints3);
firePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Current Employees", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
}
return firePanel;
}
} // @jve:visual-info decl-index=0 visual-constraint="10,10"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -