2c00L
2c00L

Reputation: 514

Resizable scrollpane to an awt frame

I need to add a scrollable JPanel to a AWT frame that can scale when the frame is re-sized. The scrollpane appears when I set it a fixed size. But I need the panel to cover the whole frame and re-size automatically when I re-adjust the frame size.

    Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
    Frame frame = SWT_AWT.new_Frame(composite);

    SimulationPanel simPanel= new SimulationPanel(WIDTH,simPanelTotalHeight);

    JScrollPane scrollPane = new JScrollPane(simPanel);
    scrollPane.setSize(new Dimension(500,400));

    JPanel contentPane = new JPanel(null);
    contentPane.add(scrollPane);

    frame.add(contentPane);

Upvotes: 1

Views: 138

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

  1. Don't set the JScrollPane's size or its preferredSize. Either can mess with its ability to re-size if needed.
  2. Instead add it to a container using a layout that allows it to re-size with the container. BorderLayout comes to mind. Make sure that the container hierarchy also attaches to the top-level window in some decent way using a smart combination of layout managers. This is one of the key reasons to understand well and use the layout managers.
  3. Don't use null layout, and in fact that is one thing that is messing up your code.
  4. As a side issue: Why use AWT's Frame and not Swing's JFrame? This doesn't make much sense.

Upvotes: 2

Related Questions