简体   繁体   English

将带有数据的JTable添加到JDialog

[英]Adding a JTable with data to a JDialog

I am trying to display a JTable with data from a database. 我正在尝试使用数据库中的数据显示JTable。 My code has a submit button and when I hit that the data has to appear in the table. 我的代码有一个提交按钮,当我点击该按钮时,数据必须出现在表中。

To create the application, I created a "New JDialog" using NetBeans and starting placing some components like text boxes along with the submit button. 为了创建该应用程序,我使用NetBeans创建了一个“ New JDialog”,并开始将一些组件(如文本框)与“提交”按钮一起放置。 I then added a Panel to it and then added a JTable inside of the panel. 然后,我向其中添加了一个面板,然后在面板内部添加了一个JTable。

In my code, I have a method for the Default table model which returns a default table model object. 在我的代码中,我有一个Default表模型的方法,该方法返回一个默认表模型对象。 As part of the submit button's event handling code, I am passing the default table model's object into the JTable component (just using _table = new JTable(default table model object). _table was declared by NetBeans when I added the table to my form. 作为提交按钮的事件处理代码的一部分,我将默认表模型的对象传递到JTable组件中(仅使用_table = new JTable(默认表模型对象)。_table是在将表添加到表单时由NetBeans声明的。

How should I proceed and get my table to display data? 我应该如何进行操作并让我的表格显示数据? NetBeans basically creates the code. NetBeans基本上创建代码。 Technically: 技术上:

1) My class extends JDialog and then comes the constructor which takes a frame and boolean as input. 1)我的类扩展了JDialog,然后是构造函数,该构造函数将一个框架和布尔值作为输入。 2) The initcomponents method comes in the constructor. 2)initcomponents方法位于构造函数中。 3) I have my methods and then the event handling code. 3)我有我的方法,然后是事件处理代码。 4) The main method contains the invoke later with a runnable object and the run method which instantiates a JDialog object and sets it to true. 4)main方法包含稍后带有可运行对象的调用以及将JDialog对象实例化并将其设置为true的run方法。

If the explanation is not clear and I should post the code, please let me know. 如果解释不清楚,我应该发布代码,请告诉我。 Thanks a lot! 非常感谢!

I'm assuming that _table was created by the netbeans form editor and that it is a reference to the table which is on the screen. 我假设_table是由netbeans表单编辑器创建的,并且它是对屏幕上表的引用。 So instead of using _table = new JTable(...) , you should be using _table.setModel(model) 因此,您应该使用_table.setModel(model)而不是_table = new JTable(...) _table.setModel(model)

I only see four columns, how can see all the columns and adjust the column width to display the entire column for all of the columns 我只看到四列,如何查看所有列并调整列宽以显示所有列的整个列

Wrap the JTable in a JScrollPane ... JTable包装在JScrollPane ...

See How to Use Scroll Panes and How to Use Tables for more details 有关更多详细信息,请参见如何使用滚动窗格如何使用表格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM