Question

I am trying to put the imageJ window inside a desktop pane that I have created but I am having trouble doing this. How do you use imageJ2 source code in order to make a customized GUI where the imageJ window is inside the desktop pane? This is for imageJ2: https://github.com/imagej/imagej

Below is the code where I have tried making a customized GUI where it displays the image window. But it is not like the original window that is made by imageJ because it does not contain any information about the image and you can't select it. I want to make the imageJ window be inside a desktop pane but I am not able to do so.

public class CustomGui extends ImageWindow implements InternalFrameListener, ActionListener {

    MyInternalFrame frame;
    int openFrameCount =0;
    ImagePlus img;
    boolean called = false;
    JMenuItem save;
    String title;
    final String SHOW ="show";

    public CustomGui(ImagePlus imp, String title, JDesktopPane desktop, final JMenuItem save) {
        super(imp);
        // TODO Auto-generated constructor stub
        setCall();
        img = imp;
        save.setEnabled(true);
        //this.title = title;
        FileInfo file = imp.getFileInfo();
        this.save = save;
        frame = new MyInternalFrame(title, img, save);
        JTextField text = new JTextField(img.getHeight());
        text.setVisible(true);
        text.setEditable(false);;
        frame.add(text);

        frame.add(new JLabel(new ImageIcon(imp.getImage())));

        frame.setVisible(true);
        desktop.add(frame);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {

        }
        frame.addInternalFrameListener(this);
        // options(name, desktop);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals(SHOW)) {
            save.setEnabled(true);
            frame.addInternalFrameListener(this);
        }

        System.out.println("beingg called");
    }

    public void setCall() {
        called = true;
    }

    public void setCallF() {
        called = false;
    }

    public boolean getCall() {
        return called;
    }

    private class MyInternalFrame extends JInternalFrame {

        static final int xPosition = 30, yPosition = 30;
        public MyInternalFrame(String title, ImagePlus img, JMenuItem save) {
            super(title, true,true, true, true);
            setSize(img.getHeight(), img.getWidth());

            // Set the window's location.
            setLocation(xPosition * openFrameCount, yPosition * openFrameCount);
            save.setEnabled(true);
        }
    }

    @Override
    public void internalFrameActivated(InternalFrameEvent arg0) {
        // TODO Auto-generated method stub
        setCall();
        save.setEnabled(true);
        System.out.println("resized");
    }

    @Override
    public void internalFrameClosed(InternalFrameEvent arg0) {
        // TODO Auto-generated method stub
        setCall();
        save.setEnabled(false);
        System.out.println("resized");
    }

    @Override
    public void internalFrameClosing(InternalFrameEvent arg0) {
        // TODO Auto-generated method stub
        setCall();
        save.setEnabled(false);
        System.out.println("resized");
    }

    @Override
    public void internalFrameDeactivated(InternalFrameEvent arg0) {
        // TODO Auto-generated method stub
        setCall();
        save.setEnabled(false);
        System.out.println("resized");
    }

    @Override
    public void internalFrameDeiconified(InternalFrameEvent arg0) {
        // TODO Auto-generated method stub
        setCall();
        save.setEnabled(true);
        System.out.println("resized");
    }

    @Override
    public void internalFrameIconified(InternalFrameEvent arg0) {
        // TODO Auto-generated method stub
        setCall();
        save.setEnabled(false);
        System.out.println("resized");
    }

    @Override
    public void internalFrameOpened(InternalFrameEvent arg0) {
        // TODO Auto-generated method stub
        //setCall();
        save.setEnabled(true);
        System.out.println("opened");
    }

}
Was it helpful?

Solution

Suddenly there are a rash of questions on StackOverflow about this topic [1, 2, 3, 4, 5, 6, 7]. They have little to do with ImageJ specifically. They are all just questions about adapting a fundamentally SDI-based application (java.awt.Frame) to an MDI-based one (javax.swing.JInternalFrame).

Doing this is a huge endeavor, especially for a junior programmer. You will need to rewrite substantial portions of the ImageJ 1.x codebase, as it was never designed to work as anything other than a single SDI AWT-based UI.

You will need to be thoroughly familiar with Java Swing, particularly MDI and JInternalFrame; see the Java Tutorial on JInternalFrame to get started.

But that alone will not be enough. Unless you invest hundreds or even thousands of hours with an extremely careful design, and leverage advanced techniques such as Java bytecode manipulation, you will end up with something that is not backwards compatible with ImageJ 1.x, and hence does not work with the majority of its plugins. And no matter what you do, some plugins will not behave as you want, because they directly rely on the fact that ImageJ1's data structures are SDI AWT components by nature.

So before charging down this track, I want to ask: what do you really want to accomplish? Why are you doing this? What is the end goal? How will it help users?

If you seriously want to redesign ImageJ as an MDI application for some reason, then I would encourage you to check out ImageJ2. One of its primary goals is proper separation of concerns—i.e., keeping the data model logic isolated from the display logic—so that things like MDI user interfaces become possible. It has been in development now for over four years, and we do have a proof of concept MDI Swing user interface. But it is not very functional yet, and the UI framework itself still needs more iterations—these components are still very much in beta, and will be for at least another year. You are very welcome to get involved, and we are happy to answer specific technical questions about it, but it would be a very steep learning curve for anyone who is not already intimately familiar with Java Swing.

Much easier might be to find an alternative way to accomplish your goals here. I encourage you to write to the ImageJ mailing list describing your needs at a high level. Then we can offer some suggestions on ways forward.

Note: This answer was adapted from a reply I wrote to the ImageJ mailing list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top