Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTextField displayed as slit when using FlowLayout...please explain

Can someone please explain to me, why every time I use the FlowLayout Layout manager my textfields are displayed as slits.

I have bumped my head against this problem for some time now, and I can't seem to figure out why it goes wrong.

I get a feeling it is a simple thing that I am overlooking time and time again, so if someone would please explain this phenomenon to me, I would be forever 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);
    }
}
like image 316
TrashCan Avatar asked May 11 '12 10:05

TrashCan


People also ask

What does FlowLayout do in Java?

A flow layout arranges components in a left-to-right flow, much like lines of text in a paragraph. Flow layouts are typically used to arrange buttons in a panel. It will arrange buttons left to right until no more buttons fit on the same line.

What is the role of JTextField?

JTextField is a lightweight component that allows the editing of a single line of text. For information on and examples of using text fields, see How to Use Text Fields in The Java Tutorial. JTextField is intended to be source-compatible with java. awt.

Which method is used to select all text in the JTextField?

Seems like you have to use selectAll() in conjunction with requestFocusInWindow(), to get the desired effect.

What event does JTextField generate?

JTextField: generates "ActionEvent" using the ActionListener interface. When a user press "Enter", an ActionEvent is fired and performs tasks defined in actionPerformed() method.


1 Answers

From the Swing layout manager tutorial

The FlowLayout class puts components in a row, sized at their preferred size. If the horizontal space in the container is too small to put all the components in one row, the FlowLayout class uses multiple rows. If the container is wider than necessary for a row of components, the row is, by default, centered horizontally within the container

So you need to adjust the preferred size of your textfield, preferably by using the setColumns method.

Note that if you want your text field to span the whole width you might want to use another layout then the FlowLayout for the reason quoted above

For example, the following code gives a nice looking JTextField, but I have 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);
      }
    } );
  }
}
like image 99
Robin Avatar answered Oct 02 '22 19:10

Robin