JTextField textField = new JTextField("Default input", 10);
textField.setColumns(20);
panel.validate();
After changing the size of a text box with the setColumns method, you need to call the validate method of the surrounding container. The validate method recomputes the size and layout of all components in a container. After you use the validate method, the layout manager repaints the container, and the changed size of the text field will be visible.
In general, you want to let the user add text (or edit the existing text) in a text field. Quite often these text fields start out blank. To make a blank text field, just leave out the string as a parameter for the JTextField constructor:
JTextField textField = new JTextField(20);
You can change the contents of the text field at any time by using the setText method from the TextComponent parent class. For example:
textField.setText("Hello!");
You can find out what the user typed by calling the getText method. This method returns the exact text that the user typed. To trim any extraneous leading and trailing spaces from the data in a text field, apply the trim method to the return value of getText:
String text = textField.getText().trim();
To change the font in which the user text appears, use the setFont method.
textField.setColumns(20);
panel.validate();
After changing the size of a text box with the setColumns method, you need to call the validate method of the surrounding container. The validate method recomputes the size and layout of all components in a container. After you use the validate method, the layout manager repaints the container, and the changed size of the text field will be visible.
In general, you want to let the user add text (or edit the existing text) in a text field. Quite often these text fields start out blank. To make a blank text field, just leave out the string as a parameter for the JTextField constructor:
JTextField textField = new JTextField(20);
You can change the contents of the text field at any time by using the setText method from the TextComponent parent class. For example:
textField.setText("Hello!");
You can find out what the user typed by calling the getText method. This method returns the exact text that the user typed. To trim any extraneous leading and trailing spaces from the data in a text field, apply the trim method to the return value of getText:
String text = textField.getText().trim();
To change the font in which the user text appears, use the setFont method.
Jtextfld.java
import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class Jtextfld extends JFrame { private JPanel contentPane; private JTextField textField; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Jtextfld frame = new Jtextfld(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Jtextfld() { setAlwaysOnTop(true); setResizable(false); setTitle("TextFiled "); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 425, 256); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel(" Change TextFiled width"); lblNewLabel.setBounds(94, 11, 197, 38); contentPane.add(lblNewLabel); textField = new JTextField(); //new JTextField(10); or textField.setColumns(10); or textField.setSize(int width,int height); textField.setBounds(144, 86, 86, 20); contentPane.add(textField); textField.setColumns(10); JButton btnNewButton = new JButton("Original width"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { textField.setSize(86, 20); } }); btnNewButton.setBounds(70, 171, 117, 23); contentPane.add(btnNewButton); JButton btnChangeWidth = new JButton("Change width"); btnChangeWidth.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { textField.setSize(140, 20); } }); btnChangeWidth.setBounds(246, 171, 117, 23); contentPane.add(btnChangeWidth); } }
OUTPUT:-
No comments:
Post a Comment