by Dinesh Thakur Category: Swing

The JTextField is a text field of a single line. Class inherits from the javax.swing.text.JTextComponent. Its methods include:

• public JTextField (int NumCols): creates a JTextField NumCols occupying columns. If it contains the text exceeds this size will not be lost even if it can not display all simultaneously;
• public JTextField (String text, int NumCols): same as above but also present an initial text;
• public void setFont (Font font): sets the font with which the text is displayed;
• public String getText () returns its content in left;
• public String getSelectedText (): returns the selected text inside;
• public void copy (): Copies the selected text to the system clipboard;
• public void cut (): same as above but also removes the selected text;
• public void setEditable (boolean b) Enables or disables the ability to change the text content.


import java.awt.*;

import javax.swing.*;
import java.awt.event.*;
public class JavaExampleTextFieldAlignmentInJApplet extends JApplet
    JTextField Txt = new JTextField("Welcome To Java");
    JButton BtnLft,BtnRght,BtnCntr;
    public void init()
        Container Cntnr = getContentPane();
        Cntnr.setLayout(new FlowLayout());
        Cntnr.add(new ButtonPanel());
        class ButtonPanel extends JPanel implements ActionListener
               public ButtonPanel()
                  BtnLft = new JButton("Left");           
                  BtnRght = new JButton("Right");           
                  BtnCntr = new JButton("Center");           
                 setLayout(new FlowLayout());
                 add(new JLabel("Alignment"));
                  public void actionPerformed(ActionEvent e1)
                        if(e1.getSource() == BtnLft)
                         else if(e1.getSource() ==BtnRght)
                                        else if(e1.getSource() ==BtnCntr)
/*<APPLET  CODE =JavaExampleTextFieldAlignmentInJApplet.class  WIDTH=360 HEIGHT=290></APPLET>*/

JTextField Alignment in Java Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Search Content

Popular Article