Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Components disappear after minimizing the frame or after moving another window on top #234

Closed
dleonext opened this issue Jul 17, 2014 · 5 comments

Comments

@dleonext
Copy link

I'm using webcam-capture libraries and AWT to develop a simple interface for taking pictures from a webcam. The buttons and the combobox in my JFrame disappear after minimizing the window or after moving another window on top of it. Moving the pointer over the frame restores the components' visibility. I'm not skilled with Java UI, I can't figure out what's wrong with my code.

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;


public class ImageCaptureManager extends JFrame {

    private class SkipCapture extends AbstractAction {

        public SkipCapture() {
            super("Skip");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
        }
    }

    private class SnapMeAction extends AbstractAction {

        public SnapMeAction() {
            super("Snap");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
        }
    }

    private class captureCompleted extends AbstractAction {

        public captureCompleted() {
            super("Completed");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
        }
    }

    private class saveImage extends AbstractAction {

        public saveImage() {
            super("Save");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
        }
    }

    private class deleteImage extends AbstractAction {

        public deleteImage() {
            super("Delete");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
        }
    }

    private class StartAction extends AbstractAction implements Runnable {

        public StartAction() {
            super("Start");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            btStart.setEnabled(false);
            btSnapMe.setEnabled(true);
            executor.execute(this);
        }

        @Override
        public void run() {
            panel.start();
        }
    }

    private Executor executor = Executors.newSingleThreadExecutor();
    private Dimension captureSize = new Dimension(640, 480);
    private Dimension displaySize = new Dimension(640, 480);
    private Webcam webcam = Webcam.getDefault();
    private WebcamPanel panel;

    private JButton btSnapMe = new JButton(new SnapMeAction());
    private JButton btStart = new JButton(new StartAction());
    private JButton btComplete = new JButton(new captureCompleted());
    private JButton btSave = new JButton(new saveImage());
    private JButton btDelete = new JButton(new deleteImage());
    private JButton btSkip = new JButton(new SkipCapture());
    private JComboBox comboBox = new JComboBox();

    public ImageCaptureManager() {
        super("Frame");

        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowDeiconified(WindowEvent arg0) {
            }

            @Override
            public void windowClosing(WindowEvent e) {
            }
        });

        List<Webcam> webcams = Webcam.getWebcams();
        for (Webcam webcam : webcams) {
            System.out.println(webcam.getName());
            if (webcam.getName().startsWith("USB2.0 Camera 1")) {
                this.webcam = webcam;
                break;
            }
        }

        panel = new WebcamPanel(webcam, displaySize, false);
        webcam.setViewSize(captureSize);

        panel.setFPSDisplayed(true);
        panel.setFillArea(true);

        btSnapMe.setEnabled(false);
        btSave.setEnabled(false);
        btDelete.setEnabled(false);

        setLayout(new FlowLayout());

        Panel buttonPanel = new Panel();
        buttonPanel.setLayout(new GridLayout(10, 1));
        buttonPanel.add(Box.createHorizontalStrut(20));
        buttonPanel.add(btSnapMe);
        buttonPanel.add(Box.createHorizontalStrut(20));
        buttonPanel.add(btSave);
        buttonPanel.add(Box.createHorizontalStrut(20));
        buttonPanel.add(btDelete);
        buttonPanel.add(Box.createHorizontalStrut(20));
        buttonPanel.add(btComplete);
        buttonPanel.add(Box.createHorizontalStrut(20));
        buttonPanel.add(btSkip);

        JLabel label1 = new JLabel("Test");
        label1.setText("Bla bla bla");
        JLabel label2 = new JLabel("Test");
        label2.setText(" ");

        Panel captionAndWebcamPanel = new Panel();
        captionAndWebcamPanel.add(label1);
        captionAndWebcamPanel.add(label2);
        captionAndWebcamPanel.add(panel);
        captionAndWebcamPanel.add(label2);
        captionAndWebcamPanel.add(comboBox);
        captionAndWebcamPanel.setLayout(new BoxLayout(captionAndWebcamPanel, BoxLayout.Y_AXIS));

        add(captionAndWebcamPanel);

        add(buttonPanel);
        pack();
        setVisible(true);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        btStart.doClick();

        setSize(900, 600);
    }
}

Thanks in advance!

@sarxos
Copy link
Owner

sarxos commented Jul 17, 2014

Hi @dleonext,

I cannot confirm your issue. I tested your code on Windows XP SP2 32-bit and Ubuntu Linux 64-bit, both hosting Java 6 JDK, by running:

public static void main(String[] args) {
    new ImageCaptureManager();
}

I tried minimize and maximize window several time, moving another window on top of displayed frame, in all cases I see no effect - all components are being drawn correctly, no disappearance.

However, I googled a little bit and found these articles:

So I added super paint component call into the WebcamPanel code and I would like to ask you to test it. If this won't help then I don't know what else can be done since your code looks (and works) completely fine.

The newest snapshot JAR can be found here:

https://oss.sonatype.org/service/local/artifact/maven/redirect?r=snapshots&g=com.github.sarxos&a=webcam-capture&v=0.3.10-SNAPSHOT

@dleonext
Copy link
Author

Hello,
thanks for your immediate reply!

I just solved my issue using

JPanel buttonPanel = new JPanel();
[...]
JPanel captionAndWebcamPanel = new JPanel();

instead of:

Panel buttonPanel = new Panel();
[...]
Panel captionAndWebcamPanel = new Panel();

Do you have any idea why this has solved my problem?

@sarxos
Copy link
Owner

sarxos commented Jul 17, 2014

@dleonext,

Wow! I completely have no idea why changing Panel to JPanel fixed the issue. Maybe because Panel is old AWT (starting with Java 1.0) component and JPanel is from Swing. Swing is build on top of AWT, but I suppose that, due to some historical compatibility reasons, there may be some issues when using old (AWT) and new (Swing) components together.

Great to know the issue is now fixed :) I'm closing this ticket.

@sarxos sarxos closed this as completed Jul 17, 2014
@ApurvaMatey
Copy link

I am also facing same problem but in swing. I use JPanel to create panels. but my panels are overlapping... what should I do?

@sarxos
Copy link
Owner

sarxos commented Sep 25, 2017

@ApurvaMatey try using layout manager or set panels bounds in such a way that they do not overlap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants