Showing posts with label GUI components in java. Show all posts
Showing posts with label GUI components in java. Show all posts

Monday, 8 April 2019

JSlider in java | Online Help

JSlider in java:

The JSlider class is used to select an integer value from a range of integer values. There are two types of sliders. They are:

-Horizontal slider

-Vertical slider


Horizontal slider:

The minimum integer value is present at the left extreme and the maximum value is at the extreme right.

Vertical slider:

The minimum integer value is present at the extreme bottom and the maximum value is at the extreme top.

The value which is pointed by thumb is selected.JSlider can be incremented or decremented by on side respectively.

Constructors available in JSlider in java:

The JSlider class has five constructors as follow:

JSlider()

This method is used to create a JSlider with a range of 0-100.

JSlider(int direction)

This method is used to create a JSlider with the specified direction.

JSlider(int min,int max)

This method is used to create a JSlider with a range of min to max value.

JSlider(int direction,int min,int max)

This method is used to create a JSlider with a range of min to max value and the specified direction.

JSlider(int direction,int min,int max,int value)

This method is used to create a JSlider with a range of min to max, an initial value as specified and the specified direction.

Parameters:

direction: Swing Constants.HORIZONTAL / Swing Constants.VERTICAL
min, max: Minimum and Maximum for the JSlider value.
value: Initial value for the JSlider

Methods for JSlider in java:

The most commonly used JSlider() methods are as follow:

void setMajorTickSpacing( int units)

The setMajorTickSpacing()method is used to set the spacing of major tick marks.

void setMinorTickSpacing(int units)

The setMinorTickSpacing()method is used to set the spacing of minor tick marks.

void setPaintTicks(Boolean b)

The setPaintTicks() method is used to display the tick marks. If it is true. By default, it is not shown.

Sample Program for JSlider in java:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
/*<applet code="jsliderdemo" width=500 height=500></applet>*/
public class jsliderdemo extends JApplet implements ChangeListener
{
JSlider js=new JSlider(SwingConstants.HORIZONTAL,0,100,0);
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
js.addChangeListener(this);
c.add(js);
}
public void stateChanged(ChaneEvent e)
{
JSlider js1=(JSlider)e.getSource();
showStatus("slider minimum:"+js1.getMinimum()+",maximum:"+js1.getMaximum+",value:"+js1getValue());
}
}

JComboBox in java |Online Help

JComboBox in java:

The JComboBox in java is a class which can be used to add a drop-down list of item to a container. The JComboBox is a combination of text and drop-down list. The JComboBox class has to constructors as follow:

JComboBox()

This constructor creates an empty JComboBox.

JComboBox(object[] items)

This constructor is used to create a JComboBox that contains the element in the specified array.

Methods for JComboBox in java:

The most commonly used JComboBox() method s are as follows:

void addItemListener(ItemListener Handler)

This method is used to configure an event handler for the combo box.

void setSelectedIndex(int selection)

The setSelectedIndex() method is used to set the index of the selcted item.

int getSelectedIndex()

The getSelectedIndex() method is used to written the index of the selected item.

void setFont(Font strfont)

The setFont() method is used to set the font for the JLabel.

void setBackground(Color bgcolor)

The setBackground() method is used to set the color of the background of the JLabel.

void setForeground(Color strcolor)

The setForeground() method is used to set the color of the string for the JLabel.

Sample Program for JCombo in java:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*<applet code="jcombobox" width=500 height=500></applet>*/
public class jcombobox extends JApplet
{
JComboBox jcb1=new JComboBox();
JTextField jtf;
public void init()
{
Container c=getContentPane();
jcb1.addItem("undergraduate");
jcb1.addItem("postgraduate");
jcb1.addItem("research");
c.setLayout(new FlowLayout());
c.add(jcb1);
}
}

JCheckBox in java |Online Help

JCheckBox in java:

     The JCheckBox in java is a class which is used to do multiple selections from the list of options. The state of the JCheckBox is changed by clicking the mouse on the JCheckBox.The JCheckBox class defines the constructors.

