在IntelliJ IDEA中使用JTable [英] Working with JTable in IntelliJ IDEA

查看:318
本文介绍了在IntelliJ IDEA中使用JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在intellij中以gui形式创建了一个jtable,我没有看到任何数据。
最奇怪的是,当我使用它而不在intellij
中创建表单时,代码可以工作。

我使用最常见的例子

String [] columnNames = {First Name,Last Name};

Object [] [] data = {{Kathy,Smith},{John,Doe}

然后

JTable table = new JTable(data,columnNames);

但我没有数据。

这是因为布局管理器吗?

继续使用intellij gui和jtable的任何帮助?

任何好的jtable + gui表格intellij例子?

解决方案

使用IDEA GUI Designer时,如果您有 new JTable,则会自动为您创建JTable( ...)在你的代码中它会覆盖由IDEA创建的表对象,并且在Designer中为表配置的所有属性都将丢失。



<那么,你有两种方法。一种是依靠IDEA创建表并设置其属性,然后使用 table.setModel(dataModel); 从dataModel向表中提供数据。



第二种方法是从代码中动态创建表,然后通过 scrollPane.setViewportView(myTable)将此表添加到表单上的现有JScrollPane或其他面板中);



IDEA还有更高级的方法,称为自定义创建。如果在窗体上为JTable启用此复选框,则IDEA会将 createUIComponents()方法添加到您手动创建此组件的绑定类,如 .. 。= new JTable(...)



你可以下载完整的示例项目并尝试不同的方法。



I created a jtable in gui form in intellij and I dont see any data. The strangest thing is that when I am using it with out creating a form in intellij the code works.
i used the most common example
String[] columnNames = {"First Name", "Last Name"};
Object[][] data = {{"Kathy", "Smith"},{"John", "Doe"}
and then
JTable table = new JTable(data, columnNames);
But I get no data.
Is this because of the layout manager?
any help in continue working with intellij gui and jtable?
any good jtable+gui form intellij example?

解决方案

When using IDEA GUI Designer, JTable is created for you automatically, if you have new JTable(...) in your code it overrides the table object created by IDEA and all the properties configured for the table in the Designer will be lost.

So, you have 2 approaches here. One is to rely on IDEA to create the table and set its properties, then use table.setModel(dataModel); to provide data to your table from the dataModel.

Second approach is to create the table dynamically from your code and then add this table into the existing JScrollPane or other panel on the form via scrollPane.setViewportView(myTable);.

IDEA also has more advanced approach called Custom Create. If you enable this checkbox for the JTable on the form, IDEA adds createUIComponents() method to the bound class where you create this component manually like ... = new JTable(...).

You can download the complete sample project and experiment with different approaches.

这篇关于在IntelliJ IDEA中使用JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