1

I have a JTable with a few rows of data but the rows don't stretch across the full width of the table. I'd like to be able to see all of the data without it being cut off and have the zebra stripes stretch the whole width, right now this is how my table looks, so far I have been successful in having my data being shown without being cut off:

enter image description here

I have already attempted to achieve what I want with the following code:

orderTable = new JTable(orderTableModel ) {
            public boolean isCellEditable(int row, int col) {
                return false;
            }

            public Component prepareRenderer(TableCellRenderer r, int row, int col) {
                Component c = super.prepareRenderer(r, row, col);

                // Next 3 lines adapted from https://stackoverflow.com/questions/17858132/automatically-adjust-jtable-column-to-fit-content/25570812
                int rendererWidth = c.getPreferredSize().width;
                TableColumn tableColumn = getColumnModel().getColumn(col);
                tableColumn.setPreferredWidth(Math.max(rendererWidth + getIntercellSpacing().width, tableColumn.getPreferredWidth())); // Sets width of columns to fill content.

                // Rows alternate in colour for readability.
                if (row % 2 == 0) {
                    c.setBackground(Color.WHITE);
                } else {
                    c.setBackground(new Color(234, 234, 234));
                }

                if (isRowSelected(row)) {
                    c.setBackground(new Color(24, 134, 254));
                }

                return c;
            }
        };

        orderTable.setFont(new Font("", 0, 14));
        orderTable.setRowHeight(orderTable.getRowHeight() + 10);
        orderTable.setAutoCreateRowSorter(true);
        orderTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
user9109814
  • 175
  • 2
  • 14
  • 2
    You will want to create and post a valid [mcve] program with your question so that we can reproduce the problems ourselves with minimal difficulty and effort on our part (it is your question and so the effort should be yours). You don't show how you add the orderTable to the GUI. It should be placed into a JScrollPane and then the scrollpane into another container, and the layout of that container will matter a great deal. Also have you tried different auto resize modes? – Hovercraft Full Of Eels Apr 22 '18 at 23:03
  • 1
    `orderTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);` ... maybe set this to "auto" instead – MadProgrammer Apr 22 '18 at 23:03
  • 1
    Trying to perform "zebra stripping" in a cell renderer is error prone and won't achieve the result you're looking for. The problem? The cell renderer only paints the area it's responsible for. The solution? Well, that's some what complicated. "A" solution would be to extend the `JTable` and "paint" the "zebra strip" before the content, not actually as easy as it sounds. You could have a look at [this example](https://stackoverflow.com/questions/25279727/java-abstracttablemodel-2-different-color-for-each-row/25279954#25279954) which presents a couple of possible ideas – MadProgrammer Apr 22 '18 at 23:07
  • Thank you for the ideas, I have set the auto resize to `AUTO_RESIZE_ALL_COLUMNS` and it looks fine now. Thanks again. – user9109814 Apr 22 '18 at 23:10

0 Answers0