JLabel not visible on Jpanel

18,766

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.

Share:
18,766
Mohammad Faisal
Author by

Mohammad Faisal

@faisal6621 facebook linkedin Er. Mohammad Faisal (Blogger) Er. Mohammad Faisal (Wordpress) If you are looking for something to vote on, I'm looking for votes (ideally upvotes 🤪) on [my questions/answers with zero][1]...

Updated on June 04, 2022

Comments

  • Mohammad Faisal
    Mohammad Faisal almost 2 years

    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");
        }
    }
    
  • Mohammad Faisal
    Mohammad Faisal over 12 years
    added magnet.validate() but still not working. Neither reapint() nor validate() is working. I'd to make the label visible on the panel.
  • Mohammad Faisal
    Mohammad Faisal over 12 years
    I'd used setLocation() does it now enough because setBounds() may trim the text. setBounds() also not working. I request you to please check the code by executing.
  • Harry Joy
    Harry Joy over 12 years
    @MohammadFaisal: No. When you use null in layout you must explicitly define bounds of a component.
  • Harry Joy
    Harry Joy over 12 years
    @AndrewThompson If he will not specify bounds for a null layout the component will never come into the picture. Isn't this right?
  • Mohammad Faisal
    Mohammad Faisal over 12 years
    Isn't any way out, because it trims my text. the user can enter any number of lines.
  • Harry Joy
    Harry Joy over 12 years
    @MohammadFaisal: Use some layout.
  • Mohammad Faisal
    Mohammad Faisal over 12 years
    Actually I want these labels draggable, therefore I'd setLayout(null)
  • Andrew Thompson
    Andrew Thompson over 12 years
    OK - I think the null layout should be swapped out for a custom layout, but my main objection to the answer was that setBounds() is so much guesswork as to the size of the component. It would be better (even when using a null layout) to set it to the preferred size then call setLocation() rather than setBounds().
  • Mohammad Faisal
    Mohammad Faisal over 12 years
    @AndrewThompson: setLocation() is not working. I'd forcefully have to use setBounds(). Can I get the preferred size after using the setBounds()?
  • Harry Joy
    Harry Joy over 12 years
    @AndrewThompson: hmm... now I get it. Mohammad Faisal: instead of setBounds() use setSize(getPreferedSize()) and setLocation(); This might work for you.
  • Harry Joy
    Harry Joy over 12 years
    @AndrewThompson: I have updated the answer. Is it right for now?
  • Andrew Thompson
    Andrew Thompson over 12 years
    "Is it right for now?" Gets an up-vote here. Good call on the edit. :)
  • clockw0rk
    clockw0rk over 3 years
    setbounds is the preferred way when u want to update the element, f.e. when the size of parent JFrame changes. u will have to work with percentual numbers or a clever calculation of getDimension()