Question

This code puts a JTable into a JFrame (sole component of the whole UI):

    JFrame frame = new JFrame( "Title");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTable table = appender.createTable();
    JScrollPane scrollPane = new JScrollPane(table);
    table.setFillsViewportHeight(true);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);

I also have some code to set the preferred size of the columns.

Nothing fancy. When the UI opens, the table takes up the whole view and I have no scroll bars. The combined preferred widths of the columns need more horizontal space than the windows is wide. Despite of that, there is no horizontal scroll bar and the columns are too narrow.

What do I have to do that

  1. The columns are still resizable by the user
  2. The current width is respected? I.e. when I change the width of one column, the width of the other columns should not change.
Was it helpful?

Solution

table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );

You can also add the scrollpane containing the table to a JPanel and then add the panel to the frame. That way the panel will change in size, not the scrollpane.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top