Text Field

The Text Field in java specially instantiated from JTextField by importing
javax.swing.*;
Here now we are gonna make a simple textfield with definite size and it’s position in JFrame.

First of all we should know dat JFrame, JPanel, JTextField are the classes of the java.swing.These all classes are included in Java swing hierarchy.Now to code for textfield we’ll need the Layout and dat will be defined soon by ourself.In Defaut the java uses it’s own Layout for textbox.The main code for defining the position and size is given below:
textbox_name.setBounds(x_axis,y_axis, width, height);

The detail code of the text box is given below
[sourcecode=’java’]
//Import directives
import javax.swing.*;

public class Text{

//Atributes1
//—————
static JPanel panel;
static JFrame frame;

//Attributes2
//—————
int x_axis = 100;
int y_axis = 10;
int width = 100;
int height = 20;
int column = 4;

JLabel label;
JTextField textBox;

public Text() {

//JFrame
frame = new JFrame(“test”);
//Jpanel
panel = new JPanel();

//label
label = new JLabel(“User”);

//textbox
textBox = new JTextField(4);

//looping for the textbox position and it’s size
for(int i = 0; i<10; i++){ switch(i){ case 1: label.setBounds(x_axis,y_axis, width, height); case 2: textBox.setBounds((x_axis+40),y_axis, width, height); } } //controls panes panel.setLayout(null); panel.add(label); panel.add(textBox); //DISPLAYING frame.getContentPane().add(panel); frame.setVisible(true); frame.setSize(400, 100); } public static void main (String[] args) { Text finalObject = new Text(); } } [/sourcecode]

Leave a Reply