Monday, April 6, 2020

Use jTextField in java Netbeans with Properties and easy example

Use jTextField in java Netbeans with Properties and easy example

In java, when you want to input some text or value etc., you have to choose some palette. 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 some basic Properties Settings in Netbeans:

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.

 a). 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);

    }  

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

 c) Change Text Color or Foreground Color of JTextField:
private void TxtForeColorBtnActionPerformed(java.awt.event.ActionEvent evt) {                                               
   
   MyInputTxt. setForeground (Color.GREEN);
   //or 
   // For Custom Color        
   MyInputTxt. setForeground (new Color(146,220,120)); 
    
    }

 d) Change Font for jTextField:
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);  
 
    }

 e) Set Horizontal 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);

}

 f) Change border style of jTextField:
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);

    }

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

 Code for 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: