1

I am trying to create a new JTextField in my Jframe. I want to play around with the positioning of the textfield. I tried using setBounds and setLocation to change the position of the text box but it doesn't change the location of the text box at all.

This is my code:

public class GUI_Tutorial extends JFrame {

    public static void main(String[] args) {

        GUI_Tutorial frame = new GUI_Tutorial();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setSize(1000, 800);
        frame.setVisible(true);
        frame.setTitle("Calculator");
        frame.setLayout(new FlowLayout());

    }

    public GUI_Tutorial() {
        //frame.setLayout(new FlowLayout());
        JTextField num1;
        num1 = new JTextField(10);
        add(num1);
        num1.setVisible(true);
        num1.setLocation(5, 5);     
    } 
}

May I know what am I doing wrong?

c0der
  • 18,467
  • 6
  • 33
  • 65
Lord Jesus
  • 81
  • 8
  • **"I want to play around with the positioning"** do you mean pragmatically or by dragging ? Have a look in [A Visual Guide to Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) – c0der May 13 '18 at 15:13
  • @c0der Pragmatically. – Lord Jesus May 13 '18 at 15:16
  • The right way to change layout is to use layout managers. Read the proposed tutorial. – c0der May 13 '18 at 15:17
  • Is layout a neccessary thing? – Lord Jesus May 13 '18 at 15:20
  • *"Is layout a neccessary thing?"* **Yes.** Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 13 '18 at 15:22
  • It is the right way to layout. You can set `frame.setLayout(null);` and set bounds pragmatically , but that is **not** a good practice. – c0der May 13 '18 at 15:23
  • @c0der I tried both situation, setLayout(null) vs setLayout(new FlowLayout). If it is set to null, the textbox won't be visible . If set to FlowLayout, the textbox is visible. Why is that? – Lord Jesus May 13 '18 at 15:25
  • **"If it is set to null, the textbox won't be visible"** because the layout manger sets the bounds of the components. When it is set to `null` the bounds are not set. Read the proposed tutorial. – c0der May 13 '18 at 15:28
  • Setting the bounds in this case won't be good practice as it might be hard to add event handlers to such situation. Thanks though – Lord Jesus May 13 '18 at 15:31

2 Answers2

2

Your problem is one of layout managers. When you add a component to a container, the layout manager will dictate where the component will go. A JFrame's contentPane (the JFrame sub-container that holds its components) uses a BorderLayout by default, and items added to this container in a default manner will fill the central portion of the container, will fill it completely if nothing else is added to other BorderLayout locations.

Possible solutions for placing items:

  • The worst suggestion, use a null layout. You would do this by calling getContentPane().setLayout(null);. But when you do this, you the programmer are 100% responsible for the exact position and size of all components added. This leads to hard to maintain GUI's that might not even work on other platforms -- so don't do this.
  • Use a GUI builder to help build your program: this has definite advantages, one being that it shields you from having to directly understand the layout managers, but this also is a disadvantage, because once you run into edge cases (and you usually will and quickly) that knowledge is essential. My own view is to initially avoid using this so as to better understand the layout managers, and then once you're familiar with the managers, then sure use this as needed.
  • Better is to learn and experiment with the layout managers, and Borders, playing with placement of components. Remember that you can nest containers (usually JPanels) and each can be given its own layout manager, allowing for complex GUI's that are created easily and maintained easily.

Note that

  • (again) JFrames (actually their contentPanes), JDialogs, and other top-level windows use BorderLayout by default, that JPanels use FlowLayout by default, that JScrollPanes use their own specialty layout, one that you will likely never want to muck with, and that most other components/containers, such as JComponent, JLabel,... have null layouts by default.
  • Best resource is the tutorial: Lesson: Laying Out Components Within a Container
  • Borders can also be useful here, especially the EmptyBorder which can allow you to put a blank buffer zone around your components.
  • Start playing with the simpler layout managers, FlowLayout, BorderLayout, GridLayout, then BoxLayout, before moving to the more complex
  • Thanks for the advice, right now I am trying to hardcode the GUI to my liking. Considering my situation, is it better to just learn with GUI builder tools ( drag and drop method) ? – Lord Jesus May 13 '18 at 15:35
  • @LordJesus: If you have to maintain the GUI, you will find hard-coding the position the worst solution. Why? Any time you want to change the position of a component, you have to then cascade a bunch of changes (manually) to any other component that might be affected by this change, and this can lead quickly to a nightmare situation. Also, while your GUI might look good on your own computer, if you port it to another, you might find components not shown, or text not shown. Just don't do it. – DontKnowMuchBut Getting Better May 13 '18 at 15:39
  • Okay. So how should I learn GUI then? Should I learn other framework such as JavaFx , AWT or etc ? Or is Swing a good way to start? Should I learn building GUI by hardcoding or using builder tools? – Lord Jesus May 13 '18 at 15:41
  • @LordJesus: Myself, if I were starting out, I'd skip Swing and AWT and go with the latest Java GUI library, JavaFX. I'd avoid builders to start with and instead strive to learn the library through and through. – DontKnowMuchBut Getting Better May 13 '18 at 15:43
  • Is JavaFx much easier? Is it possible to hardcode in JavaFx? I always want to learn by hardcode GUI in the beginning to understand the fundamentals before using builder tools. – Lord Jesus May 13 '18 at 15:44
  • @LordJesus: "easier" is a loaded term and one I can't answer. Do what we've all done -- search for the tutorials and dive in. – DontKnowMuchBut Getting Better May 13 '18 at 15:45
  • My whole purpose of this post is to design a simple GUI calculator with 2 textfields ( 2 input numbers) and few different operators (buttons) and display the result of the operand based on what the user chooses. – Lord Jesus May 13 '18 at 15:46
1

Try removing frame.setLayout(new FlowLayout());. You'll then need to use num1.setBounds(x, y, width, height) rather than setLocation()

But, as other users have pointed out, you should be using a layout manager. Read up on the different layouts and choose the best one for your GUI.

178865
  • 13
  • 4