gpt4 book ai didi

java - JTable 中未显示 ColumnName

转载 作者:行者123 更新时间:2023-12-01 18:49:52 26 4
gpt4 key购买 nike

friend 们大家好,这是我的程序,一切正常。但我无法在 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);


}
}

最佳答案

让我们从所有让我害怕的事情开始......

// 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。这提供了要显示的标题区域...

enter image description here

contentPane.add(new JScrollPane(table));

查看How to use tablesHow 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);
}

关于java - JTable 中未显示 ColumnName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16207329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com