2009-12-04 116 views
0

我是使用Jtable的新手,需要一些帮助。我想写下面的代码从队列中表JTable中未显示数据

一个问题显示数据:数据不会出现在表格中

我不叩头什么是一个问题!

我需要帮助,请

源代码:

//类学生

public class student { 
    int id,age; 
    String fn,ln; 

    public student(int id,String fn,String ln,int age){ 
     this.age = age; 
     this.id = id; 
     this.fn = fn; 
     this.ln = ln; 
    } 
} 

//类节点

public class node { 
    student info; 
    node link; 
    public node(student st,node next){ 
     this.info = info; 
     this.link = link; 
    } 

} 

//类队列

import javax.swing.*; 

public class Queue{ 
    node front ,rear; 
    int length; 

    public void enqueue(student x){ 
     node newnode=new node(x,null); 
     if(front==null) 
      front=rear=newnode; 
     else{ 
      rear.link=newnode; 
      rear=newnode; 
     }length++; 
    } 


    public student dequeue(){ 
     student temp=front.info; 
     if(front==null && rear==null){ 
      throw new RuntimeException("empty"); 
     }else{ 

      front=front.link; 
      if(front==null) 
      rear=null; 

      length--; 
     }return temp; 
    } 


    public String[][] getData(){ 
      Queue x= new Queue(); 
      x.front= front; 
      x.rear = rear; 
      String s[][] = new String[x.length][4]; 
     if(x.front==null){ 
     JOptionPane.showMessageDialog(null,"the Queue is empty,you must add new student befor");  
     }else{ 

      student tmp; 
      for(int i=0;i<x.length;i++){ 

       try{ 
       tmp = x.dequeue(); 
       s[i][0] = tmp.id + " "; 
       s[i][1] = tmp.fn; 
       s[i][2] = tmp.ln; 
       s[i][3] = tmp.age + " "; 

       }catch(Exception e){ 
       System.out.println("Exception from getData"); 
       } 
      } 
     } 
     return s; 
    } 

} 

//类节目

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class program extends JFrame implements ActionListener{ 

    Container c; 

    JTextField txtID,txtFN,txtLN,txtAge; 
    JLabel lblTitle,lblID,lblFN,lblLN,lblAge; 
    JButton btnAdd,btnUpdate,btnDelet,btnPrint,btnSort,btnRefresh,btnCancel; 
    JTable table; 

    String data[][]; 
    Queue q; 

    public program(){ 
     c = getContentPane(); 
     setTitle(" Student Applicaion "); 
     c.setLayout(null); 
     q = new Queue(); 

     // add lbl 
     lblTitle = new JLabel("ADD NEW STUDENT"); 
     lblTitle.setFont(new Font ("Helvetica", Font.PLAIN, 20)); 
     lblTitle.setBounds(50,20,200,20); 
     c.add(lblTitle); 


     lblID = new JLabel("ID"); 
     lblID.setBounds(50,50,70,20); 
     c.add(lblID); 

     lblFN = new JLabel("First Name"); 
     lblFN.setBounds(50,80,70,20); 
     c.add(lblFN); 

     lblLN = new JLabel("Last Name"); 
     lblLN.setBounds(50,110,70,20); 
     c.add(lblLN); 

     lblAge = new JLabel("Age"); 
     lblAge.setBounds(50,140,70,20); 
     c.add(lblAge); 

     // add txt 
     txtID = new JTextField(); 
     txtID.setBounds(130,50,120,20); 
     c.add(txtID); 

     txtFN = new JTextField(); 
     txtFN.setBounds(130,80,120,20); 
     c.add(txtFN); 

     txtLN = new JTextField(); 
     txtLN.setBounds(130,110,120,20); 
     c.add(txtLN); 

     txtAge = new JTextField(); 
     txtAge.setBounds(130,140,120,20); 
     c.add(txtAge); 

     // add btn 
     btnAdd = new JButton("Add"); 
     btnAdd.setBounds(90,180,70,25); 
     c.add(btnAdd); 

     btnRefresh = new JButton("Refresh"); 
     btnRefresh.setBounds(165,180,80,25); 
     c.add(btnRefresh); 



     btnUpdate = new JButton("Update"); 
     btnUpdate.setBounds(300,50,100,20); 
     c.add(btnUpdate); 

     btnDelet = new JButton("Delet"); 
     btnDelet.setBounds(300,80,100,20); 
     c.add(btnDelet); 

     btnPrint = new JButton("Print"); 
     btnPrint.setBounds(300,110,100,20); 
     c.add(btnPrint); 

     btnSort = new JButton("Sort"); 
     btnSort.setBounds(300,140,100,20); 
     c.add(btnSort); 

     btnCancel= new JButton("Cancel"); 
     btnCancel.setBounds(355,435,80,25); 
     c.add(btnCancel); 

     // add table 
    // print(); 


     btnAdd.addActionListener(this); 
     btnUpdate.addActionListener(this); 
     btnDelet.addActionListener(this); 
     btnPrint.addActionListener(this); 
     btnSort.addActionListener(this); 
     btnRefresh.addActionListener(this); 
     btnCancel.addActionListener(this); 

     setSize(450, 500); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(EXIT_ON_CLOSE);  
     setVisible(true); 
    } 

     public void actionPerformed(ActionEvent e){ 
     if(e.getSource() == btnCancel) 
      System.exit(0); 
     else if(e.getSource() == btnRefresh){ 
      txtAge.setText(""); 
      txtFN.setText(""); 
      txtID.setText(""); 
      txtLN.setText(""); 
     } 
     else if(e.getSource() == btnAdd){ 
      if(isThreeDigit(txtID.getText().trim()) == false || isNumber(txtID.getText().trim()) == false) 
       JOptionPane.showMessageDialog(null,"you entered invalid ID"); 
      else if(isCapital(txtFN.getText().trim()) == false) 
       JOptionPane.showMessageDialog(null,"you must begin First name with capital Character"); 
      else if(!isString(txtFN.getText().trim()) || !(isString(txtLN.getText().trim()))) 
       JOptionPane.showMessageDialog(null,"You can't enter number on your name"); 
      else if(checkAge(txtAge.getText().trim()) == false) 
       JOptionPane.showMessageDialog(null,"You must enter real age in numbers format"); 
      else{ 

       student st = new student(Integer.parseInt(txtID.getText().trim()),txtFN.getText(), 
       txtLN.getText(),Integer.parseInt(txtAge.getText().trim())); 
       q.enqueue(st); 


      // print(); 

      } 
     } 
     else if(e.getSource() == btnPrint){ 
      print(); 

     } 

     } 

     public void print(){ 
      String col[] = {"ID","FName","LName","Age"}; 
      data = q.getData(); 
      table = new JTable(data,col); 
     // table.editingStopped(ChangeEvent e) ; 
      JScrollPane scrollPane = new JScrollPane(table); 
      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
      scrollPane.setBounds(10,230,420,200); 
      c.add(scrollPane); 


     } 
//~~~~~~~~~~~~ Constraints 

    public boolean isThreeDigit(String id){ 
     if(id.length() <= 3) 
      return true; 


     return false; 
    } 
    public boolean checkAge(String age){ 

      if(!isNumber(age) || Integer.parseInt(age) > 90 || Integer.parseInt(age) < 6) 
       return false; 

       return true; 
    } 
     public boolean isNumber(String id){ 

      for(int i=0;i<id.length();i++){ 
      char c=id.charAt(i); 
      if(c>'9'|| c<'0') 
       return false; 
       } 
       return true; 
     } 
    public boolean isString(String id){ 

     for(int i=0;i<id.length();i++){ 
      char c=id.charAt(i); 
      if(c<='9'|| c<='0') 
       return false; 
       } 
       return true; 

    } 

    public boolean isCapital(String FN){ 
     char c=FN.charAt(0); 
     if(c>='A'&& c<='Z') 
     return true; 

     return false; 
    } 


    public static void main (String[] args) { 
     new program(); 
} 
} 
+0

