Use jTextField in Java NetBeans with Properties and easy example

If we talk about TextBox in Java, It is called as jTextField. When you want to input some text or value, you have to choose some Palette in Java. One of them is jTextField as Input Box. So, here SKOTechlearn describe about jTextFiled in Java Swing Netbeans with some basic properties settings and some easy example.

Java Swing Gui Project in Netbeans

So, what point we cover in this post according to jTextField palette which is described bellow:

  1. jTextField Properties Settings in Netbeans
  2. jTextField Properties setting through code
  3. jTextFileld with Get and set Value Example

So, Lets start, First drag this control on jFrame , then proceed with above steps.


 1. jTextField Properties Settings in Netbeans

Here we change some basic properties, which is necessary to provide attractive look according to application requirement.
Properties
Details
editable
This property is used for lock or unlock TextField
background
You can choose Backcolor
foreground
You can choose text color
Font
Change Font with Font Name, Font Style and Font Size
horizontalAlignment
Set Text alignment (LEFT, RIGHT, CENTER, LEADING, TRAILING)
SelectedTextColor
Set Border like (Bevel Border, Empty Border, Line Border etc)
Font
Change selected text Forecolor from this properties
SelectionColor
Change text selection back color

Now, understand it with following figure.
jTextField Properties Settings in java
Properties Settings in java Netbeans
So, this way you can easily change some basic properties of jTextField in java Netbeans.

2. jTextField Properties Setting Through Code

Now, you can apply above given properties settings through following code.

 Lock or Unlock JTextField:

The following code is for unlock or make editable TextBox.
private void TxtPropertyBtnActionPerformed(java.awt.event.ActionEvent evt) {    
     
   MyInputTxt.setEditable(true);

  }  

The following code is for lock or disable edit in TextBox.
private void TxtPropertyBtnActionPerformed(java.awt.event.ActionEvent evt) {    
     
   MyInputTxt.setEditable(false);

 }  


 Change Background Color of jTextField :

First Import color class for background and foreground color:
import java.awt.Color;

Then write following code.
private void TxtBackColorBtnActionPerformed(java.awt.event.ActionEvent evt) {   
   
   MyInputTxt.setBackground(Color.yellow);
      //or 
    // For Custom Color        
   MyInputTxt.setBackground(new Color(246,220,220));    
 
  }  


 Change JTextField Foreground Color:

private void TxtForeColorBtnActionPerformed(java.awt.event.ActionEvent evt) {   
   
   MyInputTxt.setForeground(Color.GREEN);
   //or 
   // For Custom Color        
   MyInputTxt.setForeground(new Color(146,220,120)); 
    
  }


 Change jTextField Font:

Fist Import Font class on your project like bellow.
import java.awt.Font;

Then write following code.
private void TxtFontBtnActionPerformed(java.awt.event.ActionEvent evt) {  

        Font myFont = new Font("Calibri", Font.BOLD, 20);
        MyInputTxt.setFont(myFont);  
 
 }


 jTextField Alignment :

Import following class.
import javax.swing.JTextField;

After that write following code.
private void TxtHAlignBtnActionPerformed(java.awt.event.ActionEvent evt) {   

     MyInputTxt.setHorizontalAlignment(JTextField.CENTER);

}


 Change jTextField Border Style:

First import border class.
import javax.swing.border.Border;

Then write following code:
private void TxtBorderBtnActionPerformed(java.awt.event.ActionEvent evt) {  
       
    Border myborder = BorderFactory.createEtchedBorder(0, Color.cyan, Color.yellow);
    MyInputTxt.setBorder(myborder);

 }


 jTextField Selected Text Color and Selection Color :

 // For Seleted Text Color
 MyInputTxt.setSelectedTextColor(Color.gray);
  // for Selection Color
 MyInputTxt.setSelectionColor(Color.ORANGE);



3. jTextFileld with Get and Set Text Example

In this process we consider about how to change value of jTextField or retrieve value from one TextField to another in java Swing NetBeans.

jLabel Design in Java Netbeans

So, the following code example will explain all these.

In Java For Input value we use setText(String) and for retrieve value or text we use getText().

 Code For Input or Set Text:

private void SetTxtBtnActionPerformed(java.awt.event.ActionEvent evt) {   
      
        MyInputTxt1.setText("SKO Tech Learn Tips");
        
  }

 Get text from one jTextField and set to another:

private void GetTxtBtnActionPerformed(java.awt.event.ActionEvent evt) {   
    String myStr;
    myStr = MyInputTxt1.getText();
    MyInputTxt2.setText(myStr);
  }
GetText and SetText in Java
GetText and SetText Code in Java

Auto Resize Palette or controls in java

So this is the some basic properties settings in jTextField and use of  jTextField in Java Netbeans.

Open othe jframe through button click in java

0 comments: