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

Monday, 8 April 2019

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

JTextField in Java | JTextField in java swing with Example - Online Help

JTextField in Java:  

       The JText component is used to accept text input from the user. There are two classes available in the text component.
-TextField
-TextArea.

     The JTextField class can be used to enter a single line of text. The JTextField in java has four constructors. They are


JTextField()

It is used to create an empty JTextField.

JTextField(String str)

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

JTextField(int numcols)

it is used to create a JTextField with the specified number of columns.

JTextField(String str,int numcols)

It is used to creates a JTextField with the specified text and the specified number of columns.

Methods for JTextField in java:

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

void addActionListener(ActionListener Handler)

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

String get Text()

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

void setEditable(Boolean state)

The setEditable() method is used to set the field as being editable or fixed. If the state is true, the JTextField may be edited. If the state is false, it cannot be edited.

void setText(String str)

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

Sample program for JTextField in java:

import java.awt.*;
import javax.swing.*;
/*<applet code="textdemo" width=500 height=500></applet>*/
public class textdemo extends JApplet
{
JTextField jtf=new JTextField(25);
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
c.add(jft);
jft.setText("welcome to online help");
}
}