简体   繁体   English

使用插入的数据库数据创建弹出表

[英]create popup table with database data inserted

ok this is a theoretical question. 好的,这是一个理论问题。 say I have a database connection set up and I run the query 说我已经建立了数据库连接,然后运行查询

"select * from securitysystem.employee"

and I wanted to populate a table that would autofit the data returned from the query. 我想填充一个表,该表将自动拟合查询返回的数据。 How would I do this? 我该怎么做?

Steps: 脚步:

  1. Loop through results and convert the results to a Collection of objects (Employee or however else you are modeling them.) 遍历结果并将结果转换为对象集合(Employee或您正在建模的对象。)
  2. Hand the Collection off to your GUI 将集合交给您的GUI
  3. Have the GUI create a new JDialog that contains a JTable. 让GUI创建一个包含JTable的新JDialog。
  4. When building the table model for the data it either needs to handle your Collection of objects or you need to convert it to a data format it can handle (example Object[][] for a DefaultTableModel ). 为数据构建表模型时,它要么需要处理您的对象集合,要么需要将其转换为它可以处理的数据格式( DefaultTableModel示例Object [] [])。
  5. Set the dialog to visible. 将对话框设置为可见。

You need to get that data back as a double array somehow (ideally you want a table model of some sort, but for quick and dirty a double array is fine). 您需要以某种方式将数据作为双精度数组取回(理想情况下,您需要某种类型的表模型,但是对于快速又脏的双精度数组来说,这是很好的选择)。

  • Add the JTable to a JScrollPane . JTable添加到JScrollPane
  • Add the JScrollPane to your Panel or Frame. 将JScrollPane添加到面板或框架。
  • Give it maximum stretch (ie. central panel in BroderLayout for example) 给它最大的拉伸度(例如,在BroderLayout中的中央面板)
  • Set the table.setModel(new DefaultTableModel(myDoubleArray)) 设置table.setModel(new DefaultTableModel(myDoubleArray))
  • Call table.updateUI() 调用table.updateUI()

That should do it. 那应该做。

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

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