Java Swing JFrame Layout

48,434

Solution 1

Add your components to a JPanel and then add that panel to the ContentPane of JFrame.

JFrame window = new JFrame();
JPanel mainframe = new JPanel();

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(0,0,200,200);

JButton jb = new JButton();
jb.setText("Leech");

mainframe.add(jb);

JTextField link = new JTextField(50);
mainframe.add(link);

window.getContentPane().add(mainframe);
window.pack();
window.setVisible(true);

Solution 2

The default layout on a JFrame is a BorderLayout. Calling the add method on a Container with such a layout is equivalent to a call add(..., BorderLayout.CENTER). Each of the locations of the BorderLayout can contain only one element. Hence making two calls

mainframe.add(jb);
mainframe.add(link);

results in a CENTER containing the last component you added. If you want to avoid this you can either add it to different locations, or use another layout manager (for example a FlowLayout) by calling JFrame#setLayout

Solution 3

Instead of adding directly Components to the JFrame, use a JPanel as container with the desired LayoutManager.

Here you can find several tutorials on layout managers.

Basically in Swing the LayoutManager is responsible for laying out the children Components (establishing their position and their size), so every container component you use inside your app, should be configured with the appropiate LayoutManager.

Share:
48,434
DanMatlin
Author by

DanMatlin

Updated on July 05, 2022

Comments

  • DanMatlin
    DanMatlin almost 2 years

    I just wrote a simple code where I want a textfield and a button to appear on the main frame, but after running all I see is the textfield.

    If I write the code of the button after the textfield then only the button is displayed.

    Any idea why?

        JFrame mainframe=new JFrame();
        mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainframe.setBounds(0,0,200,200);
        JButton jb=new JButton();
        jb.setText("Leech");
        mainframe.add(jb);
        JTextField link=new JTextField(50);
        mainframe.add(link);
        mainframe.pack();
        mainframe.setVisible(true);
    
  • Robin
    Robin over 12 years
    which is basically the same as setting FlowLayout as LayoutManager on the JFrame, since that is the default layout form the JPanel
  • Ungeheuer
    Ungeheuer over 8 years
    The link on LayoutManager is now dead. Please fix.
  • Michael
    Michael about 7 years
    Can't believe this is the accepted answer. Robin's answer below is the correct answer. Just have to understand what the default layout manager of a JFrame is and the default position something is added to in a BorderLayout if one isn't specified.
  • Ibrahim
    Ibrahim about 7 years
    @Michael what is the point of your comment? Do you think this answer is wrong or you don't like how it is coded that way?
  • Michael
    Michael about 7 years
    @Ibrahim the OP's question was "Any idea why?", you did not explain why his code wasn't working, Robin's answer explains why
  • Michael
    Michael about 7 years
    You should never recommend setting layout to null, that is just plain wrong.
  • Ibrahim
    Ibrahim about 7 years
    @Michael whenever I ask "why something is not working", I mean "can someone make it work for me". But that is how I think and I assume that is what he want, "make both button and textfield show in the window". He accepted it as an answer because he found what he was looking for. For other people who really want to know why have up voted Robin's answer and you can see he got more votes so you can read the answer with most vote, which is what I do on stackoverflow.
  • NoName
    NoName almost 5 years
    So what's the point of panels if you can just do this in the frame?