开发者

My Jtable is blank

*SOLVED THANKS TO ALL WHO HELPED *

Hello my q is about a jtable that i have ,i fill it with components from a .txt ,I have a main (menu ) JFrame and by pressing a jbutton i want the jtable to pop out ! My problem is that my jtable is blank and it supposed to show some date !I would appreciated any help , Thanks. this is my ''reading'' class`

public static void main(String[] args) {
     company Company=new company();
    payFrame jframe=new payFrame(Company);
    jframe.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
    jframe.setSize(600,300);
    jframe.setVisible(true);
     readClass();
}
    //diavasma
  public s开发者_运维百科tatic void readClass(){
    ArrayList<Employee> emp =new ArrayList<Employee>() ;
    //Employee[] emp=new Employee[7];
    //read from file
 try {

   int i=0;
   // int rowCounter;
   // int payments=0;

String inputDocument = ("src/Employees.txt");


FileInputStream is = new FileInputStream(inputDocument);
Reader iD = new InputStreamReader(is);


BufferedReader buf = new BufferedReader(iD);
String inputLine;
while ((inputLine = buf.readLine()) != null) {
            String[] lineParts = inputLine.split(",");

            String email = (lineParts[7]);
            int EmpNo = Integer.parseInt(lineParts[0]);
            String type = lineParts[10];
            int PostalCode = Integer.parseInt(lineParts[5]);
            int phone = Integer.parseInt(lineParts[6]);
            int DeptNo = (short) Integer.parseInt(lineParts[8]);
            double Salary;
            int card = (short) Integer.parseInt(lineParts[10]);
            int emptype = 0;
             int hours=Integer.parseInt(lineParts[11]);
            if (type.equals("FULL TIME")) {
                emptype = 1;
            } else if (type.equals("SELLER")) {
                emptype = 2;
            } else {
                emptype = 3;
            }

            /**
             *  Creates employee instances depending on their type of employment
             *  (fulltime=1, salesman=2, parttime=3)
             */
            switch (emptype) {
                case 1:


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

                    i++;
                    break;

and this is my class where i make my Jtable and fill him

public class company extends JFrame {
    private ArrayList<Employee> emp = new ArrayList<Employee>();
    public void addEmployee(Employee emplo) {

    emp.add(emplo);
}

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

public  void getOption1() {

     ArrayList<Employee> employee = getArray();
    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 : employee) {
        model.addRow(new Object[]{current.getempCode(), current.getfirst(), current.getlast(),
                    current.getaddress(), current.getcity(), current.getpostalCode(),
                    current.gettelephone(), current.getemail(), current.getdep(),
                    current.getsalary(), current.getcardcode(), current.getHours()
                });

    }


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

    table.revalidate();


There's a lot that could be improved about your code; in particular you should learn the Java naming conventions (Company instead of company; company Company=new company(); is exactly backwards of how you should be declaring your variables).

Second, your class hierarchy doesn't make sense - is a company really a JFrame? No. You should have a Company class that holds the employees, and then a CompanyFrame class that extends the JFrame and has a reference to the Company object.

Third, you have not decomposed or isolated the problem. There are any number of reasons your table might be blank. Are you sure that your code reading the text file is working correctly? Are you sure that the list of Employee objects is created correctly? The easiest way to debug this stuff is to print out the state of your objects at various points in time and make assertions (e.g. assert employee.size() > 0 if you expect there to be more than one employee at a given point.)

Finally, this goes for general programming - you have to be able to crawl before you can run. In other words, don't start off trying to get everything working completely on the first pass. Before you deal with reading the employees from files, make sure you can get it working with some hardcoded employee objects that you create yourself, within code.

Edit:

One clue is that you read your employees in a public static void method:

public static void readClass(){
    ArrayList<Employee> emp =new ArrayList<Employee>() ;

where do you ever add those employees into the employee arraylist in your frame? You have an addEmployee method but it doesn't seem to be called

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