Hey I was just wondering if there is a way to set JTextField width for example 50% of the screen size. I know how to get the dimensions of screen just JTextField sets it to number of characters not pixels
textfield = new JTextField("Way hello there", 20);
is there a way to do this ?
Setting the size of JTextField
in pixels leads you down the path of using absolute positioning which is generally not advisable. From Doing Without a Layout Manager
Although it is possible to do without a layout manager, you should use a layout manager if at all possible. Layout managers also can be reused easily by other containers, as well as other programs.
Have a look at the functionality offered by existing layout managers and avoid using the setXXXSize() component methods. For example, GridLayout
( or even GridBagLayout
) can be used to size a JTextField
width to 50% of a frame client area.