Java 7 resize a JFrame (my GUI): the componentListener is not getting fired

4.7k views Asked by At

I've Googled around and could not find an answer:

I have a JFrame as a GUI and set a componentListener like

this.addComponentListener(new CL(this, logger));

In the componentListener I have the componentResized, etc...

public void componentResized(ComponentEvent e) {
    if (e.getSource() == gui) {
       here I do the code
    }
}

Everything is working fine under Java 6, but under Java 7, the Jframe is being resized but is not being repainted: the component listener is not getting called

I tried some alternatives from StackOverflow in the way of coding, but no way to make it work.

When I minimize the GUI window and restore it, then "componentResized" is being called.

Anyone an idea what's going on and why it works under Java 6 and not Java 7.

Here is a sample code:

public class Frame extends JFrame {
    private JPanel  panel;
    public Frame() {
        super();
        getContentPane().setBounds(new Rectangle(0, 0, 200, 200));
        getContentPane().setLayout(null);
        getContentPane().add(getPanel());
        this.addComponentListener(new ComponentListener() {
            @Override
            public void componentResized(ComponentEvent e) {
                System.out.println("Window Resized: Frame");}
            @Override
            public void componentMoved(ComponentEvent e) {}
            @Override
            public void componentShown(ComponentEvent e) {}
            @Override
            public void componentHidden(ComponentEvent e) {}});
        this.setSize(200, 200);
        this.setPreferredSize(new Dimension(200, 200));
        this.pack();
    }
    public JPanel getPanel() {
        if (panel == null) {
            panel = new JPanel();
            panel.setBackground(Color.RED);
            panel.setBounds(0, 0, 200, 200);
            panel.setPreferredSize(new Dimension(200, 200));
        }
        return panel;
    }
}

And this is the startup class:

public class StartUp {
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        Frame app = new Frame();
        app.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        app.pack();
        app.setVisible(true);
    }
}

When run under Java 6 you get the "Window Resized: Frame" message.
The same run under Java 7, no messages at all.

Below are 2 links to Pictures:

  • Java 6

Java 6 Frame Image

  • Java 7

Java 7 Frame Image

1

There are 1 answers

5
mKorbel On BEST ANSWER
  • your code (before balast removed, changed to standard) work for me correctly

  • ComponentListener firing an event from every pixels

  • this is basic property of componentResized(), otherwise required SwingTimer.restart() untill resize ended)

balast removed, changed to standard

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class MyFrame {

    private JFrame frame = new JFrame();
    private JPanel panel;

    public MyFrame() {
        frame.add(getPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                System.out.println("Window Resized: Frame");
            }
        });
    }

    public JPanel getPanel() {
        if (panel == null) {
            panel = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(300, 200);
                }
            };
            panel.setBackground(Color.RED);
        }
        return panel;
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                MyFrame app = new MyFrame();
            }
        });
    }
}