动态添加的JTable不显示 [英] Dynamically added JTable not displaying
问题描述
Java新手在这里.我有一个添加到我的netbeans项目中的JFrame,并且向它添加了以下方法,该方法创建了一个JTable.问题是,由于某种原因,当我调用此方法时,未显示JTable.有什么建议吗?
Java Newbie here. I have a JFrame that I added to my netbeans project, and I've added the following method to it, which creates a JTable. Problem is, for some reason when I call this method, the JTable isn't displayed. Any suggestions?
public void showFromVectors(Vector colNames, Vector data) {
jt = new javax.swing.JTable(data, colNames);
sp = new javax.swing.JScrollPane(jt);
//NB: "this" refers to my class DBGridForm, which extends JFrame
this.add(sp,java.awt.BorderLayout.CENTER);
this.setSize(640,480);
}
在以下上下文中调用该方法:
The method is called in the following context:
DBGridForm gf = new DBGridForm(); //DBGridForm extends JFrame
DBReader.outMatchesTable(gf);
gf.setVisible(true);
...其中DBReader.outMatchesTable()定义为
... where DBReader.outMatchesTable() is defined as
static public void outMatchesTable(DBGridForm gf) {
DBReader ddb = new DBReader();
ddb.readMatchesTable(null);
gf.showFromVectors(ddb.lastRsltColNames, ddb.lastRsltData);
}
我的猜测是我正在忽略某些东西,无论是关于我正在使用的swing类,还是关于Java.有什么想法吗?
My guess is I'm overlooking something, either about the swing classes I'm using, or about Java. Any ideas?
推荐答案
"this"尚不清楚.它在小程序内吗?一个JFrame?
"this" in your context is unclear. Is it inside an applet? a JFrame?
您可能会遇到布局问题,请确保已使用新的borderlayout在类上调用了setLayout.
You may be having a layout issue, make sure you've called setLayout on your class with a new borderlayout.
在swing应用程序中,您希望使用getRootContentPane().add()而不是原始的add(),具体取决于版本.
In a swing application, you'd want to use getRootContentPane().add() instead of a raw add(), depending on the version.
有关添加顶级内容的Java教程: http: //java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html
Java tutorial on adding top-level content: http://java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html
这篇关于动态添加的JTable不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!