gpt4 book ai didi

java - 数据未显示在我的 JTable 中

转载 作者:行者123 更新时间:2023-12-02 00:10:25 27 4
gpt4 key购买 nike

滚动条和按钮将被绘制,但表格中的单元格不会被绘制,并且控制台已正确打印数据,所以我想知道我哪里做错了。

public class MyGUIManager extends JPanel {

protected JFrame frame;
protected JScrollPane scrollPane=null;
protected JTable table=null;
protected TableModel model=null;
protected JButton btnInsert=null;

protected String dbname,tblname;
protected Vector<String> colNames;
protected Vector<Vector<String>> data,backup;
protected int col;

protected MySQLGUIManager() throws Exception{

frame =new JFrame("MySQL Manager");
frame.setContentPane(this);
frame.setMinimumSize(new Dimension(800,600));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
init();
}

protected void init() throws Exception{
//some inrrelevant operations about database
loadTbl();
}

protected void loadTbl() throws SQLException{
table=new JTable(data,colNames){
private static final long serialVersionUID = 1L;
public void tableChanged(TableModelEvent e){
int column=e.getColumn();
assert e.getFirstRow()==e.getLastRow():"more than 1 row have been changed!";
int row=e.getFirstRow();
if(scrollPane!=null){
//some codes
}
}
};

model=table.getModel();
scrollPane=new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(scrollPane,BorderLayout.CENTER);
btnInsert=new JButton("Insert a row");
add(btnInsert,BorderLayout.SOUTH);
repaint();
revalidate();
// scrollPane.repaint();
// scrollPane.revalidate();
System.out.println(model.getColumnCount()+" "+model.getRowCount());
for(int i=0;i<data.size();i++){
for(int j=0;j<data.get(0).size();j++){
System.out.print(model.getValueAt(i, j)+" ");
}
System.out.println();
}
}

public static void main(String[] args) throws Exception {
new MySQLGUIManager();
}

}

(省略了一些代码,但与 GUI 无关。)提前致谢!

最佳答案

  • 数据/列模型中未添加任何内容
  • 数据和列模型未初始化
  • JPanel 中创建 JFrame 只是为了将其自身添加到框架中是不好的做法

您没有从 tableChanged 方法内部调用 super.tableChanged

table = new JTable(data, colNames) {
private static final long serialVersionUID = 1L;

public void tableChanged(TableModelEvent e) {
super.tableChanged(e);
int column = e.getColumn();
assert e.getFirstRow() == e.getLastRow() : "more than 1 row have been changed!";
int row = e.getFirstRow();
if (scrollPane != null) {
//some codes
}
}
};

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

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