Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I center a JTextfield

I'm having trouble centering a JTexfield in my program.The Textfield does not appear aligned with the JButton. I've tried using the x.setHoriontalAlignment(JTextfield.CENTER); but to no avail. Any ideas?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPanel;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.GridBagLayout;

public class WidgetProject implements ActionListener
{
 //class constants
private static final Color BUTTON_COLOUR1 = Color.WHITE;
private static final int BUTTON_HEIGHT = 75;
private static final int BUTTON_WIDTH = 400;
private static final int TEXTFIELD_HEIGHT = 400;
private static final int TEXTFIELD_WIDTH = 50;
private static final String SECONDS_PER_MINUTE = "Seconds to Minutes or Minutes to Seconds";
private static final String BUTTON2_MODIFIED_LABEL = "yes";
private static final String POUNDS_PER_KILOGRAM = "Pounds to Kilograms or Kilograms to Pounds";
private static final String CHANGE_MY_LABEL = "1";
private static final int HEIGHT = 400;
private static final int WIDTH = 400;

// instance fields
 private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JFrame frame;
private JTextField textInput;
private JPanel panel;

/**
 * A free-standing frame with two buttons.
 * 
 * @param title the title of this frame
 */
public WidgetProject(String title)
{
    // Establish the frame.
    frame = new JFrame(title);
    frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(WIDTH, HEIGHT));

    // Establish button dimensions.
    Dimension buttonDimension = new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT);

    // Establish Textfield dimensions.
    Dimension textDimension = new Dimension(TEXTFIELD_HEIGHT, TEXTFIELD_WIDTH);

    // Create and add the first button.
    button1 = new JButton(SECONDS_PER_MINUTE);
    button1.setActionCommand(CHANGE_MY_LABEL);
    button1.addActionListener(this);
    button1.setPreferredSize(buttonDimension);
    button1.setMinimumSize(buttonDimension);
    button1.setMaximumSize(buttonDimension);
    button1.setBackground(BUTTON_COLOUR1);
    frame.add(button1);

     // Create and add the second button.
    button2 = new JButton(POUNDS_PER_KILOGRAM);
    button2.setActionCommand(CHANGE_MY_LABEL);
    button2.addActionListener(this);
    button2.setPreferredSize(buttonDimension);
    button2.setMinimumSize(buttonDimension);
    button2.setMaximumSize(buttonDimension);
    button2.setBackground(BUTTON_COLOUR1);
    frame.add(button2);

    // Create an input text field.
    textInput = new JTextField(20);
    textInput.setPreferredSize(textDimension);
    textInput.setMinimumSize(textDimension);
    textInput.setMaximumSize(textDimension);
    textInput.setHorizontalAlignment(JTextField.CENTER);
    panel = new JPanel();
    panel.add(textInput);
    String string = textInput.getText();


    // Display the frame and text field.
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.add(textInput);

} // end of constructor ButtonDuo

like image 354
NABIL SHAHIN Avatar asked Mar 19 '13 18:03

NABIL SHAHIN


People also ask

How do you center a JDialog?

If you really want to center a JDialog on screen, you can use code like this: // center a jdialog on screen JDialog d = new JDialog(); d. setSize(400, 300); d.

What is the difference between a JTextField and a JTextArea?

The main difference between JTextField and JTextArea in Java is that a JTextField allows entering a single line of text in a GUI application while the JTextArea allows entering multiple lines of text in a GUI application.

Can you enter more than one line in a JTextField?

Only one line of user response will be accepted. If multiple lines are desired, JTextArea will be needed. As with all action events, when an event listener registers an event, the event is processed and the data in the text field can be utilized in the program.


2 Answers

First :x.setHorizontalAlignment(JTextField.CENTER); will set the text in the center not the JTextField

If you want to make the JTextField on the center, just create the panel = new JPanel(); and add your buttons on it Like this :panel.add(button1);

panel.add(button2);

panel.add(textInput, BorderLayout.CENTER); ---> here i set the textInput in the center of the jpanel