如果这是家庭作业,请标记为这样。 –

回答

1

你等级队列应实现TableModel

Queue q= new Queue(); 
JTable table= new JTable(q); 
+1

这不是很难做到,只是记得调用fire *方法,它的工作方式就像* magic * –

3

两件事情:然后创建表

  1. 请使用大写字母开始你的类名

  2. 自定义的TableModel可能是一个好主意。而不是从头开始实施TableModel,而是扩展AbstractTableModel,因为它处理所有事件和侦听器方面,并且会为您节省大量时间。

0

我发现至少有一个错误,不知道,如果只有一个:

public class node { 
    student info; 
    node link; 
    public node(student st,node next){ 
     this.info = info; 
     this.link = link; 
    } 
    } 

this.info = info;做什么都没有,试试这个:

public class node { 
    student info; 
    node link; 
    public node(student st,node next){ 
     this.info = st; 
     this.link = next; 
    } 
} 

一些单元测试会很好的抓住这个......或者只是使用Java的藏品...

编辑:
秒中的getData:

 public String[][] getData(){ 
      Queue x= new Queue(); 
      x.front= front; 
      x.rear = rear; 
      String s[][] = new String[x.length][4]; 
... 
      student tmp; 
      for(int i=0;i<x.length;i++){ 

        try{ 
        tmp = x.dequeue(); 

你是不是做x的实副本的位置:
- x.length仍然是零
- 你还在使用原始的节点,而不是一个副本。如果你x.dequeue()原来的节点得到改变
- x.dequeue改变x.length所以你的循环会中途停止

更好

只是导航队列:

public String[][] getData(){ 
    String s[][] = new String[length][4]; 
    if (front == null) { 
     JOptionPane.showMessageDialog(null,"the Queue is empty,you must add new student befor");   
    } else { 
     int i = 0; 
     // node node = front; // that's why class name should be uppercase!! 
     node aNode = front; 
     while (aNode != null) { // for would also do... 
      student tmp = aNode.info; 
      s[i][0] = String.valueOf(tmp.id); // + " "; 
      s[i][1] = tmp.fn; 
      s[i][2] = tmp.ln; 
      s[i][3] = String.valueOf(tmp.age); // + ""; 
      i += 1; 
      aNode = aNode.link; 
     } 
    } 
    return s; 
} 

EDIT2:
这样的表仅会在按下按钮时更新...
使用TableModel,与其他答案中的推理一样,每当队列发生变化时都要更新它。
我也不喜欢每次更新数据时创建新的JTable和JScrollPane的想法......我更喜欢用一个JTable实例创建GUI,并在需要时更新该实例的模型。 (TableModel会为你做这件事)。

+0

oooh,看起来我在没有焦点的情况下工作 – wasim

+0

我试图不改变大部分代码/逻辑。 .. –

+0

谢谢亲爱的..我非常感谢你.. 我也不喜欢创建一个新的JTable和JScrollPane每次更新数据的想法...我这样做,因为我不知道怎么可以添加新的行..当添加新的学生! – wasim