2010-12-17 80 views
3

這是我的全班同學。我從文本文件中讀取數據,並將它們放入一個aeeaylist。然後從該陣列列表我要顯示在一個JTable,數據時的具體方法是called.But是犯規從位於另一個JFrame類一個JButton顯示任何東西JTable不顯示任何內容?

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* @author George 
*/ 
import java.awt.*; 
import java.util.ArrayList; 
//import java.io.FileInputStream; 
//import java.io.FileNotFoundException; 
//import java.io.EOFException; 
//import java.io.IOException; 
//import java.io.ObjectInputStream; 
/*import java.io.File; 
import java.lang.IllegalStateException; 
import java.util.NoSuchElementException; 
import java.util.Scanner; 
import javax.swing.JOptionPane;*/ 
import java.io.*; 
//import java.util.Locale; 
import javax.swing.*; 
import javax.swing.table.DefaultTableModel; 



public class Company extends JFrame { 
    private ArrayList<Employee> emp=new ArrayList<Employee>(); 


//Employee[] list=new Employee[7]; 



public void getEmployees(Employee emplo){ 

    emp.add(emplo); 
} 


/*public void openTxt(){ 
    try { 
     Scanner input=new Scanner(new File("Employees.txt")); 
    } 
    catch(FileNotFoundException e){ 
     JOptionPane.showMessageDialog(null, "File Not Found."); 
     System.exit(1); 
    } 
    }*/ 

public void doRead() throws Exception{ 
      //ArrayList<Employee> emp=new ArrayList<Employee>() ; 
     //Employee[] emp=new Employee[7]; 
     //read from file 
     File data = new File("src/Employees.txt"); 
     BufferedReader read = new BufferedReader(new FileReader(data)); 
     String input; 
     int i = 0; 
     //int salesmen = 0; 
     while ((input = read.readLine()) != null) { 
      String [] lineParts = input.split(","); 

      /** 
      * the following block converts some of the strings inputted to 
      * the appropriate vartypes. 
      */ 

      String EmpNo=(lineParts[0]); 
      String type=lineParts[10]; 
      String PostalCode = (lineParts[5]); 
      int phone = Integer.parseInt(lineParts[6]); 
      short DeptNo = (short) Integer.parseInt(lineParts[8]); 
      double Salary; 
      short card = (short) Integer.parseInt(lineParts[11]); 
      int dtype=0; 

      if(type.equals("FULL TIME")){ 
       dtype=1; 
      } 
      else if(type.equals("SELLER")){ 
       dtype=2; 
      } 
      else 
       dtype=3; 

      /** 
      * Creates employee instances depending on their type of employment 
      * (fulltime=1, parttime=3, salesman=2) 
      */ 
      switch (dtype) { 
       case 1 : 
        //empNo,firstname, lastname, address, city, PostalCode, phone, 
        //email, deptcode,Jobtype, salary, TimecardId, hoursW 

        Salary = Double.parseDouble(lineParts[10]); 
        emp.add(new FullTimeEmployee(EmpNo,lineParts[1], lineParts[2], lineParts[3], 
          lineParts[4], PostalCode, phone, 
          lineParts[7], DeptNo,type,Salary, card, 0.0)); 

        i++; 
        break; 
       case 2 : 
        Salary = Double.parseDouble(lineParts[10]); 
        ArrayList<Orders> orders=new ArrayList<Orders>(); 
        Salary = Double.parseDouble(lineParts[10]); 
        emp.add(new Salesman(EmpNo,lineParts[1], lineParts[2], lineParts[3], 
          lineParts[4], PostalCode, phone, 
          lineParts[7], DeptNo,type,Salary, card, 0.0, orders)); 
        i++; 
        break; 
       case 3 : 
        Salary = Double.parseDouble(lineParts[10]); 
        emp.add(new PartTimeEmployee(EmpNo,lineParts[1], lineParts[2], lineParts[3], 
          lineParts[4], PostalCode, phone, 
          lineParts[7], DeptNo,type,Salary, card, 0.0)); 

        i++; 
        break; 
       default : 
        break; 
      } 


     } 

} 
public ArrayList<Employee> getArray(){ 
    return emp; 
} 

//test methodos gia tin proti epilogi-den deixnei tipota omws sto JTable ???? 
public /*JTable */ void getOptionA(){ 
    ArrayList<Employee> list=getArray(); 
    /*String[] columnNames = {"Code","First Name","Last Name","Address","Cisty","Postal Code","Phone","Email", 
           "Dept Code","Salary","Time Card","Hours"};*/ 
    /* Object[][] data; 
    */ 
    JTable table = new JTable(); 
DefaultTableModel model = new DefaultTableModel(); 
table.setModel(model); 
model.setColumnIdentifiers(new String[] {"Code","First Name","Last Name","Address","City","Postal Code","Phone","Email", 
           "Dept Code","Salary","Time Card","Hours"}); 
    for(Employee current : list){ 
     model.addRow(new Object[] {current.getCode(),current.getName(),current.getSurname(), 
            current.getAddress(),current.getCity(),current.getTK(), 
            current.getPhone(),current.getMail(),current.getDeptCode(), 
            current.getSalary(),current.getCard(),current.getHours() 
     }); 

    } 

    /*JScrollPane scrollPane = new JScrollPane(table); 
    table.setFillsViewportHeight(true);*/ 
    //return table; 
    table.setPreferredScrollableViewportSize(new Dimension(500,50)); 
    table.setFillsViewportHeight(true); 
    JScrollPane scrollPane = new JScrollPane(table); 
    add(scrollPane); 



} 
public void showOptionA(){ 
    getOptionA(); 
    Company gui =new Company(); 
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    gui.setVisible(true); 
    gui.setSize(600, 400); 

} 

} 