then : add the 'Jpanel' to the Jframe :frame.add(panel);

Try this :

package javaapplication1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPanel;

class WidgetProject implements ActionListener {
    //class constants

    private static final Color BUTTON_COLOUR1 = Color.WHITE;
    private static final int BUTTON_HEIGHT = 75;
    private static final int BUTTON_WIDTH = 400;
    private static final int TEXTFIELD_HEIGHT = 400;
    private static final int TEXTFIELD_WIDTH = 50;
    private static final String SECONDS_PER_MINUTE = "Seconds to Minutes or Minutes to Seconds";
    private static final String BUTTON2_MODIFIED_LABEL = "yes";
    private static final String POUNDS_PER_KILOGRAM = "Pounds to Kilograms or Kilograms to Pounds";
    private static final String CHANGE_MY_LABEL = "1";
    private static final int HEIGHT = 400;
    private static final int WIDTH = 400;
// instance fields
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JFrame frame;
    private JTextField textInput;
    private JPanel panel;

    /**
     * A free-standing frame with two buttons.
     * 
     * @param title the title of this frame
     */
    public WidgetProject(String title) {
        // Establish the frame.
        frame = new JFrame(title);
        panel = new JPanel();
        frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(WIDTH, HEIGHT));

        // Establish button dimensions.
        Dimension buttonDimension = new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT);

        // Establish Textfield dimensions.
        Dimension textDimension = new Dimension(TEXTFIELD_HEIGHT, TEXTFIELD_WIDTH);

        // Create and add the first button.
        button1 = new JButton(SECONDS_PER_MINUTE);
        button1.setActionCommand(CHANGE_MY_LABEL);
        button1.addActionListener(this);
        button1.setPreferredSize(buttonDimension);
        button1.setMinimumSize(buttonDimension);
        button1.setMaximumSize(buttonDimension);
        button1.setBackground(BUTTON_COLOUR1);
        panel.add(button1);

        // Create and add the second button.
        button2 = new JButton(POUNDS_PER_KILOGRAM);
        button2.setActionCommand(CHANGE_MY_LABEL);
        button2.addActionListener(this);
        button2.setPreferredSize(buttonDimension);
        button2.setMinimumSize(buttonDimension);
        button2.setMaximumSize(buttonDimension);
        button2.setBackground(BUTTON_COLOUR1);
        panel.add(button2);

        // Create an input text field.
        textInput = new JTextField(20);
        textInput.setPreferredSize(textDimension);
        textInput.setMinimumSize(textDimension);
        textInput.setMaximumSize(textDimension);
        textInput.setHorizontalAlignment(JTextField.CENTER);

        panel.add(textInput, BorderLayout.CENTER);
        String string = textInput.getText();

        frame.add(panel);
        // Display the frame and text field.
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    } // end of constructor ButtonDuo

    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
like image 106
Alya'a Gamal Avatar answered Oct 19 '22 19:10

Alya'a Gamal


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

public class JTableComponent{
  public static void main(String[] args) 
{
  new JTableComponent();
  }

  public JTableComponent(){
  JFrame // Establish the frame.
  frame = new JFrame("title");
  frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setPreferredSize(new Dimension(700, 700));

  // Establish button dimensions.
  Dimension buttonDimension = new Dimension(120, 120);

  // Establish Textfield dimensions.
  Dimension textDimension = new Dimension(110, 110);


  // Create an input text field.
  JTextField textInput = new JTextField(20);
  textInput.setPreferredSize(textDimension);
  textInput.setMinimumSize(textDimension);
  textInput.setMaximumSize(textDimension);
  textInput.setHorizontalAlignment(JTextField.CENTER);
  JPanel  panel = new JPanel();
  panel.add(textInput);
  String string = textInput.getText();


  // Display the frame and text field.
  frame.pack();
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
  frame.add(textInput);
  }
}

I have checked your code .its already in middle..

like image 2
Biswajit Avatar answered Oct 19 '22 18:10

Biswajit