views:

41

answers:

1

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.
+2  A: 
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.

camickr
Have seen the question, expected a fast answer from someone, and the end of the day makes me too slow to compete with that. I'll just add this link to the [swing tutorial](http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/components/table.html#width) and to the corresponding [javadoc](http://tinyurl.com/2emgeb3)
Gnoupi
@camickr: What is the benefit of adding an intermediate JPanel? I'm seeing no differences in column-, table-, scrollpane- or frame-behaviour.
bobndrew
I thought I read something about you wanting to keep the total size of the table constant (but I was probably wrong). Anyway, when you add the scrollpane to a frame using a BorderLayout, then the size of the scrollpane changes as the size of the frame changes. However you you first add the scrollpane to a panel (which by default uses a FlowLayout), the the size of the table will be respected. If you ten add this panel to the frame then as you resize the frame the panel will change size, but the table size does not change.
camickr