Li xiaoyu
Li xiaoyu

Reputation: 53

How to construct a JTextfield, and how to use the method selectAll()

I want to construct a Swing component JTextField, here is my Code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JTextFieldGui{

    JTextField textField;
    JLabel labelInput;
    JLabel labelOutput;

    public static void main(String[] args) {
        JTextFieldGui gui = new JTextFieldGui();
        gui.go();
    }

    public void go(){
        JFrame frame = new JFrame();
        JPanel panelInput = new JPanel();
        JPanel panelOutput = new JPanel();
        labelInput = new JLabel("Your first name: ");
        labelOutput = new JLabel("Enter your name, and you will see it here.");
        textField = new JTextField(20);
        JButton enter = new JButton("Enter");
        JButton selectAll = new JButton("Select all text");

        frame.setSize(300,200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panelInput.setLayout(new BoxLayout(panelInput, BoxLayout.X_AXIS));

        textField.addActionListener(new LabelActionListener());
        enter.addActionListener(new LabelActionListener());
        selectAll.addActionListener(new TextFieldActionlistener());

        frame.getContentPane().add(BorderLayout.NORTH, panelInput);
        panelInput.add(labelInput);
        panelInput.add(textField);
        panelInput.add(enter);
        panelInput.add(selectAll);
        frame.getContentPane().add(BorderLayout.CENTER, panelOutput);
        panelOutput.add(labelOutput);
    }

    class LabelActionListener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            labelOutput.setText(textField.getText());
        }
    }

    class TextFieldActionlistener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            textField.selectAll();
        }
    }
}

Question1: I define the width of the text field in 20 columns, but it always take up a row, like image:

https://i.sstatic.net/l6iQy.png

Question2: how to use the selectAll() method, I use it in a listener of the button selectAll, but when I click the button, nothing happens, why

Upvotes: 0

Views: 68

Answers (1)

camickr
camickr

Reputation: 324098

I define the width of the text field in 20 columns, but it always take up a row,

This is the rule of a BoxLayout. A component is resized to fill the space available. A JTextField doesn't have a maximum size so it grows. The buttons and label do have a maximum size so they don't grow.

Don't use a BoxLayout, just use a FlowLayout. It will automatically leave space between each component which is a better layout.

I use it in a listener of the button selectAll, but when I click the button, nothing happens, why

Focus is still on the button. The selected text only displays when the text field has focus.

So in he listener code you need to add:

textField.requestFocusInWindow();

The following code is old:

frame.getContentPane().add(BorderLayout.NORTH, panelInput);
  1. you don't need to get the content pane. You can just add the component to the frame.

  2. the constraint should be the second parameter

  3. there are new constraints to make the names more meaningful

So the code should be:

frame.add(panelInput, BorderLayout.PAGE_START, panelInput);

See the section from the Swing tutorial on How to Use BorderLayout for more information.

Upvotes: 1

Related Questions