简体   繁体   中英

Java Swing: Allow scroll when textfields are out out of range of scrollpane's view

I have a tabbed pane tabbedPane_1 with scrollpane scrlPnAddServer , which has viewport view set to pnlAddServer . How can i add textfields or other items below the viewable section of the scrollpane. (and allow the user to scroll down to view the items) Currently the items are just not shown when out of range.

        JTabbedPane tabbedPane_1 = new JTabbedPane(JTabbedPane.TOP);
        tabbedPane_1.setBounds(672, 20, 350, 400);
        panel.add(tabbedPane_1);
        JPanel tab = new JPanel();
        tabbedPane_1.add("Build", tab);
        tab.setLayout(null);
        JPanel tab2 = new JPanel();
        tabbedPane_1.add("More...", tab2);
        tab2.setLayout(null);

        JScrollPane scrlPnAddServer = new JScrollPane();
        scrlPnAddServer.setBounds(0, 0, 350, 370);
        tab2.add(scrlPnAddServer);
        scrlPnAddServer.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 

        JPanel pnlAddServer = new JPanel();
        pnlAddServer.setBounds(0, 0, 350, 370);
        scrlPnAddServer.setViewportView(pnlAddServer);
        pnlAddServer.setLayout(null);

Then i add items that I want the user to scroll down to see:

    tfSaveAs = new JTextField("Save As");
    tfSaveAs.setBounds(10, 600, 125, 20);
    pnlAddServer.add(tfSaveAs);
    tfSaveAs.setColumns(10);

Make use of an appropriate layout manager which is capable of calculating the required information which is used the JScrollPane to make decisions whether it should display the scrollbars or not.

This is just one of the many "auto magical" aspects that the layout management API provides

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

See Laying Out Components Within a Container for more details

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