Question

I am kind of new to ImageJ and I am not able to display the imageJ toolbar in my own GUI. Does anybody know how to display the toolbar. I have tried tool.show() but that gets deprecated. I don't know where within the ImageJ code the Toolbar class is being called. Thanks in advance.

Update: Here is part of the code

 JMenuItem toolbar = new JMenuItem("Toolbar");
    toolbar.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            JFrame f = new JFrame();
            Canvas c = new Canvas();
            Toolbar tool = new Toolbar();
            tool.setVisible(true);

            System.out.println(tool.isShowing());
            //tool.show();
            //tool.getAccessibleContext();
            tool.getGraphics();
            tool.installStartupTools();

        }

    });
Was it helpful?

Solution

I believe you wish to you use .setVisible(true) instead of .show()

Toolbar extends Canvas as can be seen here. The two methods I just mentioned are not proper to Toolbar, rather they are methods java.awt.Canvas

UPDATE

Ok I see what you are doing. Really, revalidate() may be the piece you are missing to refresh the screen. Here is some code and some greater explanation:

    import ij.gui.Toolbar;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class Test1 {

    public static Toolbar tool;
    public static JFrame f;
    public static boolean state = false;

    public static void main(String[] args) {                
        f = new JFrame();
        f.setSize(600,600);
        Canvas c = new Canvas();
        f.add(c);

        tool = new Toolbar();
        Graphics g  = tool.getGraphics();
        tool.installStartupTools();

        JMenuBar menubar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
        JMenuItem toolbar = new JMenuItem("Toolbar");
        menu.add(toolbar);
        menubar.add(menu);

        f.setJMenuBar(menubar);

        toolbar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                state = !state;
                tool.setVisible(state);
                tool.revalidate();
            }



        });

        tool.setVisible(false);
        f.add(tool);

        f.setVisible(true);
    }

}

Make sure you take your initialization code outside of your listener. That only happens once at the beginning. The action listener should be reserved for stuff that happens once per event. Like, I toggle the toolbar on and off each time the Menu item is selected. And adding the toolbar to the frame seems it is ok. It will appear at the very top (with the default layout scheme) and you can use it as you need to use it. You certainly do have other options where to display the toolbar though.

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