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 6887391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:52:45+00:00 2026-05-27T05:52:45+00:00

I’m working on a program in which I’d used a JTextArea , JButton ,

  • 0

I’m working on a program in which I’d used a JTextArea, JButton, JLabel and JPanel.
The logic I’d to implement is: user types a text in the given textArea and then click on the button. On button click I’d to retrieve the text from the textArea and create a label with the written text(as in textArea) and show it on the panel.
Everything I’d done previously is correct but the problem is with the label and panel. The label is not visible on the panel.

The code snippets is:

import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.BevelBorder;

/**
 *
 * @author mohammadfaisal
 * http://ermohammadfaisal.blogspot.com
 * http://facebook.com/m.faisal6621
 * 
 */
public class CodeMagnets extends JFrame{
    private JTextArea area4Label;
    private JLabel codeLabel;
    private JButton createButton;
    private JPanel magnet;

    public CodeMagnets(String title) throws HeadlessException {
        super(title);
        magnet=new JPanel(null);
        JScrollPane magnetScroller=new JScrollPane(magnet);
              magnetScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        magnetScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(BorderLayout.CENTER, magnetScroller);
        JPanel inputPanel=new JPanel();
        area4Label=new JTextArea(5, 30);
        area4Label.setTabSize(4);
        JScrollPane textScroller=new JScrollPane(area4Label);
        inputPanel.add(textScroller);
        createButton=new JButton("Create code magnet");
        createButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //codeLabel=new JLabel(area4Label.getText());
                codeLabel=new MyLabel(area4Label.getText());//this is for my new question
                codeLabel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
                codeLabel.setLocation(50, 20);
                codeLabel.setVisible(true);
                magnet.add(codeLabel);
                area4Label.setText("");
                //pack();
            }
        });
        inputPanel.add(createButton);
        add(BorderLayout.SOUTH, inputPanel);
        //pack();
        setSize(640, 480);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new CodeMagnets("Code Magnets");
    }
}
  • 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-05-27T05:52:46+00:00Added an answer on May 27, 2026 at 5:52 am

    You need to repaint()/validate() your panle after adding new components in it dynamically. So after this:

    magnet.add(codeLabel);
    

    add this:

    magnet.validate();
    

    or

    magnet.repaint();
    

    Also one thing you are using null layout for magnet panel. So must have to setBounds() of jLable before adding it to magnet panel. So it becomes

    public void actionPerformed(ActionEvent e) {
        codeLabel=new JLabel(area4Label.getText());
        codeLabel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
        codeLabel.setBounds(50, 20, 100, 100);
        magnet.add(codeLabel);
        magnet.repaint();
        area4Label.setText("");
    }
    

    It is not recommended to use null as layout, you should use proper layout like BorderLayout or GridLayout or even simpler FlowLayout based on your requirement.


    As said by @Andrew use something like:

    codeLabel.setSize(codeLabel.getPreferredSize());
    codeLabel.setLocation(50, 20);
    

    instead of

    codeLabel.setBounds(50, 20, 100, 100);
    

    This will solve the size issue of jLabel.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I have a text area in my form which accepts all possible characters from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I would like to run a str_replace or preg_replace which looks for certain words

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.