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.
有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top