1

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);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
KaNaDa
  • 141
  • 12

1 Answers1

2

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.JTables, see How to Use Tables

  • Thank you for this information. One question. Is it also possible to make an "sideboard" or however one want to call it. So like in excel you have the column names A, B, C, ... and the row names 1, 2, 3, ... is it possible to make this with JTable? So you have a header and the same thing on the side? – KaNaDa May 19 '19 at 23:03
  • *"One question."* Any Stack Overflow Q&A thread should focus on one question / problem. It is best to move other questions to a new, specific Q&A thread. – Andrew Thompson May 20 '19 at 02:48
  • 1
    Yes it's possible, as @AndrewThompson says have a single question in every post, but in order to prevent a duplicate question, if you make a Google Search for `JTable header top and left` you'll eventually get this answer from thrashgod: https://stackoverflow.com/a/8005006/2180785 which does what you want. – Frakcool May 20 '19 at 14:39