質問

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