Object Oriented Programming with AWT Components, Summaries of Java Programming

An overview of important AWT components such as lists, choices, text areas, scroll bars, and scroll panes. It explains how to use text areas and scroll bars, and provides constructors and methods of the TextArea and Scrollbar classes. The document also includes sample programs for TextArea, Scrollbar, and ScrollPane. The major theme of this chapter is scrolling, and all the controls support scrolling in one way or another, letting the user move around large documents with ease.

Typology: Summaries

2023/2024

Available from 11/13/2023

humble-edward
humble-edward 🇳🇬

1 document

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
A TERM PAPER ON OBJECT ORIENTED
PROGRAMMING
(AWT COMPONENTS (LIST, CHOICE, TEXT
AREAS, SCROLL BARS, SCROLL PANE)
BY
HUMBLE EDWARD
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Object Oriented Programming with AWT Components and more Summaries Java Programming in PDF only on Docsity!

A TERM PAPER ON OBJECT ORIENTED

PROGRAMMING

(AWT COMPONENTS (LIST, CHOICE, TEXT

AREAS, SCROLL BARS, SCROLL PANE)

BY

HUMBLE EDWARD

ABSTRACT

Everything about some important AWT components—lists, choices, text areas, scroll bars, and scroll panes. These components are all fundamental to AWT programming—they are familiar to nearly all GUI users—and we’ll take a look at them in overview in this section. The major themes of this Chapter is scrolling; all the controls support scrolling in one way or another, letting the user move around large documents with ease.

Lists A list control presents the user with a list of items—text strings—from which one can select. In windowing environments, space is often at a premium, so it is a good idea to place items in lists, because using scroll bars, you can hide long lists in short list components. The user can select the item in the list he or she wants, double-click it, and initiate some action. Lists can also support multiple selections at the same time, using the Shift and Ctrl keys. If you support multiple selections, you have to think about how to use the mouse in a list control. Clicking an item selects it, so the user can’t just make multiple selections and then double-click one to initiate some action because when one item is double-clicked, all the others are deselected. How to use ; Say you have a long list of items that you want to present, consider by using a list control. You can fill a list with many text items (using the List class’s add () method) and present only a few at a time—you can choose how many—to the user. The user can highlight an item in the list by clicking the item and can initiate some action by double-clicking that item. Constructors for List List() -It creates a new scrolling list List(int rows)- It creates a new scrolling list with the specified number of visible lines List(int rows, boolean multipleMode)-It creates a new scrolling list with the specified number of rows. Multiple selection is enabled if multipleMode is true Methods of the List class(What it does) void add(String item) -It adds the given item to the end of the list void add(String item, int index) -It adds the given item to the list at the position given by the index void addActionListener (ActionListener l)-It adds the given action listener to receive action events from the list. void addItem(String item)-It is deprecated and replaced by add(String) void addItem (String item, int index)-It is deprecated and replaced by add(String, int) void addItemListener (ItemListener l)-It adds the given item listener to receive item events from the list. Etc. PROGRAMS; import java.applet.Applet; import java.awt.; import java.awt.event.; import javax.swing.event.ListSelectionEvent; /* <APPLET CODE=ListItemDemo2.class WIDTH= HEIGHT=250 > */ public class ListItemDemo2 extends Applet implements ActionListener { TextArea text1;

TextField text2; List list1; Button button1; public void init() { text1 = new TextArea(5,20); text2 = new TextField(20); button1 = new Button("Remove Listener"); add(text1); add(text2); list1 = new List(4, true); list1.add("First"); list1.add("Second"); list1.add("Third"); list1.add("Fourth"); list1.add("Fifth"); list1.add("Sixth"); list1.add("Seventh"); list1.add("Eighth"); list1.add("Ninth"); add(list1); add(button1); list1.addActionListener(this); button1.addActionListener(this); } public void valueChanged(ListSelectionEvent e) { if(e.getValueIsAdjusting()) return; text1.setText(""); Object[] items = list1.getSelectedItems(); for(int i=0; i<items.length; i++) text1.append(items[i] +"\n"); } public void actionPerformed(ActionEvent e) { if(e.getSource() == button1) { ActionListener[] al = list1.getActionListeners(); for (int i=0; i<al.length; i++) { list1.removeActionListener(al[i]); text2.setText("Listener Removed"); } } } }

static int SCROLL BARS_VERTICAL_ONLY- It creates and displays a vertical scroll bar only Constructors of the TextArea class TextArea() - It constructs a new text area with the empty string as text TextArea(int rows, int columns)- It constructs a new empty text area with the given number of rows and columns and empty string as text TextArea(String text)- It constructs a new text area with the given text TextArea(String text, int rows, int columns)- It constructs a new text area with the given text and with the given number of rows and columns TextArea(String text, int rows, int columns, int scrollbars)- It constructs a new text area with the given text, rows and columns, and scroll bar visibility. Methods of the TextArea class void addNotify()- It creates the text area’s peer void append(String str)- It appends the given text to the text area’s current text void appendText(String str)- It is deprecated and replaced by append(String) AccessibleContext getAccessibleContext()- It gets the AccessibleContext associated with this TextArea int getColumns()- It gets the number of columns in the text area Dimension getMinimumSize()- It gets the minimum size of the text area Program: import java.applet.Applet; import java.awt.; import java.awt.event.; /* <APPLET CODE=TextAreaDemo1.class WIDTH= HEIGHT=200> */ public class TextAreaDemo1 extends Applet implements ItemListener { TextArea area1; CheckboxGroup group; Checkbox check1,check2; public void init() { area1 = new TextArea(10,20); add(area1); group = new CheckboxGroup(); check1 = new Checkbox("Male", false, group); add(check1); check1.addItemListener(this); check2 = new Checkbox("Female", false, group); add(check2); check2.addItemListener(this); }

public void itemStateChanged(ItemEvent e) { area1.setText("You selected "+((Checkbox)e.getItemSelectable()).getLabel()); } } Inheritance diagram for the AWT TextArea class : java.lang.Object |____java.awt.Component |____java.awt.TextComponent |____java.awt.TextArea CHOICE: Choice controls also present the user with a list of items to select from, but there’s a difference —choice controls are even more compact than lists. They look much like text fields—although you can’t edit the text in them with a small button on the right that has a downward-pointing arrow. When the user clicks the arrow, a list opens displaying all the available choices, and the user can select from one of them. After the selection is made, the choice control closes the list and displays the current selection. If the user opens the list again, the current selection is highlighted. Note that choice controls are only designed to display one choice at a time, which means you can’t select more than one item in the list at a time. Using Shift+click and Ctrl+click is

trouser.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } // Display current selections. public void paint(Graphics g) { msg = "Shirt: "; msg += shirt.getSelectedItem(); g.drawString(msg, 6, 120); msg = "Trouser: "; msg += trouser.getSelectedItem(); g.drawString(msg, 6, 140); } } SCROLL BARS: Scroll bars are, of course, the zenith of scrolling controls, and every GUI user knows about them. The user uses the mouse to manipulate the thumb—also called the scroll box, elevator, or bubble—in a scroll bar to select a value from a continuous range. You set that range when you create the scroll bar, and after the user selects a new value, you can catch scroll bar events to read that new value. The user can also click the arrow buttons at the ends of a scroll bar to increment or decrement the scroll bar’s setting by an amount you specify. The user can also click the scroll bar’s track (that is, the part that’s not the thumb or the arrow buttons) to increment or decrement the scroll bar’s setting by an amount you specify. HOW TO USE;

Every GUI user is familiar with scroll bars. In Java, scroll bars consist of arrows (the buttons at each end of the scroll bar), a thumb or bubble (the scrollable box you slide), and a track (the part of the scroll bar you slide the thumb in). The AWT supports scroll bars with the Scrollbar class. Constructors of the Scrollbar class Scrollbar() -It creates a new vertical scroll bar Scrollbar(int orientation)- It creates a new scroll bar with the given orientation Scrollbar(int orientation, int value int visible, int minimum, int maximum)-It creates a new scroll bar with the given orientation, initial value, page size, and minimum and maximum values. Methods of the Scrollbar class void addAdjustmentListener (AdjustmentListener l)- It adds the given adjustment listener to receive adjustment events. void addNotify()- It creates the scroll bar’s peer AccessibleContext getAccessibleContext()- It gets the AccessibleContext associated with this Scrollbar AdjustmentListener[]getAdjustmentListeners()- It returns an array of all adjustment listeners registered on this scroll bar int getBlockIncrement()- It gets the block increment of the scroll bar Inheritance Diagram: java.lang.Object |____java.awt.Component |____java.awt.Scrollbar PROGRAM: import java.applet.Applet; import java.awt.event.; import java.awt.; /* <APPLET CODE=ScrollText.class WIDTH= HEIGHT=200 > / public class ScrollText extends Applet implements AdjustmentListener { Scrollbar scroll1; int x = 40; int y = 40; public void init() { scroll1 = new Scrollbar(Scrollbar.HORIZONTAL, 1, 20, 20, 100); add(scroll1); scroll1.addAdjustmentListener(this); } public void paint(Graphics g) { g.drawString("Kogent", x, 80);*

static int SCROLL BARS_AS_NEEDED-It indicates that horizontal/vertical scroll bars should be shown only when the size of the child is greater than the size of the scroll pane static int SCROLL BARS_NEVER- It indicates that horizontal/vertical scroll bars should never be shown. Constructors of the ScrollPane class ScrollPane()- It creates a new scroll pane ScrollPane(int scrollbarDisplayPolicy) -It creates a new scroll pane by using a display policy Methods of the ScrollPane class protected void addImpl (Component comp, Object constraints, int index)- It adds the given component to this scroll pane void addNotify()- It creates the scroll pane’s peer void doLayout()- It lays out this container by resizing the contained component to its preferred size. Inheritance diagram for the ScrollPane class : java.lang.Object |____java.awt.Component |____java.awt.Container |____java.awt.ScrollPane PROGRAM: import java.applet.Applet; import java.awt.; /<APPLET CODE=ScrollPaneDemo2.class WIDTH= HEIGHT=120> / public class ScrollPaneDemo2 extends Applet { ScrollPane scrollpane1; Label labels[]; Panel panel1; public void init() { scrollpane1 = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS); panel1 = new Panel(); panel1.setLayout(new GridLayout(5,5)); labels = new Label[25]; for(int i=0;i<25;i++) { labels[i] = new Label("Kogent"); panel1.add(labels[i]); } setLayout(new BorderLayout()); scrollpane1.add(panel1); add(scrollpane1); }*