你好朋友这是我的程序,一切都很酷。 但是我无法在JTable顶部显示列名。我应该做些什么改变来展示它?并告诉我如何给这个表格滚动条(垂直,水平)。
提前致谢..
Report1.Java:
package com.ash;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.spi.DirStateFactory.Result;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
public class Report1 extends JFrame {
Object[][] data = null;
private JPanel contentPane;
/**
* Launch the application.
*/
ResultSet rs,rs1;
Statement stmt=null;
int k=0;
PreparedStatement pr;
public JTable table;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Report1 frame = new Report1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Report1() {
setTitle("Report");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 953, 616);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
table = new JTable();
table.setBounds(100, 136, 810, 431);
contentPane.add(table);
JButton btnNewButton = new JButton("Add");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
NewPatient.con=DbConnPa.getConObj();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
String sql="SELECT COUNT (*) FROM newpatient1";
try {
pr=NewPatient.con.prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
rs=pr.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
rs.next();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int count = 0;
try {
count = rs.getInt(1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(count);
data= new Object [count][6];
try {
stmt=NewPatient.con.createStatement();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
String sql1="SELECT * FROM newpatient1";
Object[] columns={"FirstName","LastName","Age","RefDoctor","Sex","RegId"};
try {
rs1=stmt.executeQuery(sql1);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while(rs1.next()){
for(int j=0;j<6;j++){
try {
data[k][j]=rs1.getString(j+1);
System.out.println("i="+"j="+j);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
k++;}
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
table.setModel(new DefaultTableModel(data, columns));
}
});
btnNewButton.setBounds(382, 47, 89, 23);
contentPane.add(btnNewButton);
}
}
答案 0 :(得分:4)
让我们开始讨论那些吓坏我的事情......
// This will come back and haunt you...
contentPane.setLayout(null);
table = new JTable();
// You should leave this up to the layout manager, but you're not using one, so...
table.setBounds(100, 136, 810, 431);
现在问题......
预计表格会比可用的可见区域大......
contentPane.add(table);
不是它的工作方式。
您应该使用JScrollPane
。这提供了显示标题的区域......
contentPane.add(new JScrollPane(table));
结帐How to use tables和How to use scroll panes
最终,您将要熟悉Concurrency in Swing,因为从数据库中读取结果可能是一个长时间运行的任务,导致应用程序停止
使用示例更新
我还通过了你的数据重试代码并“清理了它”,这很有趣......
public Report1() {
setTitle("Report");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 953, 616);
((JComponent) getContentPane()).setBorder(new EmptyBorder(5, 5, 5, 5));
table = new JTable();
add(new JScrollPane(table));
JButton btnNewButton = new JButton("Add");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
PreparedStatement pr = null;
ResultSet rs = null;
Statement stmt = null;
try {
NewPatient.con = DbConnPa.getConObj();
String sql = "SELECT COUNT (*) FROM newpatient1";
pr = NewPatient.con.prepareStatement(sql);
rs = pr.executeQuery(sql);
if (rs.next()) {
int count = 0;
count = rs.getInt(1);
System.out.println(count);
rs.close();
data = new Object[count][6];
stmt = NewPatient.con.createStatement();
String sql1 = "SELECT * FROM newpatient1";
Object[] columns = {"FirstName", "LastName", "Age", "RefDoctor", "Sex", "RegId"};
rs = stmt.executeQuery(sql1);
while (rs.next()) {
for (int j = 0; j < 6; j++) {
data[k][j] = rs1.getString(j + 1);
System.out.println("i=" + "j=" + j);
}
k++;
}
}
table.setModel(new DefaultTableModel(data, columns));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rs.close();
} catch (Exception e) {
}
try {
pr.close();
} catch (Exception e) {
}
try {
stmt.close();
} catch (Exception e) {
}
}
}
});
add(btnNewButton, BorderLayout.SOUTH);
}