简体   繁体   中英

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);
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM