I had the same question but with a different application - I needed to limit the height only, of a JPanel I was adding to a container with a BoxLayout. So when I called setMaximumSize on it and used its PreferredSize for the width, the result was that it did not get the stretching that I expected in the horizontal dimension; it came out shorter because that was its preferred size. I couldn't find what actual width the layout manager was applying, so was at a loss as to what width to use in the new Dimension but the setting I wanted for height worked just fine. The final answer was:
myJPanel.setMaximumSize(new Dimension(<some overly high width value>, <desired height>));
Then the BoxLayout manager gave myJpanel the correct lower width that still filled the container horizontally, and the lower height that I wanted to prevent the vertical stretch.