'JLable not showing up

public OpenFrame() {

    openFrame = new JFrame();

    loadButton.setBounds(150, 100, 150, 100);
    loadButton.addActionListener(this);
    loadButton.setText("Load Data");
    loadButton.setFocusable(false);
    loadButton.setFont(new Font("Arial", Font.BOLD, 20));
    resetButton.setBounds(150, 220, 150, 100);
    resetButton.addActionListener(this);
    resetButton.setText("Reset Data");
    resetButton.setFocusable(false);
    resetButton.setFont(new Font("Arial", Font.BOLD, 20));

    welcomeMessage = new JLabel("Hello there! Welcome back. Choose an option below:");
    welcomeMessage.setVerticalTextPosition(TOP);
    welcomeMessage.setHorizontalTextPosition(SwingConstants.CENTER);

    openFrame.setTitle("BudgetPlanner");
    openFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    openFrame.setResizable(false);
    openFrame.setLayout(null);
    openFrame.add(welcomeMessage);
    openMessage();
    openFrame.setLocationRelativeTo(null);
    openFrame.setSize(new Dimension(WIDTH, HEIGHT));
    openFrame.getContentPane().setBackground(Color.GRAY);
    openFrame.add(loadButton);
    openFrame.add(resetButton);
    openFrame.setVisible(true);
}

My JLabel is not being displayed on the screen. On this screen, I want my Label texts and two buttons to be displayed. Where did I go wrong?

p.s. please ignore the openMessage(); code in between!



Solution 1:[1]

Your problem is that you never set the bounds of the JLabel. Try setting the (x, y) coordinates to the desired location and it should work.

However, you also have other issues with regards to how you are adding components to the JFrame. For starters, never add components directly into the frame. Add the components to a panel and then add the panel to the frame's context pane. Also, never nullify the JFrame's layout manager.

I added the setBounds call on the label and worked for me.

public static void main(String[] args) {
    JFrame openFrame = new JFrame();

    JButton loadButton = new JButton();
    loadButton.setBounds(150, 100, 150, 100);
//    loadButton.addActionListener(this);
    loadButton.setText("Load Data");
    loadButton.setFocusable(false);
    loadButton.setFont(new Font("Arial", Font.BOLD, 20));

    JButton resetButton = new JButton();
    resetButton.setBounds(150, 220, 150, 100);
//    resetButton.addActionListener(this);
    resetButton.setText("Reset Data");
    resetButton.setFocusable(false);
    resetButton.setFont(new Font("Arial", Font.BOLD, 20));

    JLabel welcomeMessage = new JLabel("Hello there! Welcome back. Choose an option below:");
    welcomeMessage.setBounds(150, 50, 300, 30); // You were missing this
    welcomeMessage.setVerticalTextPosition(SwingConstants.TOP);
    welcomeMessage.setHorizontalTextPosition(SwingConstants.CENTER);

    openFrame.setTitle("BudgetPlanner");
    openFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    openFrame.setResizable(false);
    openFrame.setLayout(null);
    openFrame.add(welcomeMessage);
//    openMessage();
    openFrame.setLocationRelativeTo(null);
    openFrame.setSize(new Dimension(600, 400));
    openFrame.getContentPane().setBackground(Color.GRAY);
    openFrame.add(loadButton);
    openFrame.add(resetButton);
    openFrame.setVisible(true);
}

enter image description here


The correct way should be something like this

JPanel pane = new JPanel();
// set the size and other properties on the panel
...
// create the components and set their properties, like bounds.
pane.add(label);
pane.add(resetButton);
pane.add(loadButton);
...
// create JFrame and set it's properties.
openFrame.setContentPane(pane);

Solution 2:[2]

Make sure your JLabel have bonds

 welcomeMessage.setBounds(150, 50, 300, 30);

Then create the JPanel then add your JLabel inside JPanel you created.

JPanel myNewPanel = new JPanel();
pane.add(label);

Finally add your pane inside your JFrame.

add(myNewPanel);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hfontanez
Solution 2 Joshua Simon