gpt4 book ai didi

java - 使用 mysql 填充 JTable

转载 作者:行者123 更新时间:2023-11-29 08:14:53 37 4
gpt4 key购买 nike

我正在制作这个小程序,从头开始显示 mysql 数据库中的信息(无需拖放)。好的,所以我尝试使用 NetBeans 和拖放方法练习如何使用 mysql 数据库中的信息填充 JTable,效果非常好。但是当我对它进行硬编码时(如果这就是术语),它就不起作用了。这是我的代码。抱歉,如果它很乱或者什么,我对此仍然是菜鸟。

    import java.sql.*;
import java.awt.Dimension;
import javax.swing.*;
import net.proteanit.sql.DbUtils;
public class Main extends JFrame{

Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;



public JLabel label1,label2,label3,background;
public JButton button1 , button2 , button3;
public JTable table = new JTable();

//query for updating the JTable

public void UpdateJTable(){
String sql = "SELECT name , TypeDebt , amount , DateDebt,Due_Date from btable";
try{
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
}catch(Exception xe){
JOptionPane.showMessageDialog(null, "error");

}
}

//WindowEvent code
private void formWindowOpened(java.awt.event.WindowEvent evt){
conn = sqlconnection.ConnectDb();
UpdateJTable();
}
//constructor to be called in the main method
public Main(){
init();
}

//method for the GUI of the program
public void init(){


// Images for the buttons
ImageIcon img = new ImageIcon("C:\\Users\\janyjan\\Pictures\\addbutton.jpg");
ImageIcon img2 = new ImageIcon("C:\\Users\\janyjan\\Pictures\\searchbutton.jpg");
ImageIcon img3 = new ImageIcon("C:\\Users\\janyjan\\Pictures\\deletebutton.jpg");

// frame components/attributes
setLayout(null);
getContentPane();
setTitle("DEBT LIST");
setResizable(false);
JLabel background=new JLabel(new ImageIcon("C:\\Users\\janyjan\\Pictures\\background.jpg"));
background.setBounds(0,0,700,600);
add(background);

setVisible(true);
setSize(700,600);
getContentPane();
setLocationRelativeTo(null);

addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
//Button attributes - Location and Designs
button1 = new JButton();
button1.setBounds(30,130,150,30);
button1.setIcon(img);
background.add(button1);

button2 = new JButton();
button2.setBounds(30,90,150,30);
button2.setIcon(img2);
background.add(button2);

button3 = new JButton();
button3.setBounds(30,170,150,30);
button3.setIcon(img3);
background.add(button3);



//Jtable attribute and location
JTable table = new JTable();
table.setModel(new javax.swing.table.DefaultTableModel(
new Object [][]{
{null , null , null , null , null},
{null , null , null , null , null},
{null , null , null , null , null},
{null , null , null , null , null},
{null , null , null , null , null},
{null , null , null , null , null},
{null , null , null , null , null},
{null , null , null , null , null},
{null , null , null , null , null}
},
new String []{
"name" , "TypeDebt" , " amount " , "DateDebt" , "Due_Date"
}
));

table.setPreferredScrollableViewportSize(new Dimension(300,400));
table.setSelectionBackground(getBackground());

JScrollPane scrollpane = new JScrollPane(table);

scrollpane.setBounds(200, 60, 490, 490);
background.add(scrollpane);





}



// Main Method
public static void main(String args[]){

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new Main();

}
});
}//end of main method
}//end of class Main

最佳答案

除了使用 null 布局(这会带来无穷无尽的乐趣和喜悦)之外,您还会隐藏您的 table 变量...

基本上,您将 table 声明为实例变量...

public class Main extends JFrame {

//...
public JTable table = new JTable();

//query for updating the JTable
public void UpdateJTable() {

然后,您在 init 方法中将其重新声明为局部变量

public void init() {
//...
//Jtable attribute and location
JTable table = new JTable();
table.setModel(new javax.swing.table.DefaultTableModel(
new Object[][]{
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null}
},
new String[]{
"name", "TypeDebt", " amount ", "DateDebt", "Due_Date"
}
));

table.setPreferredScrollableViewportSize(new Dimension(300, 400));
table.setSelectionBackground(getBackground());

JScrollPane scrollpane = new JScrollPane(table);

scrollpane.setBounds(200, 60, 490, 490);
background.add(scrollpane);

这意味着当您调用 UpdateJTable 方法时,您实际上是在与实例变量交互,而不是与屏幕上的对象交互。

看一下:

关于java - 使用 mysql 填充 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20830437/

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