Java IDE Programming-I
Graphical User Interface : is an interface that uses pictures and other graphic entities along with text, to interact with user. User can interact with GUI using mouse click/ or keypress on keyboard.
GUI programming is done using Swing API. Swing is a set of customizable graphical component whose look and feel can be dictated at run time. Swing API is part of JFC.
Types of Swing Controls: Component self contained entity, customizable e.g JLabel, JTextField, JButton etc. Container- can hold other control e.g. JPanel, JFrame, JApplet, JDialog, and JInternalFrame etc. Child controls
Various Controls of Swing API: 1. Basic Controls: Simple controls for input or show simple state. e.g. JButton, JCheckBox, JComboBox, Jlist, JRadioButton,JSlider, JSpinner,JTextField, JPasswordField etc. 2. Interactive Displays: Display info. Highly formatted in nature and can be modified by user. e.g. JColorChooser, JEditorPane, JTextPane, JFileChooser, Jtable, JTextArea, Jtree. 3. Uneditable Infor. Display: Display info. That cant be edited. e.g. JLabel, JProgressBar, JSeparator, JToolTip. 4. Top Level Container: Atleast one of them is always present in any swing application. e.g. JApplet, JDialog,JFrame. 5. General Purpose: JPanel,JScrollPane, JSplitPane. 6. Specific Purpose: JInternalFrame, JlayeredFrame.
Starting GUI Application, Steps for Creation. We had already learnt this in previous Chapters. Handling Events: An event in Java is occurrence of an activity when something changes within a graphical user interface. If a user clicks on a button, clicks on a combo box, or types characters into a text field, etc., then an event triggers/ Activated. In order to react to occurrence of event, we need to create and implement a event handling mechanism in our application. Event Handling is the mechanism that controls the event and decides what should happen if an event occurs.
How Events Work? Event handling in Java is comprised of three key elements: (i) Event Source: GUI component that generates the event e.g a button. (ii) Event handler of Event Listener: a code that react to the event and compute. (iii) Event Object : whenever event occurs, event source creates a object and passes it to event listener, it contains all the necessary information about the event.
Types of Events Here are some of the most common types of events in Java:
Defining Event handlers and giving different names. Define event handler using contextual Menu
Java being OOP language, implements swing components as it implements classes. Object: is an identifiable entity with some characteristics (values/attributes associated with object) and behaviour (functions gen. called methods). Class: A class represent a set of objects that have a common structure and behaviour. It is blueprint that represent a set of similar objects. Objects are the instances of a class.
Working with Containers Every Swing application must have at least one top-level container (JFrame, JDialog, and JApplet) Each top level container has two commonly used intermediate containers: (i) A menu bar menus (ii) And content pane - components If no menu bar then content pane expands to fill that area.
Customizing Frame Appearance 1. Appearance is governed by its layout and property setting. 2. By default BorderLayout Properties: title cursor- Crosshair, East resize, West resize etc. Iconimage icon appears on title bar Resizable- resizing at runtime is possible Default CloseOperation do_nothing, hide, dispose, exit_on_close
Working with Panel A panel is a type of container that s designed to hold a group of components so they can be displayed on a frame. Adding panel to JFrame. Adding components to Panels. Adding Look to Panel setting layout, properties like boarder, background, tooltip etc.
Using HTML in swing components: We can use HTML for formatting text such as Multiple lines, Use <html> tag at the beginning of the text.
A function or Method in Java A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. Think of a method as a subprogram that acts on data and often returns a value.
When that name is encountered in a program, the execution of the program branches to the body of that method. When the method is finished, execution returns to the area of the program code from which it was called, and the program continues on to the next line of code.
There are two basic types of methods: Built-in: Build-in methods are part of the compiler package, such as System.out.println( ) and System.exit(0). User-defined: User-defined methods are created by you, the programmer. These methods take-on names that you assign to them and perform tasks that you create.
Syntax: modifier returntype nameofmethod (Parameter List) { // method body } public static int methodname(int a, int b) { } // body Here, public static modifier int return type methodname name of the method a, b formal parameters int a, int b list of parameters
Working with push buttons and their properties: covered in previous chapter
Assigning access key: TO assign access key to command button use mnemonic property (Appindix A)
Working with push buttons and their properties: covered in previous chapter
Using Action Command in Button: The Action Command Property of a button is a string that can identify the button that has fired an action event. We can use getactioncommand method to obtain value of actioncommand property. For this we will use parameter of actionperformed() event handler. In case of Calculator -multiple buttons can be assigned the same event handler.
CHECK BOX
CHECK BOX