动态添加的JTable不显示

发布于 2024-07-05 03:26:37 字数 938 浏览 4 评论 0原文

Java 新手在这里。 我有一个 JFrame,已添加到我的 netbeans 项目中,并向其中添加了以下方法,该方法创建了一个 JTable。 问题是,由于某种原因,当我调用此方法时,JTable 没有显示。 有什么建议么?

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);
}

该方法在以下上下文中调用:

DBGridForm gf = new DBGridForm(); //DBGridForm extends JFrame
DBReader.outMatchesTable(gf);
gf.setVisible(true);

... 其中 DBReader.outMatchesTable() 定义为

static public void outMatchesTable(DBGridForm gf) {
    DBReader ddb = new DBReader();
    ddb.readMatchesTable(null);
    gf.showFromVectors(ddb.lastRsltColNames, ddb.lastRsltData);
}

我的猜测是我忽略了一些内容,要么是关于我正在使用的 swing 类,要么是关于 Java。 有任何想法吗?

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);

... 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);
}

My guess is I'm overlooking something, either about the swing classes I'm using, or about Java. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心如狂蝶 2024-07-12 03:26:37

如果您没有在事件线程上运行,则可能会出现问题——我见过这会导致某些内容无法显示。

如果此代码是为了响应 AWT 事件(鼠标单击、按下按钮等)而调用的,那么这不是问题,但如果它仍然是启动应用程序的同一线程,或者此代码正在计时器上运行,则可以很好。

If you are not running on the event thread, it could be a problem--I've seen that cause stuff not to display.

If this code is called in response to an AWT event (mouse click, button press, ...) then that's not the problem, but if it's still the same thread that started your app, or this code is running off a timer, could very well be.

诗化ㄋ丶相逢 2024-07-12 03:26:37

“这个”在你的上下文中不清楚。 是在小程序里面吗? JFrame?

您可能遇到布局问题,请确保您已使用新的边框布局在类上调用了 setLayout。

在 swing 应用程序中,您需要使用 getRootContentPane().add() 而不是原始 add(),具体取决于版本。

有关添加顶级内容的 Java 教程: http://java .sun.com/docs/books/tutorial/uiswing/components/toplevel.html

"this" in your context is unclear. Is it inside an applet? a JFrame?

You may be having a layout issue, make sure you've called setLayout on your class with a new borderlayout.

In a swing application, you'd want to use getRootContentPane().add() instead of a raw add(), depending on the version.

Java tutorial on adding top-level content: http://java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文