Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8601291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:48:29+00:00 2026-06-12T01:48:29+00:00

I have a JPanel and JButton on the JFrame . on runtime add JLabel

  • 0

I have a JPanel and JButton on the JFrame.
on runtime add JLabel to JPanel When click JButton.

I use of the following code:

 panel.setLayout(null);

 jLabel _lbl=new jLabel();
 _lbl.setText("Label");
 panel.add(_lbl);
 panel.validate();

but no display any JLabel in JPanel.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:48:30+00:00Added an answer on June 12, 2026 at 1:48 am

    I see you create a JLabel called _lbl:

     JLabel _lbl=new JLabel();
    

    but you never add it to your panel. Instead you add a new JLabel with no text to your panel:

     panel.add(new JLabel());
    

    This would ofcourse construct an empty label which wont be visible.

    Also try calling revalidate() and repaint() on your JPanel instance after adding the JLabel like so:

    JLabel _lbl=new JLabel("Label");//make label and assign text in 1 line
    
    panel.add(_lbl);//add label we made
    
    panel.revalidate();
    panel.repaint();
    

    With this you may also need to call pack() on your frames instance so as to resize the JFrame to fit the new components.

    Also please never use a null/Absolute layout this is very bad practice (unless doing animation) and may prove to be problematic and very hard to use.

    Rather use a LayoutManager:

    • A Visual Guide to Layout Managers

    or if you only have a single component on the JPanel simply call add(label); as it will stretch to the JPanel size.

    UPDATE:

    Here is a small sample. Simply adds JLabels to the JPanel each time JButton is pressed:

    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class JavaApplication116 {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new JavaApplication116().createAndShowUI();
                }
            });
        }
    
        private void createAndShowUI() {
            JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            initComponents(frame);
    
            frame.setResizable(false);
            frame.pack();
            frame.setVisible(true);
        }
    
        private void initComponents(final JFrame frame) {
            final JPanel panel = new JPanel(new FlowLayout());
            JButton button = new JButton("Add label");
            button.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    JLabel _lbl = new JLabel("Label");//make label and assign text in 1 line
    
                    panel.add(_lbl);//add label we made
    
                    panel.revalidate();
                    panel.repaint();
    
                    frame.pack();//so our frame resizes to compensate for new components
                }
            });
            frame.getContentPane().add(panel, BorderLayout.CENTER);
            frame.getContentPane().add(button, BorderLayout.SOUTH);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am adding JPanel to JFrame on a JButton click. It adds the JPanel
I have a subclass of JFrame(it contains JButton, Title and Jpanel) and i added
I have the following code. Class KochSnowflakesMenu is a grid JPanel with three buttons.
I have a JPanel that I want to add some components. in particular JButton
I want to have the following layout: (i.e. jbutton and jlabel are placed at
I have a JFrame. This JFrame contains a JButton. I click the JButton and
I have a JFrame with JScrollPane in it. I have JPanel inside a scrollPane.
I have a JPanel (A) which contains another JPanel (B). Each panel implements a
I have a JPanel, which I would like to detect the following events (1)
I have a JFrame containing three JPanel. The first JPanel contains a JTextField and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.