Showing posts with label swing component in java. Show all posts
Showing posts with label swing component in java. Show all posts

Saturday, 13 April 2019

JFrame in Java - Online Help

JFrame in Java:

The JFrame in java is a class which is used to create a user-defined window. The JFrame class defines the following constructors:

JFrame()

It is used to create empty JFrame.

JFrame(String str)

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

Methods for JFrame in  Java:

Container getContentPane()

The getContentPane() method is used to return the content pane object for this JFrame.

void add(component c)

This method is used to add the given components to the content pane of this frame.

Sample program for JFrame in java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JFramedemo
{
public static void main(String arg[])
{
JFrame jf= new JFrame("online help");
Container c=jf.getContentPane();
c.setLayout(new BorderLayout());
c.add(new JButton("yes");
jf.setBounds(0,0,300,200);
jf.setVisible(true);
}
}


Monday, 8 April 2019

JList in Java | Online Help

JList in java:

The JList is used to let the user select one or more items from the list. The JList class defines the following constructors:

JList():

It is used to create an empty JList with default four rows visible.

JList(vector v)

It is used to create a JList with the specified vector object.

Methods for JList in Java:

The most commonly used JList() method is as follows:

void addItem(object obj)

This method is used to add a new object to the list.

int getSelectedIndex()

This method is used to return the index of the selected item.

int[] getSelectedItem()

This method is used to return the string value of the item selected.

Sample program for JList in Java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class jlistdemo
{
public static void main(String args[])
{
String[] str={"professor","Associate professor","Assistant professor")
JFrame jf=new JFrame("JListDemo");
JList j1=new JList(str);
jf.getContentPane().add(j1,BorderLayout.CENTER);
jf.pack();
jf.setVisible(true);
}
}

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);
}
}

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());
}
}

JLabel in Java | JLabel in Java Swing with Example

JLabel in java:

      The JLabel in java is a  class which can be used to add a text in the container.it displays a single line read-only data which cannot be changed by the user. JLabel class has three constructors. They are:


JLabel():

It is used to create an empty JLabel

JLable(String str):

It is used to create a JLabel with the specified string str.

JLabel(String str, int align):

It is used to create a JLabel with the specified string str and the alignment. The possible value of the parameters are defined in the string constants are JLabel.The values LEFT,RIGHT,CENTER,NORTH,EAST,and WEST and SOUTH.

The possible values are:

SwingConstants.CENTER or JLabel.CENTER-Displays a string in the JLabel with center alignment.

SwingConstants.LEFT or JLabel.LEFT-Displays a string in the JLabel with the left alignment.

SwingConstants.RIGHT or JLabel.RIGHT-Displays a string in the JLable with right alignment.

Example:

                      JLabel jl1=new JLabel("java",SwingConstants.CENTER);

The above statement is equal to

                      JLabel jl1=new JLabel("java",JLabel.CENTER);

The above example is used to create a JLabel "java" with the center alignment.

Methods:

The most commonly used to JLabel() methods are as follows:

void setText(String str)

The setText() method is used to set the string displayed on the label

void getText()

The getText() method is used to written the current text displayed on the label.

void setIcon(Icon icon)

The setIcon() method is used to specify the icon to display on the label.

void setHorizontalAlignment(int align)

The setHorizontalAlignment() method is used to set the alignment of the label's constants along the x-axis.

void setVerticalAlignment(int align)

The setVerticalAlignment() method is used to set the alignment of the label's constants along the y-axis.

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 JLabel in java:

import java.awt.*;
import javax.swing.*;
/*<applet code="JLabeldemo" width=500 height=500></applet>*/
public class JLabelDemo extends JApplet
{
public void intit()
{
Container c= getContentPane();
JLabel jl1=new JLabel("welcome to Online help");
c.setLayout(new FlowLayout());
c.add(jli);
}
}