[英]I have a problem with my table program. Could one tell me how to solve it?
We have to make a program which is a little bit like excel. 我们必须制作一个有点像excel的程序。 Now I have the problem, in that I want to make a field of 999 columns and 999 rows. 现在,我遇到了一个问题,因为我想创建一个999列和999行的字段。 I already tried to just at 999*999 JTextField
controls but that obviously needs very long and I get an exception that there is no memory left. 我已经尝试只使用999 * 999 JTextField
控件,但是显然这需要很长时间,而且我得到一个例外,那就是没有剩余的内存。 How could I make that better? 我该如何做得更好? Should I try to only render these text fields which are in use or is there a better method to make a table? 我应该只渲染这些正在使用的文本字段,还是有更好的方法来制作表格?
Here is my code: 这是我的代码:
tablePanel = new JPanel();
tablePanel.setLayout(new GridBagLayout());
tablePanel.setSize(100, 30);
tablePanel.setBorder(null);
JScrollPane tableScroll = new JScrollPane(tablePanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//tableScroll.getVerticalScrollBar().setPreferredSize(new Dimension(0,0));
//tableScroll.getVerticalScrollBar().setUnitIncrement(25);
tableScroll.setBounds(0, 30, 30, this.getHeight());
table = new ArrayList<>();
for (int i = 0; i < 999; i++) {
ArrayList<Component> column = new ArrayList<>();
for (int j = 0; j < 999; j++) {
JTextField field = new JTextField();
field.setPreferredSize(new Dimension(100, 30));
field.setBorder(null);
field.setFocusCycleRoot(false);
field.setFocusable(false);
gbc.gridy = j;
gbc.gridx = i;
column.add(field);
tablePanel.add(field, gbc);
}
table.add(column);
}
You can create a javax.swing.JTable
like this: 您可以这样创建一个javax.swing.JTable
:
JTable table = new JTable(999,999); // creates a 999*999 table
TableCellEditor tce = table.getCellEditor();
// use tce to follow user
and use tce
to follow what the user is doing with what cell. 并使用tce
来跟踪用户在哪个单元格中所做的事情。
For a more in-depth tutorial about javax.swing.JTable
s, see How to Use Tables 有关javax.swing.JTable
的更深入的教程,请参见如何使用表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.