We have to make a program which is a little bit like excel. Now I have the problem, in that I want to make a field of 999 columns and 999 rows. 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. 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:
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.
For a more in-depth tutorial about javax.swing.JTable
s, see How to Use Tables