gpt4 book ai didi

java - 从查询中填充 Jtable

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

当我想填充 Object[] [] 时遇到问题:每个想法都很好:

我提交了代码来向您展示问题

当我想填充 Object[] [] 时遇到问题:每个想法都很好:我提交了代码以向您展示问题

String[] columnNames = {"First Name"};

Object[][] data = {
{"Kathy"}
};

JButton btnConsulter = new JButton("Consulter");
btnConsulter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

int i=0;
// columnNames
String sql="SELECT DISTINCT idlig FROM `horaire` WHERE nomstation='"+ comboBox.getSelectedItem() +"'";
ds.Extraction(sql);
try {
while(ds.getRs().next()){
columnNames[i]=ds.getRs().getString(1);
//System.out.println(columnNames[i]);
i++;

}
//here I want : for every column fill data
int b=0,j=0;
for(j=0;j<columnNames.length;j++){
sql="SELECT heure FROM `horaire` WHERE idlig="+ columnNames[j]+" AND `nomstation`='"+ comboBox.getSelectedItem() +"'";
System.out.println(sql);
ds.Extraction(sql);

while(ds.getRs().next()){
data[b][j]=ds.getRs().getString(1); // this is the probleme
System.out.println(b+" "+ds.getRs().getString(1)); // its work good with simple display
b++;
}
}
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(62, 115, 457, 113);
contentPane.add(scrollPane);
table = new JTable(data, columnNames);
scrollPane.setViewportView(table);

}catch(Exception e){
JOptionPane.showMessageDialog(null, "chargement station "+e, "Erreur", JOptionPane.ERROR_MESSAGE);
}
}
});
btnConsulter.setBounds(468, 22, 89, 23);
contentPane.add(btnConsulter);

最佳答案

发现您的代码中存在一些问题,也许是因为您遇到了错误

首先:您需要将JTable添加到JScrollPane,然后将JScrollPane添加到JPanel 但你这样做:

JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(62, 115, 457, 113);
contentPane.add(scrollPane);
table = new JTable(data, columnNames);
scrollPane.setViewportView(table);

您也可以通过 调用 rePaint()reValidate() 中的方法来使用这种方式强>JPanel。

关于java - 从查询中填充 Jtable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27652054/

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