1. JTextField Introduction
JTextField component will render an Edit control, which will take user input. We can construct it by passing an integer as a parameter and the integer specify the char-width. For example, if we pass a value of 15, Java Swing will create a text field to show 15 letters. We can change the colour of the JTextField by calling its setForground method. The setFont method is useful for setting a font to the JTextField.
2. About the JTextField Example
Have a look at the example below:
The example shows three JTextFields. Each text fields contains the text in different text colour. Also, we may notice that the texts alignments are different. First JTextField shows the text left aligned. The second control shows the text aligned towards the middle of the text field. Final text field shows the text right aligned. You may also notice we display all texts in a bigger size than the default.
3. Create JTextFields
The below code shows that first we create flow layout for the JFrame. Then we create three JTextFields for our example. In all three instances, we specified a value of 30. This tells Java Swing the size of the text field in terms of Character Width and swing calculates the size in pixels. So, all our text boxes can show thirty letters.
1 2 3 4 5 6 7 8 9 |
//Sample 01: Set Size and Position setBounds(100, 100, 500, 300); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Text Fields JTextField tf1 = new JTextField(30); JTextField tf2 = new JTextField(30); JTextField tf3 = new JTextField(30); |
The add
method on the content pane will add all three text boxes to the JFrame
Window. Note, the order is important and Swing container displays the components in the order in which it was added to it.
1 2 3 4 |
//Sample 03: Add to the Content Pane ControlHost.add(tf1); ControlHost.add(tf2); ControlHost.add(tf3); |
The method setText will set the text for the JTextFields. In the below code, we set three different texts for the JTextFields.
1 2 3 4 5 |
//Sample 04: Set Default Text //This will usually Empty by default tf1.setText("Left Align"); tf2.setText("Center Align"); tf3.setText("Right Align"); |
4. Set Font & Color for JTextField
In the code snippet below at line number 2, we create an AWT Font. The first param is the font name, 2nd one is the font style and third param tells the size of the Font. In our example, we create Plain font style. One can try other styles also like bold & italic. Then, using the setFont method, the Font is set to the JTextField. The method setForeGround will take AWT color object and sets that to the component. Here the component is JTextField.
1 2 3 4 5 6 7 8 |
//Sample 05: Set Font and Color Font f = new Font("Verdana", Font.PLAIN, 16); tf1.setFont(f); tf2.setFont(f); tf3.setFont(f); tf1.setForeground(Color.RED); tf2.setForeground(Color.GRAY); tf3.setForeground(Color.BLUE); |
5. JTextField Text Alignment
The function
setHorizontalAlignment will decide where to place the text. Mean towards the left or right or in the middle. To study how it works, we set three different alignments for our three sample Java Swing JTextFields. Note how we pass the constants to the function setHorizontalAlignment
method call.
1 2 3 4 |
//Sample 06: Set Alignment tf1.setHorizontalAlignment(JTextField.LEFT); tf2.setHorizontalAlignment(JTextField.CENTER); tf3.setHorizontalAlignment(JTextField.RIGHT); |
6. Watch JTextField as Youtube Video
7. Code Reference
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package tube.coding.examples; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Font; import java.awt.HeadlessException; import javax.swing.JFrame; import javax.swing.JTextField; public class JFrameDemo extends JFrame { public JFrameDemo(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 500, 300); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Text Fields JTextField tf1 = new JTextField(30); JTextField tf2 = new JTextField(30); JTextField tf3 = new JTextField(30); //Sample 03: Add to the Content Pane ControlHost.add(tf1); ControlHost.add(tf2); ControlHost.add(tf3); //Sample 04: Set Default Text //This will usually Empty by default tf1.setText("Left Align"); tf2.setText("Center Align"); tf3.setText("Right Align"); //Sample 05: Set Font and Color Font f = new Font("Verdana", Font.PLAIN, 16); tf1.setFont(f); tf2.setFont(f); tf3.setFont(f); tf1.setForeground(Color.RED); tf2.setForeground(Color.GRAY); tf3.setForeground(Color.BLUE); //Sample 06: Set Alignment tf1.setHorizontalAlignment(JTextField.LEFT); tf2.setHorizontalAlignment(JTextField.CENTER); tf3.setHorizontalAlignment(JTextField.RIGHT); } } |
Categories: Swing
Tags: setFont, setForeGround, setHorozontalAlignment, setText