Results 1 to 2 of 2
  1. #1
    MGeorge's Avatar
    MGeorge is offline Aging Hacker
    Join Date
    Sep 2014
    East (San Francisco) Bay, California, USA
    Rep Power

    Default JLabels not being visibly removed from JPanel

    This is similar to Renxx's post Dynamically adding JLabel to my GUI

    I am working on a Swing app that displays a JPanel (GridLayout) full of JLabels. Each label has a popup menu which, among other things allows the label to be removed from the panel (underlying file is deleted or moved). It all works just fine until I get down to the last 3 labels (the first 2 are not removable). Deleting the file triggers the code to remove the label and validate the panel, but for this last removable label, the label remains visible until I manually reload the panel or resize the frame. At that time the label disappears.

    In writing the SSCE below I found the problem differs slightly. It visibly removes groups of labels when an entire row becomes empty. The top row is never removed.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    public class Test extends JFrame implements ActionListener {
        private JPanel content;
        private JPanel labelPanel;
        JButton delete;
        public Test() {
            content = (JPanel) this.getContentPane();
        private void init() {
            JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            labelPanel = new JPanel(new GridLayout(0, 3, 6, 6));
            content = new JPanel(new BorderLayout(6, 4));
            String[] words = {"one", "two", "three", "four", "five", "six" };
            for (String word : words) {
                JLabel label = new JLabel(word);
            delete = new JButton("Delete");
            content.add(labelPanel, BorderLayout.CENTER);
            content.add(buttonPanel, BorderLayout.SOUTH);
        public void actionPerformed(ActionEvent ae) {
            Component[] list = labelPanel.getComponents();
            int count = labelPanel.getComponentCount();
            if (count > 0) {
                JLabel lbl = (JLabel) labelPanel.getComponent(count - 1);
                System.out.format("removed %-5s  %d %s\n", lbl.getText(), 
                                  count, count == 1 ? "remains" : "remain");
            delete.setEnabled(count > 0);
        public static void main(String[] args) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Test().setVisible(true);
    This must be a nubie problem but, believe it or not, I've been writing Java code since '99 and C for almost 20 years before that so I don't really consider myself a newbie.

    TIA for any clarification on why this is happening and/or suggestions on how to fix it.

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Rep Power

    Default Re: JLabels not being visibly removed from JPanel

    It is just an update problem - if you change the window size it shows up. ;)

    Add a

    to repaint the windoe immediately
    after your .validate().
    I like likes!

Similar Threads

  1. JLabels on JPanel in wrong spot
    By Bestsanchez in forum AWT / Swing
    Replies: 4
    Last Post: 01-26-2012, 01:27 AM
  2. updating a gameBoard (JPanel filled with JLabels)
    By Bloitz in forum AWT / Swing
    Replies: 3
    Last Post: 07-07-2011, 06:31 PM
  3. Replies: 5
    Last Post: 03-05-2011, 12:43 AM
  4. JPanel and JLabels Not Repainting
    By phosphide in forum AWT / Swing
    Replies: 6
    Last Post: 11-13-2010, 03:17 AM
  5. Replies: 6
    Last Post: 06-16-2010, 06:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts