0

I am trying to set the width of the JTextField but non of the methods to change it's size are working I have tried to change the amount of columns to change the width of the JTextField but it's not working?

What is causing this?

And how will I be able to change the width?

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


public class main {
   public static void main(String[] args) {
        //Create and set up the window.
        final JFrame frame = new JFrame("Layout 1");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(0, 0, 1500, 1000);
        frame.setBackground(new Color(255,255,255));
        frame.setLayout(null);
        Container pane = frame.getContentPane();
        pane.setLayout(null);
        JLayeredPane jlp = new JLayeredPane();
        frame.setContentPane(jlp);



        JPanel container = new JPanel();
        //container.setBackground(new Color(241,241,241));
        container.setBackground(Color.RED);
        container.setBounds(0,0,frame.getWidth(), 75);
        container.setLayout(null);

        JPanel containerA = new JPanel();
        containerA.setBackground(Color.blue);
        containerA.setBounds(90  ,10,50,50);

        JPanel containerB = new JPanel();
        containerB.setBackground(Color.RED);
        containerB.setBounds(95  ,20,50,50);

        //This is the top textfield
        JTextField textField = new JTextField(1000);
        textField.setLayout(null);
        textField.setPreferredSize(new Dimension(1000,1000));
        textField.setBounds(0,0,2300,50);
        textField.setColumns(1000);
        textField.setSize(1000,10);
        textField.invalidate();
        textField.setMinimumSize(new Dimension(1000,100));



        container.add(textField);

        jlp.add(container, Integer.valueOf(2));
        jlp.add(containerA, Integer.valueOf(1));
        jlp.add(containerB, Integer.valueOf(3));

        container.setVisible(true);
        frame.invalidate();
        frame.setVisible(true);

    }
}
  • I think it's a duplicated issue, refer to http://stackoverflow.com/questions/2897506/how-can-i-control-the-width-of-jtextfields-in-java-swing for more details. – Boluny Nov 29 '15 at 09:13
  • You should manipulate your components only in the Event Dispatch Thread – Jens Schauder Nov 29 '15 at 09:18
  • Just for the heck of it, try and set the PreferredSize to match the MinimumSize and see how that works. – DevilsHnd - 退職した Nov 29 '15 at 09:37
  • Start with [How to Use Text Fields](http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html) for the basics of creating a better structured program that uses a layout manager. When you use layout managers effectively then there is no need to attempt to manage the size of the components. – camickr Nov 29 '15 at 19:22

1 Answers1

0

You need to change just this line, and will get width of text field,

 textField.setSize(1000,25);
Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55