如何在 Java Swing 中创建简单的表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24193404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create simple table in Java Swing?
提问by Heisenberg
I am working on a project "Hospital Management System" in Java Swing. Now on the records page I want to show all stored records of patients in a table format. But I have never worked with JTable in swing. Now when I tried to fetch records in a simple program, it is printing all records on console in for loop but when I go for JFrame and tried to fetch records on JTable, instead of making a table, it's showing a error message like:
我正在 Java Swing 中开发一个项目“医院管理系统”。现在在记录页面上,我想以表格格式显示所有存储的患者记录。但我从未在 Swing 中使用过 JTable。现在,当我尝试在一个简单的程序中获取记录时,它会在 for 循环中在控制台上打印所有记录,但是当我使用 JFrame 并尝试在 JTable 上获取记录时,而不是创建一个表,它会显示如下错误消息:
Ex_test.java:51: cannot find symbol, symbol : constructor JTable(java.lang.String[],java.lang.String[][])
location: class javax.swing.JTable, JTable table=new JTable(column,data);
Can anyone tell me what is the problem in my code?
谁能告诉我我的代码有什么问题?
Database is MS-Access 2007.
数据库是 MS-Access 2007。
import javax.swing.*;
import javax.swing.JTable;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class Ex_test extends JFrame
{
public static void main(String[] args)
{
Ex_test ob=new Ex_test();
}
int i=0;
String column[];
String data[][];
//JTable table;
public Ex_test()
{
super("Array");
String[] id=new String[15];
String[] name=new String[15];
String[] contact=new String[15];
try
{
Connection con;
Statement st;
ResultSet rs;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:test");
st=con.createStatement();
rs=st.executeQuery("select * from test");
String column[]={"ID","NAME","CONTACT"};
while (rs.next())
{
id[i]=rs.getString("id");
name[i]=rs.getString("sname");
contact[i]=rs.getString("contact");
i++;
}
for (i=0;i<4 ;i++ )
{
System.out.println(""+id[i]+name[i]+contact[i]);
String data[][]={{id[i],name[i],contact[i] }};
}
}
catch (Exception e)
{
}
JTable table=new JTable(column,data);
setSize(1000,1000);
setVisible(true);
}
}
采纳答案by camickr
JTable table=new JTable(column,data); // wrong parameters
The constructor expects you to specify the data as the first parameter:
构造函数希望您将数据指定为第一个参数:
JTable table=new JTable(data, column);
Don't use Arrays for reading data from a database. You don't know how large to make the arrays. Instead use Vectors, since the DefaultTableModel will support Vectors.
不要使用数组从数据库中读取数据。您不知道制作阵列有多大。而是使用 Vectors,因为 DefaultTableModel 将支持 Vectors。
Check out the TableFromDatabaseExample.java
source code from Table From Databasefor some general code to get you started in populating a JTable with data from a database.
TableFromDatabaseExample.java
从Table From Database查看源代码以获得一些通用代码,以帮助您开始使用数据库中的数据填充 JTable。
Also, don't use an empty catch block on the SQL code. You should display the exception.
另外,不要在 SQL 代码上使用空的 catch 块。您应该显示异常。