我請showOptionA()。

private void showEmployeesActionPerformed(java.awt.event.ActionEvent evt) {            
    Results showEmp=new Results(); 
    //showEmp.setVisible(true); 
    //showEmp.setOptions(1); 
    Company company=new Company(); 
    /*JTable table=company.getOptionA(); 
    JScrollPane scrollPane = new JScrollPane(table); 
table.setFillsViewportHeight(true); 
scrollPane.setViewportView(table); 
table.setVisible(true);*/ 
    company.showOptionA(); 
} 

基本上我有不同的選項的「主」的JFrame,和每個按鈕,代表不同的選項,從調用公司類適當的選項的方法。

當我點擊按鈕「顯示員工狀態」。我希望它顯示上面的JTable。相反,一個新的框架打開,但是空白?

編輯:如果我改變showOptionA()靜態,然後只是把它裏面showEmployeesActionPerformed,(它位於類PayrollForm)

public static void showOptionA(){ 

    Company gui =new Company(); 
      gui.getOptionA(); 
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    gui.setVisible(true); 
    gui.setSize(600, 400); 

} 

我現在看到的列,但沒有數據(空)

回答

2

這與調用revalidate沒有任何關係,正如另​​一個人所建議的那樣,並且可能與調用錯誤對象上的方法有關。在您的showEmployeesActionPerformed方法中,您創建一個新的公司對象,一個不可視化的對象。關鍵是在可視化的GUI上調用這個方法。您可以通過將可視化GUI對象的引用傳遞給想要調用其方法的類來完成此操作。這可以通過setCompany方法完成:

setCompany(Company company) { 
    this.company = company); 
} 

或通過構造函數參數。

+0

所以如果我明白我必須在公司類中創建一個setCompany方法,然後從showEmployeesActionPerformed調用該方法? 還是相反? 我在showEmployeesActionPerformed的方法中創建了類Company的對象,以獲得對Company公司方法的訪問 – George 2010-12-17 15:36:43

+0

我也嘗試過:我將public void showOptionA()轉換爲public static void main(String args []),以查看它是否會只從公司類內部平穩地運行,但是隻有列名出現。表格表現爲空? – George 2010-12-17 16:03:53

+0

不,您不需要公司班級中的setCompany。相反,您需要在任何其他外部類中調用公司對象方法的setCompany類。通過這種方式,您可以確保這些其他類正在調用正確顯示的公司對象上的方法。 – 2010-12-17 18:36:08

0

向JTable(或其模型)添加任何內容後調用revalidate()。

+0

你爲什麼推薦這個?只要您激發正確的更改方法以通知表模型的偵聽器,就不需要在添加或更改模型後調用重新驗證 - 即使使用DefaultTableModel,這也不是必需的。 – 2010-12-17 14:22:41

+0

同意在更新模型後不需要調用revalidate()。但是,代碼確實會創建一個新表格和滾動窗格,然後將該滾動窗格添加到框架。所以是的,你需要在向框架添加滾動窗格後重新驗證。所以正確的想法,錯誤的原因,當然假設該方法被執行並且TableModel實際上被重新創建。 – camickr 2010-12-17 16:25:08

1

我想原因是你的方法showOptionA():

首先你把你的JTable和模型你的公司對象,這是你的框架。但緊隨其後,您將創建一個新的公司對象,設置所需的框架設置並顯示該對象,而不是您的第一個公司對象,即表所在的位置。

你可以離開gui,直接在Company對象上設置DefaultCloseOperation,並將其設置爲true。

其他一些建議: 您還應該設置幀的大小,然後將其設置爲可見。 而且吸氣劑通常只是放棄相關的對象,而不是放在某個列表上。 可能會有更多的錯誤,但它至少應該顯示你的表。

0

好的問題解決!問題出在getOptionA()方法中。 我設置了Jtable的模型,但不是可見的選項。這就是爲什麼它顯得空虛。我通過移動來糾正這個問題

try { 
    //try to read from text file 
    doRead(); 
    } 
    catch(Exception e){ 
     JOptionPane.showMessageDialog(null, "An Exception has Occured! The application will now close."); 
     System.exit(0); 
    } 

table.revalidate(); 


    add(scrollPane); 
    setVisible(true); 
    setSize(600, 400); 

up,from showOptionA,up to getOptionA()method。通過這樣做ShowOptionA變得無用(因此我刪除了它)。現在我從showEmployeesActionPerformed調用getOptionA,並且它全部都OK:)。感謝所有回覆我的消息的人,並特別感謝氣墊船Full Of Eels。他幫我取得了不錯的成績,爲什麼桌子沒有按照我想要的方式出現