JCheckBox(String str)

It is used to create a JCheckBox with the specified string.

JCheckBox(String str, Boolean state)

It is used to create a JCheckBox with the specified string and initial state.

JCheckBox(String str, Icon icon)

It is used to create a JCheckBox with the specified string and icon.

Methods for JCheckBox in java:

The most commonly used JCheckBox()methods are as follows:

void setSelected(Boolean state)

The setSelected()method is used to change the state of the JCheckBox.

String getText()

The getText() method is used to return the text in the field.

void addItemListener(ItemListener il)

This method is used to configure an event handler to a checkbox.

void isSelected()

The is Selected ()method is used to return the state of the JCheckBox.

Sample Program for JCheckbox in java:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*<applet code="Jcheck"width=500 hieght=500></applet>*/
public class Jcheck extends JApplet implements
{
JChecjBox jc1,jc2,jc3;
JTextField jtf;
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
jc1=new JCheckBox("ug");
jc2=new JCheckBox("pg");
jc3=new JCheckBox("phd");
jc1.addItemListener(this);
jc2.addItemListener(this);
jc3.addItemListener(this);
c.add(jc1);
c.add(jc2);
c.add(jc3);
jtf=new JTextField(25);
c.add(jtf);
}
public void itemStateChanged(ItemEvent e)
{
if(e.getItemSelectable()==jc1)
{
jtf.setText("undergraduate");
}
else
if(e.getItemSelectable()==jc2)
{
jtf.setText("post graduate");
}
else
if(e.getItemSelectable()==jc3)
{
jtf.setText("reserch");
}
}
}
}

Sunday, 7 April 2019

JButton in Java | What is JButton in java swing with example - Online Help

JButton in Java:

The JButton in java is a class which is used to push the button. The JButton class has three constructors are as follows:

JButton()

It is used to create an empty JButton.

JButton(String str)

It is used to create a JButton with the specified string.

JButton(String str, Icon icon)

It is used to create a JButton with a specified icon;

An image is placed on the button. The following constructors are as follows:

ImageIcon(String filename)

It is used to construct an icon where the image is stored in a file.we can set the boundary of the JButton.The general form is:
                   
                                               void setBounds(int s,int y,int w,int h)
here,
w-width
h-height

Methods for JButtons in java:

The most commonly used JButton() methods are as follows:

Void addActionListener(ActionEvent ae)

This is used to add the specified action listener to receive action events from the corresponding JButton.

void setActionCommand(String actionstr)

The setActionCommand() methods are used to set the string for the action event of the JButton.

void setText(String str)

The setText() method is used to set the text for the fields.

void setEnabled(boolean state)

The setEnabled()method is used to enable or disable the JButton.

void setBackground(Color bgcolor)

The setBackground() method is used to color the background of the JLabel.

void setForeground(Color strcolor)

The setForeground() method is used to set the color of the string for the JLabel.

Sample program for JButton in java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*<applet code="JButtonDemo" width=500 height=500></applet>*/
public class JButtonDemo extends JApplet implements ActionListener
{
JButton b1,b2;
JTextField tf;
public void init()
{
Container c=getContenetPane();
c.setLayout(new FlowLayout());
b1=new JButton("internet explorer");
b1.setActionCommand("MicrosoftInternetExplorer");
b1.addActionListener(this);
b1.setBounds(20,20,150,50);
c.add(b1);
b2=new JButton("NN");
b2.setActionCommand("Netscape navigator");
b2.addActionListener(this);
b2.setBounds(20,20,150,50);
c.add(b2);
}
public void actionPerformed(ActionEvent ae)
{
tf.setText(ae.getActionCommand());
}
}

Friday, 1 March 2019

swing components in java

Swing Components in java:

Swing components facilitate efficient graphical user interface(GUI) development. These components are a collection of lightweight visual components. swing components contain a replacement for the heavyweight AWT components as well as Complex user interface components such as trees and tables.




