JTable vs. custom TableModel

泪湿孤枕 提交于 2020-01-21 12:19:41

问题


I am trying to implement a JTable on a Java GUI that can fill itself with values from a file and allow the user to make modifications to various cells. Since I'm using the GUI editor in the Netbeans IDE, my first instinct was to add the JTable to my form from the palette. however, I quickly realized that I couldn't add more than 100 rows to the table (for my application i'd need around 500+). additionally, while searching for a solution to this problem, I noticed a lot of people saying to use a Custom TableModel instead of using the JTable because it is more robust/efficient.

first, is there a way to add more than 100 rows to a JTable? secondly, is using the JTable (which uses the DefaultTableModel) really a bad implementation? my form is pretty complex, so I would prefer to use the GUI editor to adjust the size, position, etc. of my JTable as opposed to hard-coding it.


回答1:


is there a way to add more than 100 rows to a JTable?

Why is 100 a limit? That sounds like your IDE, not the DefaultTableModel. Tens of thousands of rows is no problem for the DefaultTableModel (not that any user would want to look at all that data).




回答2:


Most Swing GUI coders that I know don't "hard-code" their GUI's but rather use the easier to use layout managers (BorderLayout, GridLayout, BoxLayout, FlowLayout, MigLayout, FormLayout, etc...) to do the heavy lifting for them and to allow their complex GUI's to be resizeable and decent looking on multiple platforms. Regarding your other issue, it's not whether to use a JTable or a custom model since if you're using a JTable, you'll be using a JTable whether or not the model is default or custom, but if you're doing anything complex, then yeah, you'll likely want to add your own TableModel to your JTable. They're not that hard to create, and w can help you with it. I do wonder about your 500 line requirement -- there's no way that anyone will need or want to look at 500 lines at one time. Perhaps you want to use a database and load and remove rows into your TableModel as needed.




回答3:


Very Simple to increase rows manually rather than to increase them through GUI :

  1. Right Click on your newly designed jTable
  2. Select Customize Code
  3. Change the DEFAULT CODE to CUSTOMER PROPERTY (where the object creation started for rows)
  4. By Default null rows are 100
  5. COPY and PASTE the the NULL rows as maximum you need
  6. CLICK ok. Now your table rows size INCREASED as needed


来源:https://stackoverflow.com/questions/8143537/jtable-vs-custom-tablemodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!