Introduction to Java :Components Part-2
Lists
You will recall that in the SELECT widget in HTML programming, you could select how many choices were visible at any one time using the SIZE attribute. Well, since the Choice widget does not have such an attribute, you need to use a different component if you want that functionality. In the JDK, the List component is used for such circumstances.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.awt.*; public class ListExample { public static void main(String[] args) { Frame baseFrame = new Frame(); baseFrame.reshape(10,10,200,200); baseFrame.setTitle("List Example"); List list = new List(3, true); list.addItem("Selena"); list.addItem("Eric"); list.addItem("Mark"); list.addItem("Bob Frog"); baseFrame.add(list); baseFrame.show(); } } |
Menus and Menu Bars
Menus allow you to organize many related functions in your application in one centralized, easy-to-use interface object. Although Menus come in many forms, the most common menu types are “menu bars.”
Menu bars are particularly common and part of just about every application interface around today. Specifically, most applications will have a menu bar at the top of the screen that allows a user to choose among the major functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.awt.*; public class MenuExample { public static void main(String[] args) { Frame baseFrame = new Frame(); baseFrame.reshape(10,10,200,200); baseFrame.setTitle("Menu Example"); MenuBar bar = new MenuBar(); Menu fileMenu = new Menu("File"); Menu editMenu = new Menu("Edit"); Menu specialMenu = new Menu("Special"); fileMenu.add(new MenuItem("Open")); fileMenu.add(new MenuItem("Save")); fileMenu.addSeparator(); fileMenu.add(new MenuItem("Exit")); bar.add(fileMenu); bar.add(editMenu); bar.add(specialMenu); baseFrame.setMenuBar(bar); baseFrame.show(); } } |
Scrollbars
Almost without doubt and before you know it, your users will be filling your application with more data than it can possibly display on one screen.
When this occurs, most programmers rely on some form of scrolling control to allow the user to manipulate the currently viewable portion of an interface object. Scrolling is handled in the JDK by the Scrollbar class. Scrollbars take a little more coding than is realistic for this tutorial. However, you can certainly consult the online documentation and any one of the 200 zillion books on Java for more info.
Buttons
Buttons are probably the most common interface control in modern user interfaces. They appear in toolbars, in menus, as the up and down arrows in scrollbars, and in all sorts of other places. In fact, it is hard to imagine any interface without a button somewhere.
The Button class defines a standard push button that uses 3D effects. When the user clicks on the button with the mouse, the button visually depresses and then pops back up when the mouse button is released.
We have already seen plenty of examples of Buttons and Button code so we won’t duplicate it here.
Canvases
Finally, the Canvas class prepares an area upon which you can draw. We will discuss drawing later. So for now, we will just mention this component.