数据未出现在 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();
}
}
I am new at using Jtable and I need some help. I want to write the following CODE to display data from queue in the table
a problem : the data does not appear in the table
i dont kow what is a problem !!
i need help , please
Source Code :
// class student
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;
}
}
// class node
public class node {
student info;
node link;
public node(student st,node next){
this.info = info;
this.link = link;
}
}
// class queue
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;
}
}
// class program
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两件事:
请使用大写字母来开始您的类名称
自定义 TableModel 可能是一个好主意。不要从头开始实现 TableModel,而是扩展 AbstractTableModel,因为它会处理所有事件和侦听器方面,并且会为您节省大量时间。
Two things:
Please use an uppercase letter to start your class names
A custom TableModel is probably a good idea. Instead of implementing TableModel from scratch, extend AbstractTableModel instead, since it handles all the events and listener aspects and will save you a lot of time.
您的类队列应该实现 TableModel 。然后使用以下命令创建表:
You're class queue should implement TableModel. Then create your table with:
我发现至少一个错误,不确定唯一的一个:
this.info = info;
根本没有做任何事情,试试这个:一些单元测试会很好地捕捉到这个......或者只是使用 Java 中的集合...
编辑:
其次,在 getData 中:
您没有在此处执行 x 的真实副本:
-
x.length
仍然为零- 您仍在使用原始节点,而不是副本。如果你执行
x.dequeue()
,原始节点就会改变-
x.dequeue
更改x.length
所以你的循环会中途停止最好只是导航队列:
EDIT2:
这样,只有在按下按钮时,表格才会更新...
按照其他答案中的建议,使用 TableModel 以便在每次队列更改时更新它。
我也不喜欢每次更新数据时都创建一个新的 JTable 和 JScrollPane 的想法...我更喜欢使用 JTable 的一个实例创建 GUI,然后在需要时更新该实例的模型。 (TableModel 会为你做到这一点)。
I found at least one error, not sure if the only one:
this.info = info;
is doing nothing at all, try this:Some unit tests would be great to catch this... or just use the collections from Java...
EDIT:
second, in getData:
you are not doing a real copy of x here:
-
x.length
is still zero- you are still using the original nodes, not a copy. If you do
x.dequeue()
the original nodes get changed-
x.dequeue
changesx.length
so your loop will stop halfwayBetter just navigate the queue:
EDIT2:
That way the table only will be updated when pressing the button...
Use a TableModel, as sugested in the other answers to have it updated every time the queue gets changed.
I also don't like the ideia of creating a new JTable and JScrollPane every time to update the data... I prefer to create the GUI with one instance of the JTable and just update that instance's model when needed. (The TableModel would do that for you).