Swing components contain a pluggable look and feel. This allows all applications to run with the native look and feel on different platforms. PL&F allows the application to have the same behavior on various platforms. JFC contains operating systems neutral look and feel. swing components do not contain peers. 

Swing components allow mixing awt heavyweight and Sewing lightweight components in an application. The major difference between lightweight and the heavyweight component is that lightweight components can have transparent pixels while heavyweight components are always opaque. lightweight components can be nonregular while heavyweight components are always rectangular.

Swing components comprise a large percentage of the JFC release. The swing component toolkit consists of over 250 pure java classes and 75 interfaces contained in about 10 packages. They are used to build a lightweight user interface. Swing consists of user interface(UI) classes and non-user interface classes. The non-UI classes provide services and other operation for UI classes.


Swing features in Java:

MVC architecture :

The user can provide his own data model for a component by subclassing the model class or by implementing the appropriate interface. The Model-View-Controller architecture is used to consistently throughout the swing component set. The view and Controller parts of the architecture are combined in the component.

Nested Container:

The swing was designed to manage nested containers gracefully. The main heavyweight container(JWindow, JFrame etc.) as well as the major lightweight containers(JInternalFrame and JComponents) all delegate their operations to a JRootPane. This commonly produces a high degree of regularity in container nesting. In particular, since the fundamental component class(JComponent) contains a JRootPane, virtually any component can be nested within another.

Keystroke Handling:

A user can register interest in a particular combination of keystroke by creating a keystore object and registering it with the component. When the key combination is registered along with its association action, certain conditions have to be specified. These determine the time of initiation of the action.

Action objects:

Action interface object provides a single point of Control for the program. An example of this would be a toolbar icon and a menu item referencing the same action object. When action disabled, GUI items that reference it are automatically disabled.

Virtual desktop :

The JDesktopPane and JInternalFrame classes can be used to create a virtual desktop or multiple document interface. A JInternalFrame can be specified are cognizable, Expendables or closable, while JDesktopPane provides real estate for them to operate in.

Pluggable look and feel:

The user can select a look and feel and this can be plugged in . An interface made of Swing components can look like a win32 app,a motif app. it can use the new metal look and feel.

Wide variety of components:

Class names that start with J are components that are added to an application. For example JButton, Jlist,JPanel .



Difference between swing and AWT:

There are many differences between Java AWT and swing that are given below:

Swing
 AWT
Java swing components are platform independent
AWT components are platform dependent
Swing components do not need any native code to implement.
AWT  components can be implemented with the code
swing components are lightweight
 AWT components are  heavyweight
swing components support the pluggable look and feel
AWT does not support pluggable look and feel
Swing lets you specify which look and feel your programs GUI uses
 AWT components always have the look and feel of native platform
swing component provides a more powerful component such as a table, lists, scroll pane, color chooser, tabbed pane etc.
AWT  provides fewer components than swing.
 swing components don't have to be rectangular. For example, a button can be rounded
AWT components are always rectangular

GUI components:


Graphical user interface GUI  is a method of interacting with a computer through a metaphor direct manipulation of graphical images and widgets(windows widgets).A GUI(pronounced “gooey”) component is an object with which the user interaction via the keyboard or another form of input.

Common GUI Event types and listener interfaces:

There are many different types of events that can occur when the user interacts with a GUI. The information about any GUI event that occurs is stored in an object of a class that extends the AWT event. These event types are used with both AWT and swing components. Additional events types that are specific to swing GUI components are declared in the package javax.swing.event.

The three parts to the event handling mechanism are as follows:

Event source: The event source is the GUI component with which the user interacts.

Event object: The event object encapsulates information about the event that occurred, such as a reference to the event source and any event-specific information.

Event listener: An event listener is an object that is notified by the event source when an event occurs. The method of event listener receives an event object when the event listener is notified of the event.

The classes associated with swing components are:

  • JLabel class
  • JTextField
  • JTextArea
  • JButton
  • JOptionPane
  • JCheckBox
  • JCcomboBox
  • JSlider
  • JFrame
  • JList
  • JPanel
  • event handling
  • layout manager
  • graphics class