ここは私のクラス全体です。私はテキストファイルからデータを読んで、それらをaeeaylistに入れます。具体的な方法はcalled.Butあるとき、その配列リストから私は、JTableの上のデータを表示したい、私は別のJFrameクラスに位置するJButtonから()showOptionAを呼び出して何も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);
}
}
を示しdoesntのです。
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を()に変更した後、ちょうど
public static void showOptionA(){
Company gui =new Company();
gui.getOptionA();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.setSize(600, 400);
}
(クラスPayrollFormに配置されている)、showEmployeesActionPerformed内で呼び出す場合、私は今、(空の)列を参照してくださいが、データのありませんJTableの(またはそのモデル)には何も追加した後
私は、私は会社のクラスでsetCompanyメソッドを作成し、showEmployeesActionPerformedからそのメソッドを呼び出す必要があることを理解すれば? それともそれ以外の方法ですか? 私は、Companyのメソッドにアクセスするために、showEmployeesActionPerformedのメソッドでCompanyクラスのオブジェクトを作成しました – George
私はこれも試しました:public void showOptionA()をpublic static void main(String args [])に切り替え、 Companyクラスの内部からのみ円滑に実行されますが、再び列名のみが表示されます。テーブルは空として動作しますか? – George
いいえ、CompanyクラスにsetCompanyは必要ありません。代わりに、Companyオブジェクトのメソッドを呼び出す他の外部クラスにsetCompanyクラスが必要です。こうすることで、これらの他のクラスが表示されている* right * Companyオブジェクトのメソッドを呼び出すことができます。 –