JTextField is displayed as a cut when using FlowLayout ... please explain

Can someone explain to me why every time I use the FlowLayout layout manager my text fields appear as slots.

I have run into this problem for some time, and I cannot seem why this is going wrong.

I get the feeling that it’s just that I look over time again and again, so if someone please explain this phenomenon to me, I would always be grateful.

import java.awt.Container; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JTextField; public class Console { public Console() { makeConsole(); } private void makeConsole() { JFrame console = new JFrame("ArchiveConsole"); Container base = console.getContentPane(); base.setSize(400, 250); console.setSize(base.getSize()); base.setLayout(new FlowLayout(FlowLayout.CENTER, 5,5)); JTextField tf = new JTextField(); tf.setSize(base.getWidth(), 25); base.add(tf); console.setVisible(true); } } 
+4
source share
4 answers

In the Swing Layout Manager tutorial

The FlowLayout class puts components in a row, the size of which depends on their size. If the horizontal space in the container is too small to fit all the components on one line, the FlowLayout class uses several lines. If the container is larger than necessary for a number of components, the default row is horizontally inside the container

So, you need to adjust the preferred size of the text field, preferably using the setColumns method.

Please note that if you want your text field to cover the entire width, you can use a different layout and then FlowLayout for the above reason

For example, the following code gives a nice JTextField , but I hardcoded the number of columns

 import javax.swing.JFrame; import javax.swing.JTextField; import java.awt.Container; import java.awt.EventQueue; import java.awt.FlowLayout; public class TextFieldWithFlowLayout { public static void main( String[] args ) { EventQueue.invokeLater( new Runnable() { @Override public void run() { JFrame console = new JFrame("ArchiveConsole"); Container base = console.getContentPane(); base.setLayout(new FlowLayout( FlowLayout.CENTER, 5,5)); JTextField tf = new JTextField(); tf.setColumns( 20 ); base.add(tf); console.pack(); console.setVisible(true); } } ); } } 
+6
source

Usage: JTextField tf = new JTextField (25);

Instead: JTextField tf = new JTextField (); tf.setSize (base.getWidth (), 25);

+1
source

When you use LayoutManagers, do not try to set the size and position manually. You contradict what the LayoutManager does. Layout and layout elements of the linker based on constraints and preferred / minimum / maximum size. Most Swing components handle this automatically, so you should usually not force their preferred size.

As for your JTextField, since it does not contain text, its preferred size is close to zero. Use setColumns to specify a preferred parameter or call setPreferredSize

+1
source

Thanks!

The parameter used in the JTextField function is very important!

 JPanel panel_buttons = new JPanel(new FlowLayout(FlowLayout.LEFT)); panel_buttons.add(...); ... panel_buttons.add(...); JTextField txt_search = new JTextField(20); panel_buttons.add(txt_search); 

If the change is 20-30 or big, you may not be able to find it. Maybe txt_search is on the next line.

0
source

Source: https://habr.com/ru/post/1411945/


All Articles