[Java] embedding a panel in scrollpane

Source: Internet
Author: User

Not to put a lot of component in a window with a scroll bar, it was a bit boring for a day, but the hands-on capability should also be improved in dealing with such complicated details :)

Only one component can be put in scrollpane. Its setlayout () method is also virtual, and an error occurs when it is used. If you want many components to be placed in a window with a scroll bar, you can only put these components in a panel first, and then embed the panel into scrollpane, there is another problem here, if the layout of the Panel is set to null, the scroll bar will not be useful. I spent a whole day on panel. setlayout (null. Finally, I used setlayout (New gridlayout () to solve this problem. Below is the code to embed the panel into scrollpane (the code that is directly copied from my program has not been modified ):

// Four component arrays are initialized here.
Checkbox = new checkbox [numofitems];
Keyfield = new jtextfield [numofitems];
Chfield = new jtextfield [numofitems];
Enfield = new jtextfield [numofitems];

Scrollpane plane = new scrollpane ();
Panel = new Panel (New gridlayout (numofitems, 1); // set the layout of the Panel to the numofitems row, with one column in each row.

For (INT I = 0; I <numofitems; I ++)
{
Checkbox [I] = new checkbox ();
Keyfield [I] = new jtextfield (30 );
Chfield [I] = new jtextfield (SP [I]. STR, 30 );
Enfield [I] = new jtextfield (30 );

Panel pnl = new Panel (New flowlayout (flowlayout. Left ));
Pnl. Add (checkbox [I]);
Pnl. Add (keyfield [I]);
Pnl. Add (chfield [I]);
Pnl. Add (Enfield [I]); // Add each row of components to a sub-panel.
Panel. Add (pnl); // Add the sub-panel to the Panel.
}

Embedded. Add (panel); // embed the panel into scrollpane
Mycontainer. Add (container );
Gradient. reshape (950,600 );

Although this problem is very small, it takes me a day. I hope it will be useful here. Which brother or sister will encounter the same problems as me one day. The layout problem in Java is very troublesome, but I am too lazy to study it. I am not very interested in it. As long as the problem is solved, it will be OK.

Important references: http://www.chinalinuxpub.com/doc/oreillybookself/java/awt/ch11_04.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.